Getting the color of the clicked pixel

  • Thread starter Thread starter Wade
  • Start date Start date
W

Wade

Hi,

I have a generic Control added to a form. On this control, I draw a few
rectangles and fill them with some color. So, for example, I could have two
rectanges on my control, one filled with Blue and the other with Red.

Now ... I also have a routine that lets me add points to this Control.
These points are small circles filled in with a color.

Now, what I'd like to do is, on the Control_MouseUp event, is determine the
color of the pixel they selected.

So, if they click on an area that is not filled with a color, they'll get
the color of the Control. If they click on one of the areas with the
rectangle, then they'll get the color of the rectangle.

Anyone know how to do this?

Thanks!

Wade
 
[DllImport("coredll.dll")]
private static extern IntPtr GetDC(IntPtr hwnd);

[DllImport("coredll.dll")]
private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);

[DllImport("coredll.dll")]
private static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);


protected override void OnMouseDown(MouseEventArgs e)
{
IntPtr hdc = GetDC(IntPtr.Zero);
uint c = GetPixel(hdc, e.X, e.Y); //32bit color
ReleaseDC(this.Handle, hdc);
}
 
Back
Top