Loading image from embedded resource
Guys to add an image as an embedded resource in a Windows Forms application,Please traverse these steps.
- add the image file to the project with Project Add Existing Item.
- 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.
- 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