我可以使用GetDIBits加载当前窗口的颜色内容,但我不知道如何从一个位置加载图像的颜色。有人能告诉我怎么做吗?
char str[256];
HDC hdc;
HWND hDesktopWnd; HDC hDesktopDC; HDC hCaptureDC;
HBITMAP hCaptureBitmap; BITMAPINFO bmi = {0};
RGBQUAD *pPixels;
int nScreenWidth, nScreenHeight;
hdc = GetDC(hwnd);
GetWindowRect(hwnd,&rect);
hdc = GetDC(hwnd);
if(GetWindowRect(hwnd, &rect))
{
width = rect.right - rect.left;
height = rect.bottom - rect.top;
}
nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
hDesktopWnd = GetDesktopWindow();
hDesktopDC = GetDC(hwnd);
hCaptureDC = CreateCompatibleDC(hDesktopDC);
hCaptureBitmap = CreateCompatibleBitmap(hDesktopDC, nScreenWidth, nScreenHeight);
SelectObject(hCaptureDC, hCaptureBitmap);
BitBlt(hCaptureDC, 0, 0, nScreenWidth, nScreenHeight, hDesktopDC, 0,0, SRCCOPY|CAPTUREBLT);
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
bmi.bmiHeader.biWidth = nScreenWidth;
bmi.bmiHeader.biHeight = nScreenHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
pPixels = new RGBQUAD[nScreenWidth * nScreenHeight];
::GetDIBits(hCaptureDC,
hCaptureBitmap,
0,
nScreenHeight,
pPixels,
&bmi,
DIB_RGB_COLORS);
我以这种方式将颜色加载到数组上
for(int i= 0;i< nScreenHeight; i++){
for(int j= 0;j< nScreenWidth; j++){
col.red_palette[i][j] = pPixels[(nScreenWidth * (nScreenHeight-(i+1))) + j].rgbRed;
col.green_palette[i][j] = pPixels[(nScreenWidth * (nScreenHeight-(i+1))) + j].rgbGreen;
col.blue_palette[i][j] = pPixels[(nScreenWidth * (nScreenHeight-(i+1))) + j].rgbBlue;
}
}
delete [] pPixels;
ReleaseDC(hDesktopWnd, hDesktopDC);
DeleteDC(hCaptureDC);
DeleteObject(hCaptureBitmap);
我是Windows编程的新手,只想知道如何将图像加载到hdc上。
正如Raymond建议我已经将第二个参数传递给
HBITMAP hCaptureBitmap;
hCaptureBitmap = (HBITMAP)LoadImage( NULL, szFileName, IMAGE_BITMAP, 0, 0,
LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE );
我仍然只捕捉活动窗口的颜色,而不是图像的颜色。如何更改设备句柄以反映该内容。
HWND hDesktopWnd; HDC hDesktopDC; HDC hCaptureDC;
hDesktopWnd = GetDesktopWindow();
hDesktopDC = GetDC(hwnd);
hCaptureDC = CreateCompatibleDC(hDesktopDC);
答案 0 :(得分:1)
RGBQUAD rgba = pPixels[y * nScreenWidth + x];
请注意,y = 0位于图像的底部,而不是您所期望的顶部。