Getting the color of the clicked pixel

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
 
L

leibnizster

[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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top