Accessing direct pixels of a GDI drawing context
I want to read and write pixels directly from / to the drawing context, i.e. during a drawing operation on the window.
I understand there are GetPixel
/ functions in Windows GDI SetPixel
, but for large operations it would be much better to be able to read and write pixel data directly into memory.
How can I do this using standard GDI?
+3
source to share
2 answers
You can create a compatible DC, Bitmap:
HDC hMemDC = CreateCompatibleDC(hdc);
HBITMAP hBmp = CreateCompatibleBitmap(hdc, WIDTH, HEIGHT);
SelectObject(hMemDC, hBmp);
Next, there is the GetDIBits function that you can use to get the bits:
int GetDIBits(
_In_ HDC hdc,
_In_ HBITMAP hbmp,
_In_ UINT uStartScan,
_In_ UINT cScanLines,
_Out_ LPVOID lpvBits,
_Inout_ LPBITMAPINFO lpbi,
_In_ UINT uUsage
);
NOTE. You may need to set lpvBits to NULL to get the dimensions and aspect ratio via BITMAPINFO (lpbi parameter).
+3
source to share