日期:2014-05-20  浏览次数:20755 次

已知当前点的坐标,获得该点的颜色?????????????
在我的Form上有一点是蓝色的,我想点击Button使得Label上显示该点的颜色即 "Bule "

------解决方案--------------------
label1.BackColor = Color.Blue;
------解决方案--------------------
可能要用API了,GetPixel:
The GetPixel function retrieves the red, green, blue (RGB) color value of the pixel at the specified coordinates.


COLORREF GetPixel(
HDC hdc, // handle to DC
int nXPos, // x-coordinate of pixel
int nYPos // y-coordinate of pixel
);
------解决方案--------------------
你可以参考下如下的代码:
[DllImport( "gdi32.dll ", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern int GetPixel(IntPtr hdc, int nXPos, int nYPos);
[DllImport( "user32.dll ", EntryPoint = "GetDCEx ", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr IntGetDCEx(IntPtr hWnd, IntPtr hrgnClip, int flags);

private void button2_Click(object sender, EventArgs e)
{
Point clientPoint = this.PointToClient(Control.MousePosition);
IntPtr dc = IntGetDCEx(this.Handle, IntPtr.Zero, 0x402);
this.label1.ForeColor = Color.FromArgb(GetPixel(dc, clientPoint.X, clientPoint.Y));
}
------解决方案--------------------
上面的方法有点问题,颜色不太对,你可以使用如下的代码来操作:
[DllImport( "gdi32.dll ", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern int GetPixel(IntPtr hdc, int nXPos, int nYPos);
[DllImport( "user32.dll ", EntryPoint = "GetDCEx ", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr IntGetDCEx(IntPtr hWnd, IntPtr hrgnClip, int flags);
protected override void OnClick(EventArgs e)
{
Point clientPoint = this.PointToClient(Control.MousePosition);
IntPtr dc = IntGetDCEx(this.Handle, IntPtr.Zero, 0x402);
int intColor = GetPixel(dc, clientPoint.X, clientPoint.Y);
int num1 = intColor & 0xff;
int num2 = (intColor > > 8) & 0xff;
int num3 = (intColor > > 0x10) & 0xff;


this.label1.ForeColor = Color.FromArgb(num1, num2, num3);
base.OnClick(e);
}