BitBlt function
The BitBlt function performs a bit‑block transfer of the color data corresponding to a rectangle of pixels from one device context to another.
Syntax
BOOL BitBlt(
HDC hdcDest,
int nXDest,
int nYDest,
int nWidth,
int nHeight,
HDC hdcSrc,
int nXSrc,
int nYSrc,
DWORD dwRop
);
Parameters
| Parameter | Description |
|---|---|
hdcDest | Handle to the destination device context. |
nXDest | X‑coordinate of the upper‑left corner of the destination rectangle. |
nYDest | Y‑coordinate of the upper‑left corner of the destination rectangle. |
nWidth | Width of the source and destination rectangles, in logical units. |
nHeight | Height of the source and destination rectangles, in logical units. |
hdcSrc | Handle to the source device context. |
nXSrc | X‑coordinate of the upper‑left corner of the source rectangle. |
nYSrc | Y‑coordinate of the upper‑left corner of the source rectangle. |
dwRop | Raster‑operation code. See Raster‑Operation Codes. |
Return value
If the function succeeds, the return value is non‑zero. If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
- The source and destination device contexts must be compatible.
- When copying between a display device context and a memory device context, the
dwRopparameter can be used to control how the source pixels are combined with the destination pixels. - If the source and destination rectangles overlap, the result is undefined.
Raster‑Operation Codes
Commonly used dwRop values include:
| Code | Definition |
|---|---|
SRCCOPY (0x00CC0020) | Copies the source rectangle directly to the destination rectangle. |
SRCAND (0x008800C6) | Combines the source and destination rectangles using the Boolean AND operator. |
SRCINVERT (0x00660046) | Combines the source and destination rectangles using the Boolean XOR operator. |
SRCPAINT (0x00EE0086) | Combines the source and destination rectangles using the Boolean OR operator. |
Example
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
{
HWND hWnd = GetDesktopWindow();
HDC hDC = GetDC(hWnd);
HDC memDC;
HBITMAP hBmp;
RECT rc;
GetClientRect(hWnd, &rc);
memDC = CreateCompatibleDC(hDC);
hBmp = CreateCompatibleBitmap(hDC, rc.right, rc.bottom);
SelectObject(memDC, hBmp);
// Fill the memory DC with a solid color.
FillRect(memDC, &rc, (HBRUSH)(COLOR_WINDOW+1));
// Copy the memory DC to the screen.
BitBlt(hDC, 0, 0, rc.right, rc.bottom, memDC, 0, 0, SRCCOPY);
// Cleanup.
DeleteObject(hBmp);
DeleteDC(memDC);
ReleaseDC(hWnd, hDC);
return 0;
}
Requirements
- Header:
windows.h - Library:
Gdi32.lib - Supported on: Windows XP and later