How to get the color of the specified pixel in the screen?

  • Thread starter Thread starter Hooyoo
  • Start date Start date
H

Hooyoo

I want to get the color of the specified pixel in the screen.
Firstly I get the position of the cursor:
//Get the position of cursor.
Point position = Cursor.Position;

Next I want to get the color using the position of the cursor.
Anybody here can show me some demo codes?

Thanks.
 
Hello Hooyoo,

Use GetPixel method of WinAPI.
Sample is there http://pinvoke.net/default.aspx/gdi32/GetPixel.html

H> I want to get the color of the specified pixel in the screen.
H> Firstly I get the position of the cursor:
H> //Get the position of cursor.
H> Point position = Cursor.Position;
H> Next I want to get the color using the position of the cursor.
H> Anybody here can show me some demo codes?
H>
H> Thanks.
H>
---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Hooyoo said:
I want to get the color of the specified pixel in the screen.
Firstly I get the position of the cursor:
//Get the position of cursor.
Point position = Cursor.Position;

Next I want to get the color using the position of the cursor.
Anybody here can show me some demo codes?

In addition to what Michael wrote (I'm assuming there's no practical way to
get a Graphics object representing the screen DC in .NET)...

Keep in mind that reading from the video card frame buffer is slow, at least
with AGP cards. So hopefully you don't need to get the pixel's color very
frequently.

Also keep in mind that when an application is using an overlay buffer
(pretty common with video media players, not very common with anything
else), getting the color of a pixel within the overlay will not get the
actual color being displayed there. It will usually be black, but if not it
will be some other default color rather than the pixel you're actually
seeing.

Pete
 
Thank you, Michael.
But actually I don't like use API in C#, I think it makes ungraceful
codes.
So hopefully there are some methonds of .Net Class Libraries can
implement that.
 
Hi,

Hooyoo said:
Thank you, Michael.
But actually I don't like use API in C#, I think it makes ungraceful
codes.
So hopefully there are some methonds of .Net Class Libraries can
implement that.

Unfortunately you will have to use it, there is nothing in the framework for
doing what you want. it's after all platform dependand.

There is nothing wrong in using the win API you know
 
Back
Top