// x,y 分别为x轴,y轴坐标 返回System.Drawing.Color 可以直接显示
public System.Drawing.Color GetPixelColor(int x, int y)
{
IntPtr hdc = GetDC(IntPtr.Zero);
uint pixel = GetPixel(hdc, x, y);
ReleaseDC(IntPtr.Zero, hdc);
Color color = Color.FromArgb((int)(pixel & 0x000000FF),
(int)(pixel & 0x0000FF00) >> 8,
(int)(pixel & 0x00FF0000) >> 16);
return color;
}
//定义一个定时器 获取当前坐标上的像素点颜色
private void timer_Tick(object sender, EventArgs e)
{
textBox1.Text = GetPixelColor(Cursor.Position.X, Cursor.Position.Y).R.ToString() + " " + GetPixelColor(Cursor.Position.X, Cursor.Position.Y).G.ToString() + " " + GetPixelColor(Cursor.Position.X, Cursor.Position.Y).B.ToString();
}
追问当前上下文不存在GetDC GetPixel ReleaseDC
追答你要添加引用
using System.Runtime.InteropServices;
然后
[DllImport("user32.dll")]
public static extern System.UInt32 GetPixel(IntPtr hdc, int xPos, int yPos);
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
private static extern IntPtr ReleaseDC(IntPtr hc,IntPtr hDest);
追问无法在 DLL“user32.dll”中找到名为“GetPixel”的入口点。
追答[DllImport("gdi32.dll")] 用这个
public static extern System.UInt32 GetPixel(IntPtr hdc, int xPos, int yPos);
本回答被提问者采纳