Discussion:
C++ GetPixel()
(too old to reply)
Tvirusx1
2009-05-30 16:11:01 UTC
Permalink
Can someone please explain to me how to use GetPixel and the Coordinate of
that Pixel? I looked it up on MSDN but there it only shows the syntax. I want
to make a Bot in C++ which uses Colour search and the Mouse, but I just can't
seem to figure out how GetPixel() works. I know how to use the Mouse Commands
so don't worry about that, all I would need to know is how to use GetPixel(),
and the Coordinate of that Pixel so the Mouse can click it.

Thanks in advance, I appreciate any help.
Marc
2009-05-31 03:44:24 UTC
Permalink
Post by Tvirusx1
Can someone please explain to me how to use GetPixel and the Coordinate of
that Pixel? I looked it up on MSDN but there it only shows the syntax.
???
1000 samples in MSDN and you don't need samples for a so simple function !!!
Tvirusx1
2009-05-31 22:15:01 UTC
Permalink
Post by Marc
Post by Tvirusx1
Can someone please explain to me how to use GetPixel and the Coordinate of
that Pixel? I looked it up on MSDN but there it only shows the syntax.
???
1000 samples in MSDN and you don't need samples for a so simple function !!!
Simple function...? I searched on Google for about 3-4 days and I could'nt
find a solution. Maybe my Question wasnt clear enough? Long Question short.

How to use GetPixel()?

Or if Possible, do you know a website which explains how to use it? MSDN
isnt a big help, because Im trying to make a Program which finds a Pixel on
the Screen (or Specific window) and then Moves the Mouse Cursor to that
Pixel. In MSDN they explain how to do that with a .jpeg file, but im trying
to use GetPixel() to find a Pixel on the Sceen or in a Specific window.

Thanks in advance.
Scott Seligman
2009-05-31 23:34:31 UTC
Permalink
Post by Tvirusx1
Or if Possible, do you know a website which explains how to use it? MSDN
isnt a big help, because Im trying to make a Program which finds a Pixel on
the Screen (or Specific window) and then Moves the Mouse Cursor to that
Pixel. In MSDN they explain how to do that with a .jpeg file, but im trying
to use GetPixel() to find a Pixel on the Sceen or in a Specific window.
HDC hdcScreen = GetDC(0);
COLORREF crPixel = GetPixel(hdcScreen, 10, 10);
ReleaseDC(0, hdcScreen);

Will store the displayed pixel at coordinates 10,10 into crPixel.

Is this what you were asking for?
--
--------- Scott Seligman <scott at <firstname> and michelle dot net> ---------
I think; therefore I am.
-- Rene Descartes
Tvirusx1
2009-06-01 10:05:05 UTC
Permalink
Post by Scott Seligman
Post by Tvirusx1
Or if Possible, do you know a website which explains how to use it? MSDN
isnt a big help, because Im trying to make a Program which finds a Pixel on
the Screen (or Specific window) and then Moves the Mouse Cursor to that
Pixel. In MSDN they explain how to do that with a .jpeg file, but im trying
to use GetPixel() to find a Pixel on the Sceen or in a Specific window.
HDC hdcScreen = GetDC(0);
COLORREF crPixel = GetPixel(hdcScreen, 10, 10);
ReleaseDC(0, hdcScreen);
Will store the displayed pixel at coordinates 10,10 into crPixel.
Is this what you were asking for?
--
--------- Scott Seligman <scott at <firstname> and michelle dot net> ---------
I think; therefore I am.
-- Rene Descartes
I think thats what ive been looking for, thansk alot for your Reply.
Tvirusx1
2009-06-01 10:09:01 UTC
Permalink
It still gives me a Linker Error, heres my Script so far:

#include <iostream>
#include <windows.h>
using namespace std;

int x,y,x1,y1,i;
POINT pt;

int GetXY() //This function is for getting your current Cursor Pos, and then
returning it to main()
{
GetCursorPos(&pt);
x = (pt.x);
y = (pt.y);
return x,y;
}

int main()
{
Sleep(20000); // Wait 2 Seconds
HDC hdcScreen = GetDC(0);
COLORREF crPixel = GetPixel(hdcScreen,10,10);
ReleaseDC(0,hdcScreen);

return 0;
}

Errors:
[Linker error] undefined reference to '***@12'
Id returned 1 exit status

(im using dev c++)
Tvirusx1
2009-06-01 14:19:01 UTC
Permalink
Don't mind the GetXY()... forgot to delete it.
Tvirusx1
2009-06-01 14:15:01 UTC
Permalink
Ok, I got rid of the Linker Errors by creating a Project... but my Program
doesnt cout the values of RGB. Heres my code:

#include <windows.h>
#include <iostream>
using namespace std;

int x,y,x1,y1,i;
POINT pt;

int GetXY()
{
GetCursorPos(&pt);
x = (pt.x);
y = (pt.y);
return x,y;
}

int main()
{
HDC hdcScreen = GetDC(0);
COLORREF crPixel = GetPixel(hdcScreen,10,10);
ReleaseDC(0,hdcScreen);
int redValue,greenValue,blueValue;
redValue = GetRValue(crPixel);
greenValue = GetGValue(crPixel);
blueValue = GetBValue(crPixel);

cout << redValue << endl;
cout << greenValue << endl;
cout << blueValue << endl;
system("pause");

DeleteDC(hdcScreen);

return 0;
}

any suggestions? By the way I removed the GetXY() function because ill add
that later again.
Scott Seligman
2009-06-01 14:44:21 UTC
Permalink
Post by Tvirusx1
Ok, I got rid of the Linker Errors by creating a Project... but my Program
What do you mean? What does it output, and what should it output?
Post by Tvirusx1
DeleteDC(hdcScreen);
Don't do this. You're already releasing the DC, and since you didn't
create it, there's no need to delete it.
--
--------- Scott Seligman <scott at <firstname> and michelle dot net> ---------
Circular logic will only make you dizzy, Doctor.
-- Peri in Doctor Who:"The Two Doctors"
Tvirusx1
2009-06-01 20:09:01 UTC
Permalink
I am trying to get the Color of the Pixel. The Program is suposed to output
the RGB value (red green blue) for the Pixel at 10,10. But it doesnt output
anything.

Thanks for your reply, I didnt notice that I put ReleaseDC(0,hdcScreen); in
the code allready.

Any suggestions on how to make it work?
Scott McPhillips [MVP]
2009-06-01 21:26:14 UTC
Permalink
Post by Tvirusx1
I am trying to get the Color of the Pixel. The Program is suposed to output
the RGB value (red green blue) for the Pixel at 10,10. But it doesnt output
anything.
Thanks for your reply, I didnt notice that I put ReleaseDC(0,hdcScreen);
in
the code allready.
Any suggestions on how to make it work?
It works for me. I copied your code into a project set up as a Win32 console
program. Worked no problem. Have you tried?

cout << "Hello World!" << endl;
--
Scott McPhillips [VC++ MVP]
Tvirusx1
2009-06-02 10:24:01 UTC
Permalink
It doesnt cout the Values for me, all it does is it writes "Press any ket to
continue...". I dunno why it doesnt work for me, I use Dev C++.
Tvirusx1
2009-06-02 10:50:01 UTC
Permalink
Ive installed Visual C++ and it works! Thanks alot for all of your help. Now
there is only one more open Question.

I now will know the Colour of the Pixel, but not the Location. What I mean
with that is, I know the RGB of a Pixel I am searching for, but, I do not
have its Location. Is there a way to make the Program find the Pixel Location
and cout it, now that we now the Pixel Colour?
Jonathan Wilson
2009-06-02 11:48:22 UTC
Permalink
Post by Tvirusx1
I now will know the Colour of the Pixel, but not the Location. What I mean
with that is, I know the RGB of a Pixel I am searching for, but, I do not
have its Location. Is there a way to make the Program find the Pixel Location
and cout it, now that we now the Pixel Colour?
Only way to do that is to call GetPixel on every pixel and see if its the
color you want.
Tvirusx1
2009-06-02 12:51:01 UTC
Permalink
Thanks for all of your help! Yea I have noticed that I would have to scan
every Pixel Individually but I can use a while loop to make everything easier.

Now I can start working on my Bot =P.
Thanks everyone I really apreciate it!
Scott McPhillips [MVP]
2009-06-02 14:08:39 UTC
Permalink
Post by Tvirusx1
Thanks for all of your help! Yea I have noticed that I would have to scan
every Pixel Individually but I can use a while loop to make everything easier.
Now I can start working on my Bot =P.
Thanks everyone I really apreciate it!
Try a quick test of such a loop and you will find it is totally impractical
:(

GetPixel is way too slow. There are probably several other approaches, but
it is not clear what your goal is.
--
Scott McPhillips [VC++ MVP]
Remy Lebeau
2009-06-02 21:34:57 UTC
Permalink
Post by Scott McPhillips [MVP]
Try a quick test of such a loop and you will find it is totally
impractical :(
GetPixel is way too slow. There are probably several other approaches
Agreed. For instance, copying the screen to an in-memory bitmap, and then
looping through the bitmap's pixel data directly without using GetPixel() at
all.
--
Remy Lebeau (TeamB)
EWOlson
2009-07-10 23:40:02 UTC
Permalink
Post by Remy Lebeau
Post by Scott McPhillips [MVP]
Try a quick test of such a loop and you will find it is totally
impractical :(
GetPixel is way too slow. There are probably several other approaches
Agreed. For instance, copying the screen to an in-memory bitmap, and then
looping through the bitmap's pixel data directly without using GetPixel() at
all.
--
Remy Lebeau (TeamB)
Ya know, for what you are trying to do a freely available scripting language
would probably be better. I've used AutoIt with good results.

If you want the programming practice that is fine, but this is not an easy
task as someone new to Windows programming would initially think. (I know, I
tried it. :-P)
AlexMexico
2009-09-21 05:12:01 UTC
Permalink
I used your code in DevC, you have to create is as a project, and go to
project options, and select Win32 GUI

The other fact, is that if you want to see the pixel values, you have to add
the following line, freopen("salida.txt","w",stdout);, and check the file
salida.txt
Post by Tvirusx1
Ok, I got rid of the Linker Errors by creating a Project... but my Program
#include <windows.h>
#include <iostream>
using namespace std;
int x,y,x1,y1,i;
POINT pt;
int GetXY()
{
GetCursorPos(&pt);
x = (pt.x);
y = (pt.y);
return x,y;
}
int main()
{
HDC hdcScreen = GetDC(0);
COLORREF crPixel = GetPixel(hdcScreen,10,10);
ReleaseDC(0,hdcScreen);
int redValue,greenValue,blueValue;
redValue = GetRValue(crPixel);
greenValue = GetGValue(crPixel);
blueValue = GetBValue(crPixel);
cout << redValue << endl;
cout << greenValue << endl;
cout << blueValue << endl;
system("pause");
DeleteDC(hdcScreen);
return 0;
}
any suggestions? By the way I removed the GetXY() function because ill add
that later again.
Joseph M. Newcomer
2009-09-21 15:02:58 UTC
Permalink
See below...
Post by Tvirusx1
Ok, I got rid of the Linker Errors by creating a Project... but my Program
#include <windows.h>
#include <iostream>
using namespace std;
****
Same problems as before
****
Post by Tvirusx1
int x,y,x1,y1,i;
POINT pt;
****
Same problems as before
****
Post by Tvirusx1
int GetXY()
{
GetCursorPos(&pt);
x = (pt.x);
y = (pt.y);
return x,y;
****
Nonsensical code
****
Post by Tvirusx1
}
int main()
{
HDC hdcScreen = GetDC(0);
COLORREF crPixel = GetPixel(hdcScreen,10,10);
ReleaseDC(0,hdcScreen);
int redValue,greenValue,blueValue;
****
One variable per line, or at least spaces.
****
Post by Tvirusx1
redValue = GetRValue(crPixel);
greenValue = GetGValue(crPixel);
blueValue = GetBValue(crPixel);
cout << redValue << endl;
cout << greenValue << endl;
cout << blueValue << endl;
*****
I presume that you are getting SOMETHING, how are we to guess what you are getting?
****
Post by Tvirusx1
system("pause");
****
Anyone who uses 'system' has serious problems understanding Windows. This is foolish. It
is even more foolish to use the 'pause' command!

You can just set a breakpoint at the end of the function to see the output.
****
Post by Tvirusx1
DeleteDC(hdcScreen);
return 0;
}
any suggestions? By the way I removed the GetXY() function because ill add
that later again.
****
If it doesn't write the values out, then you have other problems. Probably the mistaken
notion that cout is actually useful for something. Why are you writing a graphics program
as a console app? If you just set the variables to 0 and try to cout them, and you don't
get anything, you have some other bug. It has nothing to do with getpixel.
joe
Joseph M. Newcomer
2009-09-21 14:58:20 UTC
Permalink
This is pretty poor code in every dimension that can exist.

See below...
Post by Tvirusx1
#include <iostream>
#include <windows.h>
****
Why are you using iostream? Why is windows.h not in stdafx.h? Why are you not creating
apps using the appwizard?
****
Post by Tvirusx1
using namespace std;
int x,y,x1,y1,i;
****
One variable per line, and if you do use something this horrible in appearance, at least
put spaces after the commas
****
Post by Tvirusx1
POINT pt;
int GetXY() //This function is for getting your current Cursor Pos, and then
returning it to main()
{
GetCursorPos(&pt);
x = (pt.x);
****
Why the gratuitous parentheses, which are completely unnecessary?
****
Post by Tvirusx1
y = (pt.y);
return x,y;
****
Why are you returning only the y-value? Note that under C rules, the x is evaluated and
discarded. Why do you have a function called GetXY that returns only an int, and returns
only the y-coordinate? Wouldn't it make sense to declare the return type as a POINT? and
just return PT? Why is the pt variable outside the function? Use of a global variable
here is a really, really, REALLY poor idea in almost any context!
****
Post by Tvirusx1
}
int main()
{
Sleep(20000); // Wait 2 Seconds
****
So why does your comment say "Wait 2 seconds" and your code wait 20 seconds?
****
Post by Tvirusx1
HDC hdcScreen = GetDC(0);
****
That would get GetDC(NULL)
****
Post by Tvirusx1
COLORREF crPixel = GetPixel(hdcScreen,10,10);
ReleaseDC(0,hdcScreen);
return 0;
}
Id returned 1 exit status
****
Of course, you would have included gdi32.lib in your link, since this is a GDI function;
by default, GDI32 would NOT have been included in the construction of a consolel app,
which does not use GDI. THis should be screamingly obvious from the documentation, which
tells you that the function is found in gdi32.lib, and you should know when you get an
undefined symbol that you have not included the correct libraries!
joe
****
Post by Tvirusx1
(im using dev c++)
a***@yahoo.com
2012-11-10 18:17:46 UTC
Permalink
Post by Joseph M. Newcomer
Post by Scott Seligman
HDC hdcScreen = GetDC(0);
****
That would get GetDC(NULL)
****
Well, technically:

HDC hdcScreen = GetDC(-1) == HDC hdcScreen = GetDC(NULL)

In this case, GetDC(0) just returns a NULL value. There's a difference.
Joseph M. Newcomer
2009-09-21 14:51:44 UTC
Permalink
What is so hard about GetPixel? It wants coordinates. Supply them. The coordinates are
in logical coordinates.

If you want to correlate the mouse, you have to use DPtoLP to locate the nearest
approximate point under the mouse. That has nothing to do with GetPixel. This is
elementary GDI graphics code, so looking for something under "GetPixel" is not going to
tell you anything useful.

If you want an example, you can look at my "Better Zoomin Utility" on my MVP Tips site.
joe
Post by Tvirusx1
Can someone please explain to me how to use GetPixel and the Coordinate of
that Pixel? I looked it up on MSDN but there it only shows the syntax. I want
to make a Bot in C++ which uses Colour search and the Mouse, but I just can't
seem to figure out how GetPixel() works. I know how to use the Mouse Commands
so don't worry about that, all I would need to know is how to use GetPixel(),
and the Coordinate of that Pixel so the Mouse can click it.
Thanks in advance, I appreciate any help.
Loading...