HOW TO: Load a Bitmap or Image from a Resource by Using Visual C#

Article translations Article translations
Article ID: 324567 - View products that this article applies to.
This article was previously published under Q324567
Expand all | Collapse all

On This Page

SUMMARY

This step-by-step article describes how to embed both metafile and raster images like .png and .jpg files as resources in a Visual C# .NET application, and how to extract them for use with System.Drawing classes.

Create a Project

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, click New, and then click Project.
  3. Under Visual C# Projects, click Windows Application.
  4. Type an appropriate name (WindowsApplication1), and then click OK.

Add a Resource

  1. On the View menu, click Solution Explorer, and then select the new project that you created earlier.
  2. On the Project menu, click Add Existing Item.
  3. Locate and open one raster file (.jpg, .png, or .tif) and one metafile file (.wmf or .emf), and then follow these steps for each file:
    1. In Solution Explorer, select the file, and then on the View menu, click Properties Window.
    2. Set the Build Action to Embedded Resource.
    3. On the Project menu, click Add Existing Item.

Create a Button to Display the Raster Image

  1. On the View menu, click Toolbox, and then add a button to the form. By default, this is Button1.
  2. Repeat step 1 to create Button2.
  3. Double-click Button1 to open the Code window, and then add the following code to the button1_Click() function:
    // Replace "filename" below with the actual filename for the JPG
    //   file you added as a resource; the name is case-sensitive.
    // Also make sure that "WindowsApplication1" is replaced with the
    //   name of your project, if different.
    Stream s = this.GetType().Assembly.GetManifestResourceStream("WindowsApplication1.filename.jpg");
    Bitmap bmp = new Bitmap( s );
    s.Close();
    Graphics g = CreateGraphics();
    g.DrawImage( bmp, 0, 0 );
    bmp.Dispose();
    g.Dispose();
    					
  4. Replace "WindowsApplication1" in the GetManifestResourceStream() call with the name of your project.
  5. Replace "filename.jpg" in the GetManifestResourceStream() call with the case-sensitive name of the file you added.

Create a Button to Display the Metafile Image

  1. On the View menu, click Designer.
  2. Double-click Button2 to open the Code window, and then add the following code to the button2_Click() function:
    // Replace "filename" below with the actual filename for the metafile
    //   file you added as a resource; the name is case-sensitive.
    // Also make sure you replace "WindowsApplication1" with the
    //   name of your project, if different.
    Stream s = this.GetType().Assembly.GetManifestResourceStream("WindowsApplication1.filename.emf");
    Metafile emf = new Metafile( s );
    s.Close();
    Graphics g = CreateGraphics();
    g.DrawImage( emf, 0, 0, 300, 300 );
    emf.Dispose();
    g.Dispose();
    					
  3. Replace "WindowsApplication1" in the GetManifestResourceStream() call with the name of your project.
  4. Replace "filename.emf" in the GetManifestResourceStream() call with the case-sensitive name of the metafile that you added.

Finish, Build, and Run the Application

  1. At the top of the source file, add the following lines of code:
    using System.IO;
    using System.Drawing.Imaging;
    					
  2. On the Build menu, click Build Solution.
  3. On the Debug menu, click Start.
  4. To display your raster image, click Button1. To display your metafile, click Button2.

Troubleshooting

Exceptions may occur if any of the following conditions are true:
  • The "WindowsApplication1" string does not reflect the actual name of your project.
  • The "filename.jpg" string mentioned in the "Create a Button to Display the Raster Image" section does not reflect the actual name of the file you added as a resource.
  • There are case mismatches between the argument text for GetManifestResourceStream() and the actual names of the entities that comprise the string.

REFERENCES

For more information, see the Visual Studio .NET Help documentation about the GetManifestResourceStream() function.

Properties

Article ID: 324567 - Last Review: May 8, 2006 - Revision: 2.3
APPLIES TO
  • Microsoft .NET Framework Software Development Kit 1.0 Service Pack 2
  • Microsoft Windows XP Professional
  • the operating system: Microsoft Windows XP 64-Bit Edition
Keywords: 
kbhowtomaster kbgdipimaging kbdswgdi2003swept KB324567

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com