【发布时间】:2018-09-07 15:15:00
【问题描述】:
我只是想根据给定像素的 RGB 值扫描内存中的位图以查找特定颜色,如果找到,请给我该像素的 x、y 线。我这里有代码将给定窗口的位图存储在内存中,并且工作正常。但是当我尝试检索红色值为 60 的像素所在的位置时,我得到了各种时髦的值。 这是我当前的代码:
bool findColor(int x, int y, int w, int h, LPCSTR fname) {
HWND window = FindWindow(0, ("windownamehere"));
HDC hdcSource = GetDC(window);
HDC hdcMemory = CreateCompatibleDC(hdcSource);
int capX = GetDeviceCaps(hdcSource, HORZRES);
int capY = GetDeviceCaps(hdcSource, VERTRES);
HBITMAP hBitmap = CreateCompatibleBitmap(hdcSource, w, h);
HBITMAP hBitmapOld = (HBITMAP)SelectObject(hdcMemory, hBitmap);
BitBlt(hdcMemory, 0, 0, w, h, hdcSource, x, y, SRCCOPY);
SelectObject(hdcMemory, hBitmapOld);
//Added
HDC hdc = GetDC(0);
BITMAPINFO MyBMInfo = { 0 };
MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);
GetDIBits(hdcMemory, hBitmap, 0, 0, NULL, &MyBMInfo, DIB_RGB_COLORS);
BYTE* lpPixels = new BYTE[MyBMInfo.bmiHeader.biSizeImage];
MyBMInfo.bmiHeader.biCompression = BI_RGB;
GetDIBits(hdc, hBitmap, 0, MyBMInfo.bmiHeader.biHeight, (LPVOID)lpPixels, &MyBMInfo, DIB_RGB_COLORS);
BYTE red, blue, green;
char* pCurrPixel = (char*)lpPixels;
for (y = 0; y < h; y++)
{
for (x = 0; x < w; x++)
{
red = pCurrPixel[0];
green = pCurrPixel[1];
blue = pCurrPixel[2];
if ((red == 134))
std::cout << x << ", " << y;
pCurrPixel += 4;
}
}
SelectObject(hdcMemory, hBitmapOld);
DeleteObject(hBitmap);
DeleteDC(hdcSource);
DeleteDC(hdcMemory);
return false;
}
【问题讨论】:
-
使用这篇文章中的以下代码:没有正常工作。我按原样复制并粘贴并使用它:w3schools.com/colors/colors_converter.asp 并且它返回了 0 个结果并进行了多次测试。