1

How to get the color of a pixel of a Panel (or anything else, such as a Form)?
(Can I do it with Graphics's functions?)

Thank you in advance.

H H
  • 263,252
  • 30
  • 330
  • 514
mrdaliri
  • 7,148
  • 22
  • 73
  • 107
  • Do you mean, by clicking the pixel with the mouse ? And does the form belong to the same application ? – digEmAll Feb 19 '12 at 21:24
  • [Here's an interesting solution to get the color of a pixel on the screen](http://stackoverflow.com/questions/753132/how-do-i-get-the-colour-of-a-pixel-at-x-y-using-c) – digEmAll Feb 19 '12 at 21:33
  • @digEmAll: i've set a BackImage for my Form, when a button is clicked, return the color of pixel(10, 10) of my form (it's backimage) – mrdaliri Feb 19 '12 at 21:56
  • have a look at my answer below – digEmAll Feb 19 '12 at 22:21

1 Answers1

2

Based on the code provided here,
I created an extension method that returns the color of a control pixel, given the client coordinates of the pixel:

public static class ControlExts
{
    public static Color GetPixelColor(this Control c, int x, int y)
    {
        var screenCoords = c.PointToScreen(new Point(x, y));
        return Win32.GetPixelColor(screenCoords.X, screenCoords.Y);
    }
}

So, in your case you can do:

var desiredColor = myForm.GetPixelColor(10,10);
Community
  • 1
  • 1
digEmAll
  • 56,430
  • 9
  • 115
  • 140