我编写了一个脚本来搜索屏幕上的图像,问题是它花费的时间太长

问题描述 投票:0回答:1
public bool FindImageOnScreen(Bitmap searchFor, Bitmap searchIn)
{
    for (int x = 0; x < searchIn.Width; x++)
    {
        for (int y = 0; y < searchIn.Width; y++)
        {
            Log($"X: {x}, Y: {y}");
            if (y != searchIn.Height && x != searchIn.Width)
            {
                // First Check
                if (searchIn.GetPixel(x, y) == searchFor.GetPixel(0, 0))
                {
                    if (searchIn.GetPixel(x + (searchFor.Width / 2), y + (searchFor.Height / 2)) == searchFor.GetPixel(searchFor.Width / 2, searchFor.Height / 2))
                    {
                        return true;
                    }
                }
            }
            else
            {
                break;
            }

            if (y == searchIn.Height && x == searchIn.Width)
            {
                return false;
            }
        }
    }
    return true;
}

上面是我在屏幕上定位图像的脚本,但由于某种原因需要几分钟才能完成此操作,我怎样才能加快速度?

它有效,但对我来说太慢了,因为我需要它立即工作

c# image performance bitmap imaging
1个回答
0
投票

你的 bool 返回值应该初始化为 false,并且只有当你满足你的标准时才应该设置为 true(对于你所看到的,你正在使用概率)

因此你的测试: if (y != searchIn.Height && x != searchIn.Width) 没有用,因为它已经被 For Conditions+initialized false 处理了。

您可以使用(学习使用)Visual Studio 中嵌入的诊断工具。它可以帮助找到瓶颈。 您可以迭代直到基于图像搜索维度的标准(如果您知道图像尚未开始,则无法检查最后一列。 Log() 在做什么?如果您写在文件中,您就会得到答案。

如果您根据高度(而不是宽度)增加 y,您可以提供帮助...

© www.soinside.com 2019 - 2024. All rights reserved.