0

我可以使用 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 上。

正如雷蒙德建议的那样,我将第二个参数传递为

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);
4

1 回答 1

1
RGBQUAD rgba = pPixels[y * nScreenWidth + x];

请注意,y=0 位于图像的底部,而不是您可能期望的顶部。

于 2012-06-22T17:06:25.323 回答