首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用GetDIBits访问图像的像素颜色

使用GetDIBits访问图像的像素颜色
EN

Stack Overflow用户
提问于 2012-06-23 00:56:58
回答 1查看 913关注 0票数 0

我可以使用GetDIBits加载当前窗口的颜色内容,但我不知道如何从某个位置加载图像的颜色。有人能告诉我怎么做吗?

代码语言:javascript
复制
        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); 

我以这种方式将颜色加载到数组中

代码语言:javascript
复制
            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所建议的,我已经将第二个参数作为

代码语言:javascript
复制
HBITMAP hCaptureBitmap;
hCaptureBitmap = (HBITMAP)LoadImage( NULL, szFileName, IMAGE_BITMAP, 0, 0,
            LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE );

我仍然只捕获活动窗口的颜色,而不是图像的颜色。如何更改设备句柄以反映这一点。

代码语言:javascript
复制
HWND hDesktopWnd; HDC hDesktopDC; HDC hCaptureDC;
hDesktopWnd = GetDesktopWindow();
hDesktopDC = GetDC(hwnd);
hCaptureDC = CreateCompatibleDC(hDesktopDC);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-23 01:06:25

代码语言:javascript
复制
RGBQUAD rgba = pPixels[y * nScreenWidth + x];

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11160773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档