Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

This function creates a bitmap compatible with the device associated with the specified device context.

Syntax

HBITMAP CreateCompatibleBitmap(
  HDC 
hdc, 
  int 
nWidth, 
  int 
nHeight
); 

Parameters

hdc

[in] Handle to a device context.

nWidth

[in] Specifies the bitmap width, in pixels.

nHeight

[in] Specifies the bitmap height, in pixels.

Return Value

A handle to the bitmap indicates success.

NULL indicates failure.

To get extended error information, call GetLastError.

Code Example

The following code example shows how to create a memory device context, and how to use a CreateCompatibleBitmapto create a bitmap.

Copy Code
  HDC hDC, 			// Handle to the display device context

	hDCMem; 		 // Handle to the memory device context
  HBITMAP hBitmap; 	// Handle to the new bitmap
  int iWidth, iHeight; // Width and height of the bitmap
  // Retrieve the handle to a display device context for the client

  // area of the window. 
  hDC = GetDC (hwnd);
  // Create a memory device context compatible with the device. 
  hDCMem = CreateCompatibleDC (hDC);
  // Retrieve the width and height of window display elements.
  iWidth = GetSystemMetrics (SM_CXSCREEN) / 10;
  iHeight = GetSystemMetrics (SM_CYSCREEN) / 10;
  // Create a bitmap compatible with the device associated with the

  // device context.
  hBitmap = CreateCompatibleBitmap (hDC, iWidth, iHeight);
// Insert code to use the bitmap.
  // Delete the bitmap object and free all resources associated
with it. 
  DeleteObject (hBitmap);
  // Delete the memory device context and the display device
context.
  DeleteDC (hDCMem);
  ReleaseDC (hDC);

Remarks

The color format of the bitmap created by the CreateCompatibleBitmapfunction matches the color format of the device identified by the hdcparameter. This bitmap can be selected into any memory device context that is compatible with the original device.

Because memory device contexts allow both color and monochrome bitmaps, the format of the bitmap returned by the CreateCompatibleBitmapfunction differs when the specified device context is a memory device context. However, a compatible bitmap that was created for a nonmemory device context always possesses the same color format and uses the same color palette as the specified device context.

If an application sets the nWidthor nHeightparameters to zero, CreateCompatibleBitmapreturns the handle to a 1- by 1-pixel, monochrome bitmap.

If a DIB section, which is a bitmap created by the CreateDIBSectionfunction, is selected into the device context identified by the hdcparameter, CreateCompatibleBitmapcreates a DIB section.

When you no longer need the bitmap, call the DeleteObjectfunction to delete it.

Requirements

Header windows.h
Library coredll.lib
Windows Embedded CE Windows CE 1.0 and later
Windows Mobile Windows Mobile Version 5.0 and later

See Also