spicy.net: Loading image from embedded resource

spicy.net

Sunday, July 02, 2006

Loading image from embedded resource

Guys to add an image as an embedded resource in a Windows Forms application,Please traverse these steps.

  1. add the image file to the project with Project Add Existing Item.
  2. Then select the file in the Solution Explorer, go to the Properties Window, and set its Build Action property to Embedded Resource. At this point the file will be embedded within your compiled file, when you build the project.
  3. The following routine shows how to load the embedded image in a PictureBox, at runtime:

    //load the bitmap...
    string bmpName = "MyNameSpace.sync.bmp";
    System.IO.Stream strm = null;
    try
    {
    strm = this.GetType().Assembly.GetManifestResourceStream(bmpName);
    string[] s = this.GetType().Assembly.GetManifestResourceNames();
    pictureBox1.Image = new Bitmap(strm);
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message.ToString());
    }
    finally
    {
    if(strm != null)
    strm.Close();
    }


Note that we have use the project's default namespace as a prefix while reading image from Resource.

Please go thru this Extensive Sample

0 Comments:

Post a Comment

<< Home