简体   繁体   中英

Make this simple c# image searching code quicker?

I am scanning the screen for a button to click using the code below.

I pass in two bitmaps one is a picture of the button the other is a screenshot.

Is there anyway I can speed this basic method up?

private PositionToClick IsInCapture(Bitmap searchFor, Bitmap searchIn)
{
    for (int x = 0; x < searchIn.Width; x++)
    {
        for (int y = 0; y < searchIn.Height; y++)
        {
            bool invalid = false;
            int k = x, l = y;
            for (int a = 0; a < searchFor.Width; a++)
            {
                l = y;
                for (int b = 0; b < searchFor.Height; b++)
                {
                    if (searchFor.GetPixel(a, b) != searchIn.GetPixel(k, l))
                    {
                        invalid = true;
                        break;
                    }
                    else
                        l++;
                }
                if (invalid)
                    break;
                else
                    k++;
            }

            if (!invalid)
                return new PositionToClick() { X = x, Y = y, found = true };
        }
    }

    return new PositionToClick();
}

You can convert the bitmaps to byte arrays first, and then compare those arrays. Also, you can restrict your search area by subtracting the width and height of the image you are searching from. I did not test the code below, it's just an illustration of what I mean.

private PositionToClick IsInCapture(Bitmap searchFor, Bitmap searchIn)
{
    var searchForArray = ImageToByte2(searchFor);
    var searchInArray = ImageToByte2(searchIn);
    for (int x = 0; x <= searchIn.Width - searchFor.Width; x++)
    {
        for (int y = 0; y <= searchIn.Height - searchFor.Height; y++)
        {
            bool invalid = false;
            int k = x, l = y;
            for (int a = 0; a < searchFor.Width; a++)
            {
                l = y;
                for (int b = 0; b < searchFor.Height; b++)
                {
                    var pixelFor = searchForArray[a * searchFor.Width + b];
                    var pixelIn = searchInArray[k + searchIn.Width + l];
                    if (pixelIn != pixelFor)
                    {
                        invalid = true;
                        break;
                    }
                    else
                        l++;
                }
                if (invalid)
                    break;
                else
                    k++;
            }

            if (!invalid)
                return new PositionToClick() { X = x, Y = y, found = true };
        }
    }
    return new PositionToClick();
}

public static byte[] ImageToByte2(Image img)
{
    using (var stream = new MemoryStream())
    {
        img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        return stream.ToArray();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM