首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c# 怎么获取屏幕下一点的颜色

2012-07-22 
c# 如何获取屏幕上一点的颜色我想获取屏幕上一点的颜色,用的是下面的方法GetPixelColor(x,y)public Syste

c# 如何获取屏幕上一点的颜色
我想获取屏幕上一点的颜色,用的是下面的方法
GetPixelColor(x,y);

public System.Drawing.Color GetPixelColor(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;
  }

不过运行起来超慢,特别是循环获取多个点的颜色时.......请问各位有什么好的方法么

[解决办法]
这个测试了,挺快,方法仅供参考

C# code
  public partial class Form1 : Form    {[DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]        private static extern int GetDC(int hwnd);        [DllImport("gdi32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]        private static extern int GetPixel(int hdc, int X, int y);        private struct POINTAPI //确定坐标        {            private int X;            private int y;        }        [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)] //确定坐标        private static extern int ReleaseDC(int hwnd, int hdc);        POINTAPI P; //确定坐标        [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]        private static extern int ScreenToClient(int hwnd, ref POINTAPI lpPoint);        [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]        private static extern int WindowFromPoint(int xPoint, int yPoint);      ......private void timer1_Tick(object sender, EventArgs e)        {            textBox6.Text = "X=" + System.Windows.Forms.Control.MousePosition.X.ToString() + " " +   "Y=" + System.Windows.Forms.Control.MousePosition.Y.ToString();            int blue;            int green;            int red;            int hD;            int h;            int c;            int a;            int b;            a = Convert.ToInt32(System.Windows.Forms.Control.MousePosition.X.ToString());            b = Convert.ToInt32(System.Windows.Forms.Control.MousePosition.Y.ToString());            h = WindowFromPoint(a, b);            hD = GetDC(h);            ScreenToClient(h, ref P);            c = GetPixel(hD, a, b);            red = c % 256;            green = (c / 256) % 256;            blue = c / 256 / 256;            if (red != -1 && green != -1 && blue != -1)            {                textBox7.BackColor = System.Drawing.Color.FromArgb(red, green, blue);               }                    } 

热点排行