我试图通过GetDIBits
从兼容位图(它完全由case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
HBRUSH hb = CreateSolidBrush(RGB(0, 0, 255));
HDC hdcc = CreateCompatibleDC(hdc);
HBITMAP bm = CreateCompatibleBitmap(hdc, r.right, r.bottom);
SelectObject(hdcc, bm);
SelectObject(hdcc, hb);
Rectangle(hdcc, 0, 0, r.right, r.bottom); //filling by the blue brush
BITMAPINFO bi = { 0 };
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
int er = GetDIBits(hdcc, bm, 0, 0, NULL, &bi, DIB_RGB_COLORS);
//In GetDIBits, as HDC argument must be compatible, yes?
if (!er)
{
cout << "ERROR HERE:"<< GetLastError()<<"ENDS";
}
COLORREF *buf = new COLORREF(bi.bmiHeader.biSizeImage); //Yet, still, I have not understood, which type array should be - char, BYTE, COLORREF or anything else
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biHeight = abs(bi.bmiHeader.biHeight);
GetDIBits(hdcc, bm, 0, bi.bmiHeader.biHeight, buf, &bi, DIB_RGB_COLORS);
for (int i(0); i < 100; i++)
{
cout << (int)GetRValue(buf[i]) << ",";
cout << (int)GetGValue(buf[i]) << ",";
cout << (int)GetBValue(buf[i]) << ",";
cout << endl;
}
SetDIBits(hdcc, bm, 0, bi.bmiHeader.biHeight, buf, &bi, DIB_RGB_COLORS);
delete []buf;
BitBlt(hdc, 0, 0, r.right, r.bottom, hdcc, 0, 0, SRCCOPY);
DeleteObject(hb);
DeleteDC(hdcc);
DeleteObject(bm);
EndPaint(hwnd, &ps);
}
break;
颜色填充)中获取像素数组,但它会返回另一种颜色。并且,当我尝试更改数组时,它实际上返回一个异常。有什么问题?
.toString()
答案 0 :(得分:1)
这一行有几个问题:
COLORREF *buf = new COLORREF(bi.bmiHeader.biSizeImage);
biSizeImage
以字节为单位,而不是COLORREF
s,因此会过度分配。biSizeImage
可能为零,在这种情况下,您不会分配任何内容。当biSizeImage
为零时,表示您必须计算所需的实际大小。biSizeImage
是设备相关(&#34;兼容&#34;)位图的大小,这可能与设备所需的大小不同 - 您试图抓住的独立数据。