Getting screen pixel colour (Visual C++)

Posted on 2003-02-28
Medium Priority
Last Modified: 2012-05-04
This is a Visual C++ question.
I was wondering if there is a way to the current pixel colour at the absolute position(x,y). i.e. Colour being displayed anywhere on the screen, not just the current program.
Question by:The_Flash
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

Priyesh earned 100 total points
ID: 8046543
CWnd* pWnd = CWnd::GetDesktopWindow() ;
CDC* pDC = pWnd->GetDC() ;

COLORREF clr = pDC->GetPixel(100, 100) ;

pWnd->ReleaseDC(pDC) ;

Expert Comment

ID: 8046710
just to show you in non MFC
HWND desktop = GetDesktopWindow();
HDC desktopHdc = GetDC(desktop);
COLORREF color = GetPixel(desktopHdc,1,1);

if you need to extract the rgb values then use this way
     int red,green,blue;

     COLORREF color = RGB(255,100,50);

     //COLORREF is stored as 0x00bbggrr, so..
     blue = (color/65536) & 255;
     green = (color/256) & 255;
     red = color & 255;

     cout<<"Red = "<<red;
     cout<<"\nGreen = "<<green;
     cout<<"\nBlue = "<<blue<<endl;

Expert Comment

ID: 8046794
That doesn't seem to work for me, but the C way does

<<<<<<<<<<<<THIS WORKS

    HDC dc = GetDC(NULL);
    COLORREF result = GetPixel(dc, 700, 250);
    ReleaseDC(NULL, dc);


in the MFC version I get result as 0xffffffff (CLR_INVALID)

because per the documentation

"The return value is the RGB value of the pixel. If the pixel is outside of the current clipping region, the return value is CLR_INVALID."


<<<<<<<<<<<<<<<<<<<<THIS ALSO WORKS

    HDC dc = GetDC(NULL);
    CDC pDC;
    COLORREF clr = pDC.GetPixel(700, 250);


but the c version of

HWND wnd = GetDesktopWindow();
HDC dc = GetDC(wnd)


so it seems that there is something wrong with getting the desktop window and querying it for a color....

Priyesh:  if you know what is the equivelant in MFC for



Author Comment

ID: 8047072
Privesh, your code didn't seem to work on windows, only the desktop.
I modified it so it grabbed a window from it's position.

// Any point in which the window current occupies
Point * point(220,340);
CWnd * pWnd = WindowFromPoint( point );
CDC * dc = pWnd->GetDC();
COLORREF color = dc->GetPixel(51,173);

After a bit of frustration and testing, I found out that if it is a broswer window, which it what I was wanting it to work for, every object on the page is treated as a seperate window.
In the case that I was testing, there were 3 flash movies in the page, and each was treated as a different window.

Author Comment

ID: 8047078
I halved the points as it didn't give me the solution, although it did lead me o it.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question