这个是在Windows下获取鼠标处的屏幕颜色的类,在其他平台或者终端环境未做测试。
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace ColorPicker
{
/// <summary>
/// 获取当前光标处颜色,win8下wpf测试成功
/// </summary>
public class ColorPickerManager
{
/// <summary>
///
/// </summary>
/// <param name="x">鼠标相对于显示器的坐标X</param>
/// <param name="y">鼠标相对于显示器的坐标Y</param>
/// <returns></returns>
public static Color GetColor(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;
}