在 C++ 中获取像素颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4839623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:42:17  来源:igfitidea点击:

Getting pixel color in C++

c++windowswinapirgbpixel

提问by rectangletangle

I would like to get the RGB values of a pixel at different x, y coordinates on the screen. How would I go about this in C++?

我想在屏幕上的不同 x、y 坐标处获取像素的 RGB 值。我将如何在 C++ 中解决这个问题?

I'm trying to create my own gaussian blur effect.

我正在尝试创建自己的高斯模糊效果。

This would be in Windows 7.

这将在 Windows 7 中。

Edit

编辑

What libraries need to be included for this to run?

需要包含哪些库才能运行?

What I have going:

我要做什么:

#include <iostream>

using namespace std ;

int main(){

    HDC dc = GetDC(NULL);
    COLORREF color = GetPixel(dc, 0, 0);
    ReleaseDC(NULL, dc);

    cout << color; 

}

回答by templatetypedef

You can use GetDCon the NULLwindow to get a device context for the whole screen, and can follow that up with a call to GetPixel:

您可以GetDCNULL窗口上使用以获取整个屏幕的设备上下文,然后可以调用以下方法进行跟进GetPixel

HDC dc = GetDC(NULL);
COLORREF color = GetPixel(dc, x, y);
ReleaseDC(NULL, dc);

Of course, you'd want to only acquire and release the device context once while doing all the pixel-reading for efficiency.

当然,为了提高效率,在执行所有像素读取时,您只想获取和释放设备上下文一次。

回答by syllogism

As mentioned in a previous post, you want the GetPixelfunction from the Win32 API.

如前一篇文章所述,您需要来自 Win32 API的GetPixel函数。

GetPixel sits inside gdi32.dll, so if you have a proper environment setup, you should be able to include windows.h(which includes wingdi.h) and you should be golden.

GetPixel 位于 gdi32.dll 中,因此如果您有适当的环境设置,您应该能够包含 windows.h(其中包含 wingdi.h),并且您应该很成功。

If you have a minimal environment setup for whatever reason, you could also use LoadLibrary on gdi32.dll directly.

如果您出于某种原因设置了最少的环境,也可以直接在 gdi32.dll 上使用 LoadLibrary。

The first parameter to GetPixel is a handle to the device context, which can be retrieved by calling the GetDC function(which is also available via <windows.h>).

GetPixel 的第一个参数是设备上下文的句柄,可通过调用 GetDC 函数(也可通过 获得<windows.h>)检索该句柄。

A basic example that loads GetPixel from the dll and prints out the color of the pixel wherever your cursor is is as follows.

从 dll 加载 GetPixel 并打印出光标所在位置的像素颜色的基本示例如下。

#include<windows.h>
#include<stdio.h>

int main(int argc, char** argv)
{
    FARPROC pGetPixel;

    HINSTANCE _hGDI = LoadLibrary("gdi32.dll");
    if(_hGDI)
    {
        pGetPixel = GetProcAddress(_hGDI, "GetPixel");
        HDC _hdc = GetDC(NULL);
        if(_hdc)
        {
            POINT _cursor;
            GetCursorPos(&_cursor);
            COLORREF _color = (*pGetPixel) (_hdc, _cursor.x, _cursor.y);
            int _red = GetRValue(_color);
            int _green = GetGValue(_color);
            int _blue = GetBValue(_color);

            printf("Red: 0x%02x\n", _red);
            printf("Green: 0x%02x\n", _green);
            printf("Blue: 0x%02x\n", _blue);
        }
        FreeLibrary(_hGDI);
    }
    return 0;
}