H o m e
C r e a t e    a n d    U s e    a n    E m b e d d e d    B i t m a p    R e s o u r c e

 

By Michael McIntyre

mikemc@getdotnetcode.com

 

“Nearly every production-quality application needs to use resources. A resource is any non-executable data that is logically deployed with an application. A resource might be displayed in an application as error messages or as part of the user interface. Resources can contain data in a number of forms, including strings, images, and persisted objects. Storing your data in a resource file allows you to change the data without recompiling your entire application.” – from Microsoft .NET Help

 

This article explains how to create and use a Bitmap as an Image resource in an application.  It explains: 1) how to create a Bitmap with the Visual Studio.NET Bitmap Editor; 2) how to embed a Bitmap in the application’s manifest file; and 3) how to retrieve and use the embedded Bitmap.

 

Before you begin make sure Visual Studio.NET’s Image Editor Toolbar is visible.  Choose View Menu -> Toolbars to make sure the Image Editor Toolbar is checked.

 

Create a Bitmap with Visual Studio.Net

 

1. Start a new Windows Forms project named Newbie.

2. From the File menu choose Add New Item. The Add New Item dialog window will open.

3. In the Templates pane of the Add New Item dialog find the Bitmap item and click it once.

4. In the Name TextBox change the name of the Bitmap to myBitmap.

5. Click the Open button.

 

A Bitmap will be added to your project and Visual Studio.NET’s Image Editor will open.

 

The Image Editor works much like the Windows Paint application.  For more instructions click here.

 

For this demonstration draw a large red cirlce on the image. Close and save the image.

 

Embed the Bitmap in an Application

 

1. In the Project Explorer pane of your project right-click the myBitmap file and choose Properties.

2. In the Property Grid set the myBitmap files Build Action property to Embedded Resource.

 

Use the Embedded Bitmap in Code

 

The Assembly.GetManifestResourceStream method is used to retrieve an embedded resource so it can be used in code.

 

Add a button name GetResourceButton to Form1 of your project.  Add a button click handler for the button.  Add the code below to the button’s click handler.

 

' Declare a variable of type Stream named aStream.

Dim aStream As System.IO.Stream

' Call the Assembly GetManifestResourceStream method passing it

' the namespace of this project and the name of the Bitmap resource.

' Assign the Stream object returned to the aStream variable.

aStream = Me.GetType().Assembly.GetManifestResourceStream("Newbie.myBitmap.bmp")

' Declare a variable of type Bitmap named aBitmap.

' Call the Bitmap New constructor passing in the aStream.

' Assign the address (reference) of the new Bitmap object

' to the aBitmap variable.

Dim aBitmap As New Bitmap(aStream)

' Set this form's BackGround image property to aBitmap.

Me.BackgroundImage = aBitmap

 

Run the project.  Click the GetResourceButton.  The Bitmap you created and embedded should be retrieved by the button handler code and your form’s background should now be covered copies of your Bitmap.

 

NOTE:  Instead of creating/embedding a new Bitmap with Visual Studio.NET you can add a pre-existing Bitmap file to your project and set its Build Action property to Embedded Resource.

 

Copyright © 2001-2003 aZ Software Developers. All rights reserved.