Bitmap Functions

The Graphics Device Interface (GDI) provides a set of functions for working with bitmaps. Bitmaps are pixel-based images that can be manipulated and displayed using GDI functions.

Overview

Bitmaps are handled using device-dependent bitmap (DDB) objects or device-independent bitmap (DIB) sections. DDBs are GDI objects that are stored in system memory and are tightly coupled to the display device. DIB sections are memory blocks that can be shared between GDI and other applications, offering more flexibility.

Creating and Managing Bitmaps

  • CreateCompatibleBitmap: Creates a bitmap that is compatible with the specified device.
  • CreateDiscardableBitmap: Creates a discardable bitmap compatible with the specified device.
  • CreateDIBitmap: Creates a DIB section and a bitmap that uses the DIB section.
  • CreateDIBSection: Creates a DIB section.
  • DeleteObject: Deletes a logical pen, brush, or font, or a bitmap.
  • GetObject: Retrieves information about the specified graphics object.
  • GetObjectAPI: Retrieves information about the specified graphics object. (Note: This is likely an internal alias or older name, standard is GetObject).

Manipulating Bitmap Data

  • GetPixel: Retrieves the RGB color value of the pixel at the specified coordinates.
  • SetPixel: Sets the pixel at the specified coordinates to the specified RGB color.
  • GetBitmapBits: Retrieves the bit pattern of a bitmap.
  • SetBitmapBits: Sets the dimensions and bit pattern of a bitmap.
  • GetDIBits: Retrieves the bits of a bitmap and a palette that GDI can use to translate the bitmap colors into a format usable on a specific output device.
  • SetDIBits: Renders a bitmap into the specified device context (DC) using the color information from the specified array of RGB values.
  • SetDIBitsToDevice: Renders a bitmap directly to a device.

Drawing and Copying Bitmaps

  • BitBlt: Performs a bit-block transfer of the color data for a rectangle of pixels from one device context to another.
  • StretchBlt: Copies a bitmap from the source rectangle into the destination rectangle, stretching or compressing the bitmap as necessary.
  • PatBlt: Creates a bit pattern on the specified device context.
  • DrawFrameControl: Draws a window frame or a control within a window frame.

Bitmap Information

  • GetBitmapDimensionEx: Retrieves the width and height (in millimeters) of a bitmap.
  • GetSystemMetrics: Retrieves various system metrics (useful for bitmap-related dimensions and flags).

Example: Creating and Drawing a Bitmap

This example demonstrates creating a simple bitmap and drawing it to a device context.


HDC hdcScreen = GetDC(NULL); // Get screen DC
HDC hdcMem = CreateCompatibleDC(hdcScreen); // Create memory DC

// Create a 100x50 monochrome bitmap
HBITMAP hBitmap = CreateCompatibleBitmap(hdcScreen, 100, 50);

// Select the bitmap into the memory DC
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hdcMem, hBitmap);

// Fill the bitmap with a color (e.g., white)
RECT rect = {0, 0, 100, 50};
HBRUSH hBrush = (HBRUSH)GetStockObject(WHITE_BRUSH);
FillRect(hdcMem, &rect, hBrush);

// Draw something on the bitmap (e.g., a red rectangle)
HBRUSH hRedBrush = CreateSolidBrush(RGB(255, 0, 0));
RECT redRect = {10, 10, 90, 40};
FrameRect(hdcMem, &redRect, hRedBrush);
DeleteObject(hRedBrush);

// Copy the bitmap from memory DC to screen DC
BitBlt(hdcScreen, 50, 50, 100, 50, hdcMem, 0, 0, SRCCOPY);

// Clean up
SelectObject(hdcMem, hOldBitmap); // Restore original bitmap
DeleteObject(hBitmap);
DeleteDC(hdcMem);
ReleaseDC(NULL, hdcScreen);
                

Note on Device Contexts (DCs)

Device contexts are essential for most GDI operations. They represent a drawing surface and hold information about drawing attributes. You typically obtain a DC for the screen, a window, or a printer, and then create compatible DCs for off-screen drawing operations.

Tip for Performance

For complex image manipulation or frequent drawing, using DIB sections with functions like GetDIBits and SetDIBits can be more efficient than constant manipulation of DDBs, especially when sharing data between different parts of your application or with other processes.