在C ++中获取像素颜色 [英] Getting pixel color in C++

查看:180
本文介绍了在C ++中获取像素颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想得到像素在屏幕上不同x,y坐标的RGB值。
我将如何在C ++中这样做?



我试图创建我自己的高斯模糊效果。



这将是在Windows 7中。



编辑 b
$ b

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



我要做什么:

  #include< iostream> 

using namespace std;

int main(){

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

cout<<颜色;

}


解决方案

在上一篇文章中,您希望 GetPixel 函数从Win32 API。



GetPixel位于gdi32.dll内部,所以如果你有一个合适的环境设置,你应该能够包括windows.h



如果你有一个最小的环境设置,无论什么原因,你也可以直接使用gdi32.dll上的LoadLibrary。



GetPixel的第一个参数是设备上下文的句柄,可以通过调用GetDC函数(也可通过< windows。



从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\\\
,_red);
printf(Green:0x%02x\\\
,_green);
printf(Blue:0x%02x\\\
,_blue);
}
FreeLibrary(_hGDI);

return 0;
}


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++?

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

This would be in 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; 

}

解决方案

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

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.

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

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>).

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

#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;
}

这篇关于在C ++中获取像素颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆