CreateBitmap

2.x

  HBITMAP CreateBitmap(nWidth, nHeight, cbPlanes, cbBits, lpvBits)    
  int nWidth; /* bitmap width, */  
  int nHeight; /* bitmap height, */  
  UINT cbPlanes; /* number of color planes */
  UINT cbBits; /* number of bits per pixel */
  const void FAR* lpvBits; /* address of array with bitmap bits */

The CreateBitmap function creates a device-dependent memory bitmap that has the specified width, height, and bit pattern.

Parameters

nWidth

Specifies the width, in pixels, of the bitmap.

nHeight

Specifies the height, in pixels, of the bitmap.

cbPlanes

Specifies the number of color planes in the bitmap. The number of bits per plane is the product of the plane's width, height, and bits per pixel (nWidth × nHeight × cbBits).

cbBits

Specifies the number of color bits per display pixel.

lpvBits

Points to an array of short integers that contains the initial bitmap bit values. If this parameter is NULL, the new bitmap is left uninitialized. For more information about these bit values, see the description of the bmBits member of the BITMAP structure in the Microsoft Windows Programmer's Reference, Volume 3.

Return Value

The return value is the handle of the bitmap if the function is successful. Otherwise, it is NULL.

Comments

The bitmap created by the CreateBitmap function can be selected as the current bitmap for a memory device context by using the SelectObject function.

For a color bitmap, either the cbPlanes or cbBits parameter should be set to 1. If both of these parameters are set to 1, CreateBitmap creates a monochrome bitmap.

Although a bitmap cannot be copied directly to a display device, the BitBlt function can copy it from a memory device context (in which it is the current bitmap) to any compatible device context, including a screen device context.

When it has finished using a bitmap created by CreateBitmap, an application should select the bitmap out of the device context and then remove the bitmap by using the DeleteObject function.

Example

The following example uses the CreateBitmap function to create a bitmap with a zigzag pattern and then uses the PatBlt function to fill the client area with that pattern:

HDC hdc;
HBITMAP hbmp;
HBRUSH hbr, hbrPrevious;
RECT rc;

int aZigzag[] = { 0xFF, 0xF7, 0xEB, 0xDD, 0xBE, 0x7F, 0xFF, 0xFF };

hbmp = CreateBitmap(8, 8, 1, 1, aZigzag);
hbr = CreatePatternBrush(hbmp);

hdc = GetDC(hwnd);
UnrealizeObject(hbr);
hbrPrevious = SelectObject(hdc, hbr);
GetClientRect(hwnd, &rc);

PatBlt(hdc, rc.left, rc.top,
    rc.right - rc.left, rc.bottom - rc.top, PATCOPY);
SelectObject(hdc, hbrPrevious);
ReleaseDC(hwnd, hdc);

DeleteObject(hbr);
DeleteObject(hbmp);

See Also

BitBlt, CreateBitmapIndirect, CreateCompatibleBitmap, CreateDIBitmap, CreateDiscardableBitmap, DeleteObject, SelectObject