Home All Groups Group Topic Archive Search About

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

Author
15 Dec 2006 3:52 AM
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.

Author
15 Dec 2006 5:00 AM
Michael Nemtsev
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
Are all your drivers up to date? click for free checkup

Author
15 Dec 2006 5:16 AM
Lucian Wischik
Michael Nemtsev <nemt***@msn.com> wrote:
>Use GetPixel method of WinAPI.
>Sample is there http://pinvoke.net/default.aspx/gdi32/GetPixel.html

Looking at that page, I think it has a leak. It should
ReleaseDC(hwnd,hdc) the hdc that it acquired.


Ah, it's a wiki. I guess I'll go and change it.

--
Lucian
Author
15 Dec 2006 9:13 AM
Hooyoo
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.
Michael Nemtsev wrote:
Show quoteHide quote
> 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
Author
15 Dec 2006 2:53 PM
Ignacio Machin ( .NET/ C# MVP )
Hi,

"Hooyoo" <zhao_huy***@126.com> wrote in message
news:1166174019.740417.85170@n67g2000cwd.googlegroups.com...
> 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


--
Ignacio Machin
machin AT laceupsolutions com
Author
16 Dec 2006 12:57 AM
Hooyoo
Thanks, got it.
"Ignacio Machin ( .NET/ C# MVP ) дµÀ£º
"
Show quoteHide quote
> Hi,
>
> "Hooyoo" <zhao_huy***@126.com> wrote in message
> news:1166174019.740417.85170@n67g2000cwd.googlegroups.com...
> > 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
>
>
> --
> Ignacio Machin
> machin AT laceupsolutions com
Author
15 Dec 2006 5:16 AM
Peter Duniho
"Hooyoo" <zhao_huy***@126.com> wrote in message
news:1166154771.588175.143310@16g2000cwy.googlegroups.com...
>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

Bookmark and Share