【问题标题】:Detecting a color when it appears on screen [duplicate]当颜色出现在屏幕上时检测颜色[重复]
【发布时间】:2020-06-09 03:10:57
【问题描述】:

我想做一个颜色检测程序,当找到某种颜色时显示一个消息框

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace evade
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            SearchPixel("#00042");

        }

        private bool SearchPixel(string hexcode)
        {
            Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics graphics = Graphics.FromImage(bitmap as Image);
            graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);

            Color desiredPixelColor = ColorTranslator.FromHtml(hexcode);

            while (true)
            {

                for (int x = 0; x < SystemInformation.VirtualScreen.Width; x++)
                {
                    for (int y = 0; x < SystemInformation.VirtualScreen.Height; y++)
                    {
                        Color currentPixelColor = bitmap.GetPixel(x, y);


                        if (desiredPixelColor == currentPixelColor)
                        {

                            MessageBox.Show("Found!");
                            return true;

                        }

                        else
                        {
                            return false;
                        }
                    }



                }

            }

        }


    }
}

这就是我的代码。颜色是 #00042 这个 (html) 我在 while 循环上运行检测,所以当屏幕上出现所需的颜色时,它会显示一个小消息框,但它不起作用。

【问题讨论】:

  • 我认为你的颜色代码是错误的。 htmlcsscolor.com/hex/000042 您需要 4 个零,即:RGB 代码为 6 位
  • 哦,是的,我的错。它仍然不起作用
  • 我认为你应该将CopyFromScreen 放在while 循环中。
  • Bitmap.GetPixel 是非常慢的方法。请改用this way

标签: c#


【解决方案1】:

在您的代码中:

if (desiredPixelColor == currentPixelColor)
{

    MessageBox.Show("Found!");
    return true;

}
else
{
    return false;
}

如果找到颜色则返回 true,但同时一旦颜色不匹配返回 false。

如果第一个像素不匹配会怎样?然后你返回 false 而不检查其余的像素!

相反,您需要在 for 循环退出后返回 false:

for (int x = 0; x < SystemInformation.VirtualScreen.Width; x++)
{
    for (int y = 0; x < SystemInformation.VirtualScreen.Height; y++)
    {
        Color currentPixelColor = bitmap.GetPixel(x, y);
        if (desiredPixelColor == currentPixelColor)
        {

            MessageBox.Show("Found!");
            return true;

        }
    }
}
return false;

另外,去掉那个 while(true) 循环,因为它没有必要。

【讨论】:

  • 非常感谢您提供的信息!另外,我仍然认为我应该保持一段时间,因为我想在单击按钮后不断检查。
  • 如果您保持从按钮单击处理程序调用的 while 循环,您的应用程序将变得无响应,因为您将占用主 UI 线程并且它将无法响应用户交互或重新粉刷自己。此外,使用return 语句,while 循环将退出;因此它对你无能为力。如果要定期检查,请从 Button 处理程序打开 Timer 控件,然后从 Tick() 事件中检查颜色。或者创建另一个线程并从那里检查...
猜你喜欢
  • 1970-01-01
  • 2011-07-26
  • 2023-03-25
  • 2015-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
相关资源
最近更新 更多