Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting pixel color in C++

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; 

}
like image 662
rectangletangle Avatar asked Jan 29 '11 21:01

rectangletangle


People also ask

How do I find the pixel color?

Use the ColorSync Utility calculator to get the color values of a pixel on your screen. In the ColorSync Utility app on your Mac, click Calculator in the toolbar of the ColorSync Utility window. Click the magnifying glass , then move your pointer over an area on the screen that you want to examine.

What is GetPixel in C graphics?

The GetPixel function retrieves the red, green, blue (RGB) color value of the pixel at the specified coordinates.

What colors make a pixel?

Each pixel on a computer screen is composed of three small dots of compounds called phosphors surrounded by a black mask. The phosphors emit light when struck by the electron beams produced by the electron guns at the rear of the tube. The three separate phosphors produce red, green, and blue light, respectively.


1 Answers

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:

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.

like image 197
templatetypedef Avatar answered Oct 03 '22 08:10

templatetypedef