匹配颜色

来源:互联网 发布:西华师范大学知乎 编辑:程序博客网 时间:2024/04/29 18:03
  private static Bitmap GetScreenShot()
            {
              Bitmap result = new Bitmap(Screen.PrimaryScreen.Bounds.Width
                  , Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
              {
                using (Graphics gfx = Graphics.FromImage(result))
                {
                  gfx.CopyFromScreen(Screen.PrimaryScreen.Bounds.X
                      , Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
                }
              }


              return result;
            }


            private static Point[] FindColor(Color color)
            {
                int searchValue = color.ToArgb();
                List<Point> result = new List<Point>();
                using (Bitmap bmp = GetScreenShot())
                {
                    for (int x = 0; x < bmp.Width; x++)
                    {
                            for (int y = 0; y < bmp.Height; y++)
                            {
                            if (searchValue.Equals(bmp.GetPixel(x, y).ToArgb()))
                                result.Add(new Point(x, y));
                            }
                    }
                }


                return result.ToArray();
            }


            private void button1_Click(object sender, EventArgs e)
            {
                Point[] points = FindColor(label1.BackColor);
                if (points.Length > 0)
                    Cursor.Position = points[0];
            }
0 0