c++ - 使用 GetDIBits 访问图像的像素颜色

标签 c++ image getdibits

我可以使用 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 );

我仍然只捕获事件窗口的颜色,而不是图像的颜色。我如何更改设备句柄以反射(reflect)这一点。

HWND hDesktopWnd; HDC hDesktopDC; HDC hCaptureDC;
hDesktopWnd = GetDesktopWindow();
hDesktopDC = GetDC(hwnd);
hCaptureDC = CreateCompatibleDC(hDesktopDC);

最佳答案

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

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

关于c++ - 使用 GetDIBits 访问图像的像素颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11160773/

相关文章:

image - 想要使用hadoop处理图像

C++/Win32 : How to get the alpha channel from an HBITMAP?

winapi - 使用 Windows GetDIBits 函数获取位图像素值

c++ - 如何在 UML 类图中显示私有(private)继承关系

c++ - beagle bone black 的 undefined reference 链接器错误静态库交叉编译

c++ - 指向槽函数的指针

java - java.awt.image.BufferedImage 的二值化规则是什么

html - 图片在您的网站上的下载顺序是什么?

c - hwnd 转 ppm 问题

c++ - shared_ptr<> 的自定义删除器给出 'no context error'