HOW TO: Load a Bitmap or Image from a Resource by Using Visual C# (324567)
The information in this article applies to:
- Microsoft .NET Framework SDK 1.0
- Microsoft Windows XP Professional
- the operating system: Microsoft Windows XP 64-Bit Edition
This article was previously published under Q324567 For a Microsoft Visual Basic .NET version of this article, see 324566.
IN THIS TASKSUMMARY
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- Start Microsoft Visual Studio .NET.
- On the File menu, click New, and then click Project.
- Under Visual C# Projects, click Windows Application.
- 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.
back to the top
Create a Button to Display the Raster Image- On the View menu, click Toolbox, and then add a button to the form. By default, this is Button1.
- Repeat step 1 to create Button2.
- 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();
- Replace "WindowsApplication1" in the GetManifestResourceStream() call with the name of your project.
- Replace "filename.jpg" in the GetManifestResourceStream() call with the case-sensitive name of the file you added.
back to the top
Create a Button to Display the Metafile Image- On the View menu, click Designer.
- 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();
- Replace "WindowsApplication1" in the GetManifestResourceStream() call with the name of your project.
- Replace "filename.emf" in the GetManifestResourceStream() call with the case-sensitive name of the metafile that you added.
back to the top
Finish, Build, and Run the Application- At the top of the source file, add the following lines of code:
using System.IO;
using System.Drawing.Imaging;
- On the Build menu, click Build Solution.
- On the Debug menu, click Start.
- To display your raster image, click Button1. To display your metafile, click Button2.
back to the top
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.
back to the top
REFERENCES
For more information, see the Visual Studio .NET Help documentation about the GetManifestResourceStream() function.
back to the top
Modification Type: | Minor | Last Reviewed: | 5/8/2006 |
---|
Keywords: | kbDSWGDI2003Swept kbgdipimaging kbHOWTOmaster KB324567 kbAudDeveloper |
---|
|