Export (0) Print
Expand All
Expand Minimize
17 out of 33 rated this helpful - Rate this topic

Code: Retrieving an Image that is an Embedded Resource (Visual C#)

Visual Studio .NET 2003

This example retrieves an image that is an embedded resource of the assembly.

Example

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.

Replace "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.

Robust Programming

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).

See Also

Windows Forms Example Topics | Code: Finding the Names of Resources in an Assembly | Code: Running a Program | Process Class

Show:
© 2014 Microsoft. All rights reserved.