Thursday, December 20, 2012

How to retrieve an image that is an embedded resource of the assembly?


The image can be embedded resource of the assembly. This is useful in case you are developing any custom library or windows application etc. You need to first add the image in the project under any subfolder. Once it is added to the project, go to its properties by pressing F4 on image. Change the Build Action property to Embedded Resource.

In order to use the image m you need to use the following code:

try
           {
                      Assembly myAssembly = Assembly.GetExecutingAssembly();
            using (Stream imgStream = myAssembly.GetManifestResourceStream   
                        ("Namespace.Subfolder.Image.jpg"))
                {
                    Image img = Image.FromStream(imgStream);
                    picBoxLogo.Image = img;
                }
                         } catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

No comments:

Post a Comment