Oli*_*ver 3 c++ windows graphics winapi gdi+
我有以下代码来截取窗口的截图,并获取其中特定像素的颜色:
void ProcessScreenshot(HWND hwnd){
HDC WinDC;
HDC CopyDC;
HBITMAP hBitmap;
RECT rt;
GetClientRect (hwnd, &rt);
WinDC = GetDC (hwnd);
CopyDC = CreateCompatibleDC (WinDC);
//Create a bitmap compatible with the DC
hBitmap = CreateCompatibleBitmap (WinDC,
rt.right - rt.left, //width
rt.bottom - rt.top);//height
SelectObject (CopyDC, hBitmap);
BitBlt (CopyDC, //destination
0,0,
rt.right - rt.left, //width
rt.bottom - rt.top, //height
WinDC, //source
0, 0,
SRCCOPY);
COLORREF col = ::GetPixel(CopyDC,145,293);
// Do some stuff with the pixel colour....
delete hBitmap;
ReleaseDC(hwnd, WinDC);
ReleaseDC(hwnd, CopyDC);
}
Run Code Online (Sandbox Code Playgroud)
行'删除hBitmap;' 导致运行时错误:访问冲突.我想我不能像那样删除它?
因为位图占用了大量空间,如果我没有摆脱它,我将最终导致巨大的内存泄漏.我的问题是:释放DC的HBITMAP是否处理这个问题,或者即使在我发布DC之后它仍然存在?如果是后一种情况,我该如何正确摆脱HBITMAP?