GDI+ Resources: Bitmaps





A bitmap is a graphic object used for displaying pictures on windows. It is the primary type of graphic used for various occasions. For example, a bitmap can be used as a background for a window. That is the case for the Pinball game that ships with some versions of Microsoft Windows:

An example of a bitmap

A bitmap can also be used for an aesthetic purpose to decorate a dialog box. Thatís how it is used on some of the installation wizard boxes such as the graphic on the left section of the WordPerfect 2002 installer:


Probably the most regular use of bitmaps is as small graphics on toolbars:


Creating a Bitmap

To create a bitmap, you can use any graphics application, including the Paint program that is installed with Microsoft Windows. In Visual Studio .NET, to create a bitmap, on the main menu, you would click Project -> Add New Item... In the Add New Item dialog box, click Bitmap File, accept the suggested name of the file or specify your own, and click Open:

Add New Item

A new file with an extension of .bmp would be added to your project. You can then design it as you see fit. Here is an example:

Bitmap Design: Food Basket


Practical Learning Practical Learning: Creating a Bitmap

  1. Start a new Windows Application named Resources1
  2. Right-click the following picture and click Copy
  3. To start Microsoft Paint, on the Taskbar, click Start -> (All) Programs -> Accessories -> Paint
  4. On the main menu of Paint, click Edit -> Paste
  5. To change the orientation of the butterfly, on the main menu of Paint, click Image -> Flip/Rotate...
  6. In the Flip and Rotate dialog box, click the Flip Horizontal radio button and click OK
  7. To save the picture, on the main menu of Paint, click File -> Save
  8. Locate the .\Resources1\bin\Debug folder of the current project and display it in the Save In combo box
  9. Save the file as Butterfly.bmp and return to Visual Studio

Using a Bitmap

To support bitmaps, the .NET Framework provides the Bitmap class. The Bitmap class is based on the abstract Image class. If you have created a bitmap and stored it as a file, you can pass the path of that file to the following constructor of the class:

public Bitmap(string filename);

(If you create a picture using the Add New Item dialog box, Visual C++ and Visual C# have different ideas when saving the file. For this reason, in Visual C#, you may have to either manually move the file or provide the complete path to the file). Once the picture is ready, to present it to the user, you can call the Graphics.DrawImage() method that is overloaded with as many different versions as you can possibly need. One of the versions of this method has the following syntax:

public void DrawImage(Image img, Point pt);

The first argument can be a bitmap that you can have previously initialized. The second argument specifies the location where the picture will be drawn, which will be the top-left corner of the picture with regards to its parent.

Here is an example:



private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
	Bitmap bmpFood = new Bitmap("C:\\Programs\\MSVCS\\WindowsApplication1\\FoodBasket.bmp");
	Graphics graph = this.CreateGraphics();

	graph.DrawImage(bmpFood, 0, 0);

Practical Learning Practical Learning: Displaying a Bitmap

  1. Click an empty area of the form to make sure it is selected. In the Properties window, click the Events button Events
  2. To display the picture, double-click the Paint field and implement it as follows:
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    	Bitmap Butterfly = new Bitmap("Butterfly.bmp");
    	e.Graphics.DrawImage(Butterfly, 10, 10);
  3. Execute the application to test it:
  4. Close the form and return to your programming environment

Home Copyright © 2004-2009 FunctionX, Inc.