I have the following code to take a screenshot from a window and get the color of a specific pixel in it:
void ProcessScreenshot(HWND hwnd){
HDC WinDC;
HDC CopyDC;
HBITMAP hBitmap;
RECT rt;
GetClientRect (hwnd, &rt);
WinDC = GetDC (hwnd);
CopyDC = CreateCompatibleDC (WinDC);
hBitmap = CreateCompatibleBitmap (WinDC,
rt.right - rt.left,
rt.bottom - rt.top);
SelectObject (CopyDC, hBitmap);
BitBlt (CopyDC,
0,0,
rt.right - rt.left,
rt.bottom - rt.top,
WinDC,
0, 0,
SRCCOPY);
COLORREF col = ::GetPixel(CopyDC,145,293);
delete hBitmap;
ReleaseDC(hwnd, WinDC);
ReleaseDC(hwnd, CopyDC);
}
string 'delete hBitmap;' raises a runtime error: access violation. Think I can't just delete it like that?
Since bitmaps take up a lot of space, if I don't get rid of it, I get a huge memory leak. My question is: Does DC exempt HBITMAP from this, or does it stick even after I released DC? If so, how to get rid of HBITMAP?
source
share