全栈工程师开发手册 (作者:栾鹏)
c#教程全解
c#获取屏幕上某坐标点的颜色
using System.Runtime.InteropServices;
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);
}