C# 当颜色出现在屏幕上时检测颜色

C# 当颜色出现在屏幕上时检测颜色,c#,C#,我想做一个颜色检测程序,当发现某种颜色时显示一个消息框 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;
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;
                        }
                    }



                }

            }

        }


    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用System.Security.AccessControl;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间规避
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
搜索像素(“00042”);
}
私有布尔搜索像素(字符串十六进制代码)
{
位图位图=新位图(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);
Graphics Graphics=Graphics.FromImage(位图作为图像);
graphics.CopyFromScreen(0,0,0,0,位图.Size);
Color desiredPixelColor=ColorTranslator.FromHtml(十六进制代码);
while(true)
{
对于(int x=0;x
这就是我的代码。颜色是#00042这一个(html),我正在while循环上运行检测,因此当所需颜色出现在屏幕上时,它会显示一个小消息框,但不起作用。

在您的代码中,您有:

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;
for(int x=0;x

另外,去掉
while(true)
循环,因为它不是必需的。

我认为您的颜色代码是错误的。你需要4个零,即RGB代码需要6个数字哦,是的,我的错。虽然我认为你应该把
CopyFromScreen
放在
while
循环中,但它仍然不能工作。
Bitmap.GetPixel
是一种非常慢的方法。请改用。非常感谢您提供的信息!此外,我仍然认为我应该保留while,因为我希望在单击按钮后不断检查。如果你保留从按钮单击处理程序调用的while循环,你的应用程序将变得无响应,因为你将绑定主UI线程,它将无法响应用户交互或重新绘制自身。此外,使用
return
语句,while循环退出;因此,它对你毫无帮助。如果要定期检查,请从按钮处理程序中打开计时器控件,然后从Tick()事件中检查颜色。或者创建另一个线程并从那里检查。。。