【发布时间】:2011-01-29 21:54:21
【问题描述】:
我有以下代码来截取一个窗口,并获取其中特定像素的颜色:
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);
}
'删除 hBitmap;' 行导致运行时错误:访问冲突。我想我不能就这样删除它吗?
因为位图占用大量空间,如果我不摆脱它,最终会导致巨大的内存泄漏。我的问题是:发布 HBITMAP 的 DC 是否可以解决这个问题,或者即使在我发布了 DC 之后它仍然存在?如果是后者,我该如何正确摆脱HBITMAP?
【问题讨论】:
标签: c++ windows winapi graphics gdi+