You are currently offline, waiting for your internet to reconnect

HOW TO: Load a Bitmap or Image from a Resource by Using Visual Basic .NET

Support for Windows XP has ended

Microsoft ended support for Windows XP on April 8, 2014. This change has affected your software updates and security options. Learn what this means for you and how to stay protected.

This article was previously published under Q324566
SUMMARY
This step-by-step article describes how to embed metafile and raster images like .png and .jpg files as resources in a Microsoft Visual Basic .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 Basic Projects, click Windows Application.
  4. Type an appropriate name (WindowsApplication1), and then click OK.
back to the top

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.
back to the top

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 file name 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.Dim s As Stream = Me.GetType().Assembly.GetManifestResourceStream("WindowsApplication1.filename.jpg")Dim bmp As Bitmap = New Bitmap(s)s.Close()Dim g As Graphics = 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 that you added.
back to the top

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 file name 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. Dim s As Stream = Me.GetType().Assembly.GetManifestResourceStream("WindowsApplication1.filename.emf")Dim emf As Metafile = New Metafile(s)s.Close()Dim g As Graphics = 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.
back to the top

Finish, Build, and Run the Application

  1. At the top of the source file, add the following lines of code:
    Imports System.IOImports 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.
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 that is 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
Properties

Article ID: 324566 - Last Review: 05/08/2006 23:39:00 - Revision: 2.3

  • Microsoft .NET Framework Software Development Kit 1.0 Service Pack 2
  • Microsoft Windows XP Professional
  • the operating system: Microsoft Windows XP 64-Bit Edition
  • kbhowtomaster kbgdipimaging kbdswgdi2003swept KB324566
Feedback
ata-bi-slot="2"> Privacy & cookies
  • Trademarks
  • © 2015 Microsoft