【发布时间】: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#