Reputation: 523
I want to read and write pixels directly from/to a drawing context, i.e. during a paint operation on a window.
I understand that there are GetPixel
/SetPixel
functions in Windows GDI, but for big operations it would be far better to be able to read and write pixel data directly to memory.
How can I do this using standard GDI?
Upvotes: 6
Views: 1941
Reputation: 210755
Use GetObject
to get the BITMAP
struct which contains a pointer to the bitmap's data.
Upvotes: 2
Reputation: 3112
You could create a compatible DC, Bitmap:
HDC hMemDC = CreateCompatibleDC(hdc);
HBITMAP hBmp = CreateCompatibleBitmap(hdc, WIDTH, HEIGHT);
SelectObject(hMemDC, hBmp);
Next, there is GetDIBits function you can use to get 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 might need to set lpvBits to NULL to get the dimensions and format of the image via BITMAPINFO (lpbi parameter).
Upvotes: 4