【问题标题】:scan a specific area of pixels扫描特定区域的像素
【发布时间】:2021-12-09 21:36:55
【问题描述】:

我想检查某个区域中的像素是否具有某种颜色。 目前我只能检查我的屏幕中间。但我意识到我宁愿从屏幕中间扫描一个 10x10 的框。

这是我目前实际使用的代码。

Point xy = new Point(Screen.PrimaryScreen.Bounds.Width / 2 + 1, Screen.PrimaryScreen.Bounds.Height / 2 + 1);

Color GetPixel(Point position)
        {
            using (var bitmap = new Bitmap(1, 1))
            {
                using (var graphics = Graphics.FromImage(bitmap))
                {
                    graphics.CopyFromScreen(position, new Point(0, 0), new Size(1, 1));
                }
                return bitmap.GetPixel(0, 0);
            }
        }

color = GetPixel(xy);
Color purple = Color.FromArgb(255,254,93,255);

 if (color.Equals(purple) == true).....

是否可以选择从 10x10 的盒子中扫描紫色,并在颜色在该盒子中时返回 true?

【问题讨论】:

    标签: c# colors pixel scanning


    【解决方案1】:

    试试这个功能:

    private bool ContainsColor(Point StartPosition, int BoxSize, Color ColorToScanFor)
        {
            using (Bitmap image = new Bitmap(BoxSize, BoxSize))
            {
                using (Graphics graphics = Graphics.FromImage(image))
                {
                    graphics.CopyFromScreen(StartPosition, Point.Empty, new Size(BoxSize, BoxSize));
    
                    for (int X = 0; X < image.Width; X++)
                    {
                        for (int Y = 0; Y < image.Height; Y++)
                        {
                            if (image.GetPixel(X, Y).ToArgb() == ColorToScanFor.ToArgb())
                            {
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }
    

    当你想调用函数时,试试这样:

    MessageBox.Show(ContainsColor(new Point(0, 0), 10, Color.Black).ToString());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 2020-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多