简体   繁体   English

在 C++ 中获取像素颜色

[英]Getting pixel color in C++

I would like to get the RGB values of a pixel at different x, y coordinates on the screen.我想在屏幕上的不同 x、y 坐标处获取像素的 RGB 值。 How would I go about this in C++?我将如何在 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; 

}

You can use GetDC on the NULL window to get a device context for the whole screen, and can follow that up with a call to GetPixel :您可以在NULL窗口上使用GetDC来获取整个屏幕的设备上下文,然后可以通过调用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.当然,为了提高效率,在执行所有像素读取时,您只想获取和释放设备上下文一次。

As mentioned in a previous post, you want the GetPixel function 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 at your current cursor position is as follows.从 dll 加载 GetPixel 并打印出当前光标位置的像素颜色的基本示例如下。

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

typedef WINAPI COLORREF (*GETPIXEL)(HDC, int, int);

int main(int argc, char** argv)
{

    HINSTANCE _hGDI = LoadLibrary("gdi32.dll");
    if(_hGDI)
    {
        while(true) {
            GETPIXEL pGetPixel = (GETPIXEL)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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM