如何获得一个指定坐标的颜色值?

jeky123 2004-11-21 08:11:57

是用api吗?具体怎么做?
...全文
133 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
AhBian 2004-11-21
  • 打赏
  • 举报
回复
如果是在屏幕上获取指定坐标的颜色值,基本上是要用到 WIN32API 的。
[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));
}

static public Color GetColorOfScreen(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);
}

如果是获取 BitMap 的指定坐标的颜色值,可以简单地直接使用 Bitmap.GetPixel 方法即可。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧