Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C+中获取像素颜色+;_C++_Windows_Winapi_Rgb_Pixel - Fatal编程技术网

C++ 在C+中获取像素颜色+;

C++ 在C+中获取像素颜色+;,c++,windows,winapi,rgb,pixel,C++,Windows,Winapi,Rgb,Pixel,我想得到屏幕上不同x,y坐标下像素的RGB值。 我将如何在C++中进行这个? 我正在尝试创建自己的高斯模糊效果 这将在Windows7中实现 编辑 要运行此功能,需要包括哪些库 我要做的是: #include <iostream> using namespace std ; int main(){ HDC dc = GetDC(NULL); COLORREF color = GetPixel(dc, 0, 0); ReleaseDC(NULL, dc);

我想得到屏幕上不同x,y坐标下像素的RGB值。 我将如何在C++中进行这个? 我正在尝试创建自己的高斯模糊效果

这将在Windows7中实现

编辑

要运行此功能,需要包括哪些库

我要做的是:

#include <iostream>

using namespace std ;

int main(){

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

    cout << color; 

}
#包括
使用名称空间std;
int main(){
HDC dc=GetDC(空);
COLORREF color=GetPixel(dc,0,0);
ReleaseDC(NULL,dc);

cout您可以在
NULL
窗口上使用
GetDC
来获取整个屏幕的设备上下文,然后调用
GetPixel

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

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

如前一篇文章所述,您需要Win32 API中的函数

GetPixel位于gdi32.dll内部,所以如果您有一个合适的环境设置,您应该能够包括windows.h(包括wingdi.h),并且您应该是金色的

如果出于任何原因进行了最低限度的环境设置,也可以直接使用gdi32.dll上的LoadLibrary

GetPixel的第一个参数是设备上下文的句柄,可以通过调用GetDC函数(也可以通过
获得)来检索该句柄

下面是一个从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;
}
#包括
#包括
typedef WINAPI COLORREF(*GETPIXEL)(HDC,int,int);
int main(int argc,字符**argv)
{
HINSTANCE _hGDI=LoadLibrary(“gdi32.dll”);
如果(_hGDI)
{
while(true){
GETPIXEL pGetPixel=(GETPIXEL)GetProcAddress(_hGDI,“GETPIXEL”);
HDC _HDC=GetDC(NULL);
如果(_hdc)
{
点光标;
GetCursorPos(&_cursor);
COLORREF(u color=(*pGetPixel)(_hdc,_cursor.x,_cursor.y);
int _red=GetRValue(_color);
int_绿色=GetGValue(_颜色);
int_blue=GetBValue(_color);
printf(“红色:0x%02x\n”,红色);
printf(“绿色:0x%02x\n”,绿色);
printf(“蓝色:0x%02x\n”,_蓝色);
}
免费图书馆;
}
}
返回0;
}

没有足够的信息。您能解释一下为什么要这样做吗?另外,这肯定是操作系统特有的。很抱歉,我添加了更多信息。@Anteater7171-如果您
#包括
,这应该足够了,前提是您是从Visual Studio构建的。多年前我使用了这个技巧,除此之外没有做任何特殊的事情。@Antater7171-抱歉,我对该编译器一无所知,也帮不了你多少忙。你可能想链接一些系统DLL文件,如
gdi32.lib
,可能值得在MSDN上查找需要哪些特定的库。请注意,如果你想对超过几个像素的
GetPixel
执行此操作的速度非常慢。快速的方法是创建一个兼容的DC,在其中创建并选择一个分区,然后直接访问支持其像素的内存。