您的位置:首页 > 编程语言 > C#

c#获取屏幕上某坐标点的颜色

2017-10-04 09:56 519 查看
全栈工程师开发手册 (作者:栾鹏)

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: