获取最小化窗口的RGB数据

时间:2013-09-23 21:52:21

标签: c++ windows bitmap bitblt getdibits

我想获取运行窗口特定部分的RGB数据,这是最小化的,我不太清楚如何做到这一点。这是我获取指定矩形窗口的位图信息的代码。

BYTE* LoadBMPFromHandle(HWND hwnd, int leftTopX, int leftTopY, int width, int height)
{
BITMAPINFO bmi;
BITMAPINFOHEADER bmpiHeader;
HDC dc, memdc;
HBITMAP hbWnd;

bmpiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpiHeader.biPlanes = 1;
bmpiHeader.biBitCount = 24;
bmpiHeader.biCompression = BI_RGB;
bmpiHeader.biSizeImage = 0;
bmpiHeader.biXPelsPerMeter = 3780;
bmpiHeader.biYPelsPerMeter = 3780;
bmpiHeader.biClrUsed = 0;
bmpiHeader.biClrImportant = 0;
bmpiHeader.biWidth = width;
bmpiHeader.biHeight = height;
bmi.bmiHeader = bmpiHeader;

dc = GetDC(hwnd);
memdc = CreateCompatibleDC(dc);
hbWnd = CreateCompatibleBitmap(dc, width, height);
SelectObject(memdc, hbWnd);
BitBlt(memdc, 0, 0, width, height, dc, leftTopX, leftTopY, SRCCOPY);

int paddedWidth = width + (4-(width%4))%4;
BYTE* BGRwnd = new BYTE [paddedWidth*height*3];

if( 0 == GetDIBits(dc, hbWnd, 0, height, BGRwnd, &bmi, DIB_RGB_COLORS) )
{
    cout <<"GetDIBits function fail..." << endl;
    DeleteDC(memdc);
    DeleteObject(hbWnd);
    ReleaseDC(hwnd, dc);
    exit(0);
}

DeleteDC(memdc);
DeleteObject(hbWnd);
ReleaseDC(hwnd, dc);

return BGRwnd;
}

但是,只有在没有其他窗口与我有句柄的窗口重叠时,此功能才有效。

hwnd - 处理我的窗口 LoadBMPFromHandle必须检索具有左上坐标(leftTopX,leftTopY)和dimesions width x height的矩形的BGR(蓝绿红)数据。

谢谢!

0 个答案:

没有答案