Code: Retrieving an Image that is an Embedded Resource (Visual C#)
This example retrieves an image that is an embedded resource of the assembly.
System.Reflection.Assembly thisExe; thisExe = System.Reflection.Assembly.GetExecutingAssembly(); System.IO.Stream file = thisExe.GetManifestResourceStream("AssemblyName.ImageFile.jpg"); this.pictureBox1.Image = Image.FromStream(file);
Compiling the Code
This example requires:
- A Windows Form with a PictureBox control named pictureBox1.
Add the image file to the project, and set the Build Action property to Embedded Resource in Solution Explorer.
"AssemblyName.ImageFile.jpg" with the name of the resource, as it is known in the assembly. Use the GetManifestResourceNames method of the Assembly object to find the resource name. See Code: Finding the Names of Resources in an Assembly.
The following conditions may cause an exception:
- The embedded resource does not exist in the assembly, and the call to GetManifestResourceStream returns Nothing.
- There may not be an application associated with the file type (the file extension).