GetPixel returns incorrect values

Hello everyone. This is my first post on this forum, please don't go hard on me if I didn't post it in the right place or if I did something wrong, I don't post in forums very much. So, I have this problem with GetPixel fuction. Basically, it should return the color decimal at x, y. The code I am about to post works perfectly on windows 7 32bit, but recently I bought a new laptop y50-70 with windows 8.1 64bit and the same code works completely different. I can't find any solution to the problem, can't even describe it. I think it could have something to do with desktop handle, HDC, GetDC(), GetPixel(), maybe even with my computer resolution, refresh rate or something like that... I have even recorded some videos which could help you understand the problem I am having because I can't even describe it correctly. It is like the real color is x = 219, y = 407 away from the place, where my mouse is pointing.

Feel free to use this code, hope it will work fine for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <Windows.h>
using namespace std;

void Detect();

int main()
{
	Detect();

	return 0;
}

void Detect()
{
	POINT p;
	HDC hDC = GetDC(0);
	int x, y;

	while (!GetAsyncKeyState(VK_INSERT)) // Press insert to stop
	{
		GetCursorPos(&p);
		x = p.x;
		y = p.y;
		hDC = GetDC(0);
		cout << x << " " << y << " " << GetPixel(hDC, x, y) << endl;
		Sleep(50);
	}
	ReleaseDC(0, hDC);
}


Links to the problem below:
https://youtu.be/q2H2M8WLHVI
https://youtu.be/UcneHwXaGoM

If anyone could at least help somehow or tell what to do, where to go, I would very, very much appreaciate it. One of the main reasons why I started programming is because something like this, working with colours, conditions, etc... and now I can't advance further which is really sad. Hope to hear a reply. Thank you.
I can't see the problem but you should put GetDC() above the loop, not inside it.
No, no. I use it to refresh the screen to get same or new values at that location.
But GetDC() doesn't refresh the screen. You don't need to update the DC when something happens on the screen. And calling GetDC() like that without a corresponding ReleaseDC() causes a memory leak.
I had a Dialog App template handy and just whipped up something. If you want to look at it the source and compiled exe (Code::Blocks / MinGW) are here: https://www.dropbox.com/s/2oa5ljgxe4mfqom/getrgb.zip?dl=0

EDIT: The relevant code is in getrgb.cpp. I decided to use a thread to capture the pixel info, as it wouldn't cause squirrely behavior in the dialog itself.

*Also running Windows 8.1
Last edited on
Thanks ahcfan, I will add ReleaseDC to the loop, don't want any memory leaks. But this is not a solution to the main problem. Thank you Texan40 as well, I tried out your program, thought it would work but, unfortunately, it didn't. Are there any other ideas what can cause this issue? The code above works on windows 7, but not on windows 8.1, maybe it will work or you, try it out if you want and I really want that code to work for me.
Odd. I'm running Windows 8.1 as well and it captures the R/G/B for any values on either of my dual screens when I click 'Start'. Sorry it didn't work.
Made a command line like yours, just had to link with gdi32. Works as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <Windows.h>

using namespace std;

void Detect(HDC &hDC)
{
  POINT p;
  while (!GetAsyncKeyState(VK_ESCAPE)) // Press escape to stop
  {
    GetCursorPos(&p);
    COLORREF cr = GetPixel(hDC,p.x,p.y);
    cout << "(" << p.x << "," << p.y << ") R(" << (unsigned int)GetRValue(cr) << ") G(" << (unsigned int)GetGValue(cr) << ") B(" << (unsigned int)GetBValue(cr) << ")" << endl;
    Sleep(0);
  }
}

int main()
{
  HDC hDC = GetDC(0);
  Detect(hDC);
  ReleaseDC(0,hDC);

  return 0;
}
Last edited on
No, still doesn't work neither on VisualStudio, nor on CodeBlocks, though it is nice to see RBG values in your code. Could it be because of my screen resolution, some other issues or maybe there are some functions that need to be included for your or my code to work or maybe it can't work on some machines?
Topic archived. No new replies allowed.