[DllImport(“gdi32.dll”)]
static public extern uint GetPixel(IntPtr hDC, int XPos, int YPos);
[DllImport(“gdi32.dll”)]
static public extern IntPtr CreateDC(string driverName, string deviceName, string output, IntPtr lpinitData);
[DllImport(“gdi32.dll”)]
static public extern bool DeleteDC(IntPtr DC);
static public byte GetRValue(uint color)
{
return (byte)color;
}
static public byte GetGValue(uint color)
{## 读鼠标位置的颜色
return ((byte)(((short)(color)) >> 8));
}
static public byte GetBValue(uint color)
{
return ((byte)((color) >> 16));
}
static public byte GetAValue(uint color)
{
return ((byte)((color) >> 24));
}
public Color GetColor(Point screenPoint)
{
IntPtr displayDC = CreateDC(“DISPLAY”, null, null, IntPtr.Zero);
uint colorref = GetPixel(displayDC, screenPoint.X, screenPoint.Y);
DeleteDC(displayDC);
byte Red = GetRValue(colorref);
byte Green = GetGValue(colorref);
byte Blue = GetBValue(colorref);
return Color.FromArgb(Red, Green, Blue);
}
获取鼠标所在位置颜色
最新推荐文章于 2023-12-02 18:20:08 发布
这段代码展示了如何使用DllImport导入gdi32.dll中的函数,获取显示器上指定位置的像素颜色,并将其分解为红、绿、蓝和alpha通道的值。通过CreateDC、GetPixel、DeleteDC等函数,实现了从屏幕捕获颜色信息,然后利用GetRValue、GetGValue、GetBValue和GetAValue将颜色值转化为可读的字节。
摘要由CSDN通过智能技术生成