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