获取鼠标所在位置颜色

这段代码展示了如何使用DllImport导入gdi32.dll中的函数,获取显示器上指定位置的像素颜色,并将其分解为红、绿、蓝和alpha通道的值。通过CreateDC、GetPixel、DeleteDC等函数,实现了从屏幕捕获颜色信息,然后利用GetRValue、GetGValue、GetBValue和GetAValue将颜色值转化为可读的字节。
摘要由CSDN通过智能技术生成

[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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值