C#获取某坐标的颜色

版权声明:版权声明:本文为博主原创文章,转载请附上博文链接! https://blog.csdn.net/qq_36051316/article/details/83859244

C# 获取某坐标的颜色:

这个获取的方法是基于屏幕获取的如果要基于我们的winfrom软件获取的话,就要加个算法计算这个东西。那么可以参考文章:

直接调用我们的 GetPixelColor(); 这个方法传入坐标点就OK了

得到的结果

我们会得到一个ARGB的值

获取某坐标的颜色的核心代码:

 #region 获取某坐标的颜色
 private struct POINT
 {
     private int x;
     private int y;
 }
 static POINT point;

 [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
 private static extern int GetDC(int hwnd);
 [DllImport("gdi32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
 private static extern int GetPixel(int hdc, int x, int y);
 [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
 private static extern int ReleaseDC(int hwnd, int hdc);
 [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
 private static extern int WindowFromPoint(int x, int y);
 [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
 private static extern int ScreenToClient(int hwnd, ref POINT lppoint);

 //获取屏幕指定坐标点的颜色
 public static Color GetPixelColor(int x, int y)
 {
     int h = WindowFromPoint(x, y);
     int hdc = GetDC(h);

     ScreenToClient(h, ref point);
     int c = GetPixel(hdc, x, y);
     return Color.FromArgb(c);
 }

 #endregion

猜你喜欢

转载自blog.csdn.net/qq_36051316/article/details/83859244