11.2.1 Creating and Loading a Bitmap File

You can create a bitmap by using Image Editor. Using this application, you specify the dimensions of the bitmap, and then fill it in by painting in the blank area with such tools as a brush, spray can, and even text. Any of these tools can produce images using colors from a palette of up to 28 colors, which you can define.

To create and load a bitmap using this method, follow these steps:

1.Start Image Editor and create the bitmap by following the directions given in Microsoft Windows Programming Tools.

2.After creating the bitmap image, save it in a file that has the filename extension .BMP.

3.In your application's resource-definition (.RC) file, add a BITMAP statement that defines that bitmap as an application resource. For example, the following statement specifies that a bitmap resource is in the file DOG.BMP:

IDDOGBMP BITMAP dog.bmp

The name IDDOGBMP is a resource identifier; the filename DOG.BMP specifies the file that contains the bitmap.

4.In your application's source file, load the bitmap by using the LoadBitmap function. This function takes the bitmap's resource name, loads the bitmap into memory, and returns a handle of the bitmap. For example, the following statement loads the bitmap resource named Dog and stores the resulting bitmap handle in the variable hDogBitmap:

hDogBitmap = LoadBitmap(hinst, MAKEINTRESOURCE(IDDOGBMP));

5.Select the bitmap into a device context by using the SelectObject function. For example, the following statement loads the bitmap specified by hDogBitmap into the device context specified by hdcMemory:

SelectObject(hdcMemory, hDogBitmap);

6.Display the bitmap by using the BitBlt function. For example, the following statement displays a copy of the bitmap in the memory device context hdcMemory on the device identified by hDC:

BitBlt(hDC, 10, 10, 100, 150, hdcMemory, 0, 0, SRCCOPY)

This example displays the bitmap beginning at the coordinates (10,10) of the destination device context. The bitmap is 100 units wide and 150 units high. The bitmap is taken from the memory device context beginning at the coordinates (0,0). The SRCCOPY value specifies that Windows should copy the source bitmap to the destination.