Getting pixel color in C ++

I would like to get RGB pixel values ​​in different x, y coordinates on the screen. How can I do this in C ++?

I am trying to create my own Gaussian blur effect.

It will be on Windows 7.

Edit

What libraries should be included for start?

What will i do:

#include <iostream> using namespace std ; int main(){ HDC dc = GetDC(NULL); COLORREF color = GetPixel(dc, 0, 0); ReleaseDC(NULL, dc); cout << color; } 
+6
c ++ windows pixel winapi rgb
source share
2 answers

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

GetPixel is inside gdi32.dll, so if you have the proper environment settings, you must enable windows.h (including wingdi.h) and you must be golden.

If you have minimal environment settings for any reason, you can also directly use LoadLibrary on gdi32.dll.

The first GetPixel parameter is a device context handle that can be obtained by calling the GetDC function (which is also available through <windows.h> ).

A basic example that loads GetPixel from a DLL and prints the color of the pixel, wherever your cursor is, looks like this.

 #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; } 
+7
source share

You can use GetDC in the NULL window to get the device context for the entire screen, and you can do this by calling GetPixel :

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

Of course, you only want to get and release the device context once, doing all the pixel readings for efficiency.

+13
source share

All Articles