Microsoft KB Archive/324567

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

Article ID: 324567

Article Last Modified on 5/8/2006

-

APPLIES TO


 * Microsoft .NET Framework Software Development Kit 1.0 Service Pack 2
 * Microsoft Windows XP Professional
 * Microsoft Windows XP Professional for Itanium-based systems

-



This article was previously published under Q324567



For a Microsoft Visual Basic .NET version of this article, see 324566.

IN THIS TASK
SUMMARY
 * Create a Project
 * Add a Resource
 * Create a Button to Display the Raster Image
 * Create a Button to Display the Metafile Image
 * Finish, Build, and Run the Application
 * Troubleshooting

REFERENCES



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.

back to the top

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.

back to the top

Add a Resource
 On the View menu, click Solution Explorer, and then select the new project that you created earlier. On the Project menu, click Add Existing Item. 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:  In Solution Explorer, select the file, and then on the View menu, click Properties Window. Set the Build Action to Embedded Resource. On the Project menu, click Add Existing Item.</ol> </li></ol>

back to the top

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

back to the top

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

back to the top

Finish, Build, and Run the Application
<ol>  At the top of the source file, add the following lines of code: using System.IO; using System.Drawing.Imaging; </li> On the Build menu, click Build Solution.</li> On the Debug menu, click Start.</li> To display your raster image, click Button1. To display your metafile, click Button2.</li></ol>

back to the top

Troubleshooting
Exceptions may occur if any of the following conditions are true:
 * The &quot;WindowsApplication1&quot; string does not reflect the actual name of your project.
 * The &quot;filename.jpg&quot; string mentioned in the &quot;Create a Button to Display the Raster Image&quot; 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.

back to the top

<div class="references_section">