如何获取屏幕指定坐标的颜色或者鼠标位置的屏幕颜色...请各位帮帮忙...
解决方案 »
- winform外接程序关于路径问题,求助
- C# 合并Excel
- C#如何快速读取BMP图象?
- WinForm下关于实现定时或手动更新远程数据库的问题...
- 关于type和typeof
- 欢迎C#高手加入QQ群8976405(C#宇宙联盟)。。。
- 帮我解决问题,我给150分
- System.UnauthorizedAccessException: 对路径“c:\inetpub\wwwroot\TragramMini\WebTragMini\tan\Up?
- 求C#读取xml问题?在线等
- 在程序中如何调用外部的软件,比如按个按钮就启动Outlook
- 实在是无法回报帮助过我的人,再散一次分
- 在ASP.net下用CSS如何定义DataGird控件水平居中、垂直居中?
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);
}这段代码很简单,听起来很复杂的东西,其实很简单的。
我看过论坛上的许多回帖,不少有点走弯路,还要 BitBlt,还要 Bitmap,其实都是不必的。
而且忽略了 GetPixel 获取的 uint 值与 Color 的四个分量之间的对应关系。
Color.ToARGB() 结果的顺序是 ARGB,而 GetPixel() 返回的是 ABGR。