How to: Retrieve an Image that is an Embedded Resource

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

Procedure

To set up this example

  1. Create a Windows Form Application with a PictureBox control named pictureBox1.

  2. Add the following code example to the Form1_Load event handler.

    System.Reflection.Assembly thisExe;
    thisExe = System.Reflection.Assembly.GetExecutingAssembly();
    System.IO.Stream file = 
        thisExe.GetManifestResourceStream("AssemblyName.ImageFile.jpg");
    this.pictureBox1.Image = Image.FromStream(file);
    
  3. Add an existing image file to the project, and set its Build Action property to Embedded Resource in Solution Explorer.

  4. Replace "AssemblyName.ImageFile.jpg" with the name of the resource in the assembly.

  5. Use the GetManifestResourceNames method of the Assembly object to find the resource name.

Robust Programming

The following conditions may cause an exception:

  • The embedded resource is not in the assembly, and the call to GetManifestResourceStream returns Nothing.

  • There may not be an application associated with the file type, in other words, the file name extension.

See Also

Concepts

Designing a User Interface in Visual C#

Other Resources

Creating and Using Bitmaps and Icons

Visual C# Guided Tour