Get pixel color from screen coordinance

Nezo

Member
Joined
Sep 27, 2005
Messages
12
Programming Experience
3-5
Alright so I've read about 100 different threads across the internet and none answer this question which has been asked by many.

I want to read the pixel color ANYWHERE on the screen via x,y coordinance,

I do NOT want to take a screen shot of the screen or anything using bmp.GetPixel. Anyone know how to do this? It seems like it should be fairly simple. More specificly the x,y would be the mouse position on the screen, which is easy enough to get, but getting the color of those coordinance is what I'm trying to figure out.
 
I can tell you the right direction for half of it, but trust me; this is anything but an easy task to accomplish (it's low level, and even the slightest error could cause your mouse to freeze).

First, you need to hook the WH_MOUSE API in win32.dll.
This link will give you an outline of what to expect when doing this:
http://www.vbaccelerator.com/home/VB/Code/Libraries/Hooks/Mouse_Gestures/article.asp

The cursor doesn't retain any information about what it's clicking or what's around it; all it knows is that it did click, and it clicked at X/Y. It's very basic information, but it has your essential X and Y coordinates.

As far as using the X and Y coordinates to find the color; I have no idea. When you get down to this level, nothing is graphical or has any color. The values are going to somehow need to be brought back to whatever process is outputting to the monitor.

On the second part, I may be way off about somehow needing to intercept the information as it's being sent to the monitor. There will be something that works in that manner and more then likely, would be the easiest to try and decipher since you know exactly what pixel you're after. Unfortunately, I don't even have a direction for you.

I do know that this is the only way to go about it. Since there are seemingly infinite factors that work against you (mouse drivers could be different, resolution changes, ect ect); nothing else is going to be stable enough.
 
Hmm, has to be an easier way, I've posted on the AutoIt website asking them how they did it in C++. They have a PixelGetColor Function that is most excellent, and will pick up colors on your screen reguardless of what is running.

http://www.autoitscript.com/autoit3/docs/functions/PixelGetColor.htm

I've written several programs using that function, but the language restrictions and slower productivity with autoit have forced me to use vb.net mostly these days =(
 
How do you know that that PixelGetColor function doesn't do exactly what sevenhalo is suggesting, or that it doesn't create a screenshot and get the pixel information from that. The fact that you can make a single method call in AutoIt doesn't mean anything about how complex the implementation of the method is. Maybe it's easy, maybe not. The fact that you want an easy way doesn't mean that there is one.
 
Well it is an assumption based off performance of the function, it really might do a billion complex things but I doubt it has anything to do with taking a screenshot.

How far away could a process be from reading an image to reading a whole screen? I suppose that is a silly question, but reading a pixel from coordinance on an image is really the same idea, just need a different type of map right?
 
Figured it out after referencing some .dlls for a couple hours

Dim LocalMousePosition As Point
PublicDeclareFunction GetPixel Lib "gdi32.dll" (ByVal hdc AsInteger, ByVal x AsInteger, ByVal y AsInteger) AsInteger
PublicDeclareFunction GetWindowDC Lib "user32.dll" (ByVal hwnd AsInteger) AsInteger
PublicDeclareFunction GetDesktopWindow Lib "user32.dll" () AsInteger
Dim winDc = GetWindowDC(GetDesktopWindow)
Dim color = GetPixel(winDc, LocalMousePosition.X, LocalMousePosition.Y).ToString


Not to bad, but a lot more efficent than taking screenshots :D

Edit:
oh and to view the color...

txtColor1.BackColor = System.Drawing.ColorTranslator.FromOle(color)

 
Figured it out after referencing some .dlls for a couple hours
[...]
oh and to view the color...

txtColor1.BackColor = System.Drawing.ColorTranslator.FromOle(color)


Hey there,
I?ve been reading this (old) thread, but I couldn?t figure out how I possibly compare coulors. :shame:
I need a certain value for each colour, not the appearance (i couldn?t view the color anyway..)
what do I need to get a "name" of the color?
 
Back
Top