user3690993
user3690993

Reputation: 89

Finding the bitmap in bigger bitmap (Pixel bot) - How to make it/for loops faster?

so this function is finding a bitmap in bigger bitmap.. buuuut .. it takes like 4-6s to go through 1920x1080 screen.. I wouldn't be asking you if I 100% understood to that code, which I dont, I am a newbie in programming and my logic is kinda sleeping already (4:00 AM).. (Code is from video tutorial, i was using it for a long time without even studying it :D)

So what do I exactly want is to make a bot finding pixels/pictures in the game and then clicking it, but 4-6s is pretty long, aint it ? I was pretty curious about this, because pixel bots for several games made in other programming languages are pretty, pretty fast ! Like 1s... So I was like, its caused by c# ? It probably aint caused by c# .. but this code is proly working, but maybe it doesnt need to be that long ? I thought about "getting faster loops" .. but thats aint happening probably.

Try to invent some solution, I will be very grateful ! And I still think that this problem might be important to solve for many other people.

private bool FindBitmap(Bitmap bmpNeedle, Bitmap bmpHaystack, out Point location)
{
    for (int outerX = 0; outerX < bmpHaystack.Width - bmpNeedle.Width; outerX++)
    {
        for (int outerY = 0; outerY < bmpHaystack.Height - bmpNeedle.Height; outerY++)
        {
            for (int innerX = 0; innerX < bmpNeedle.Width; innerX++)
            {
                for (int innerY = 0; innerY < bmpNeedle.Height; innerY++)
                {
                    Color cNeedle = bmpNeedle.GetPixel(innerX, innerY);
                    Color cHaystack = bmpHaystack.GetPixel(innerX + outerX, innerY + outerY);

                    if (cNeedle.R != cHaystack.R || cNeedle.G != cHaystack.G || cNeedle.B != cHaystack.B)
                    {
                        goto notFound;
                    }
                }
            }
            location = new Point(outerX, outerY);
            return true;

        notFound:
            continue;
        }
    }
    location = Point.Empty;
    return false;
}    

Upvotes: 2

Views: 1856

Answers (1)

IL4Miy
IL4Miy

Reputation: 128

I wrote a little class for it using LockBits and unsafe code. The performance is great. I decided to use uint values instead of colors. For converting you can use http://www.vcskicks.com/color-uint.php

/// <summary>
///     Represents a bitmap with bit functions.
/// </summary>
public class LockedBitmap
{
private Rectangle _bounds;

/// <summary>
///     Gets or sets the bitmap.
/// </summary>
public Bitmap Bitmap { get; set; }

/// <summary>
///     Gets or sets the values of the bitmap.
/// </summary>
/// <remarks>Watch at the static length!</remarks>
public uint[] Buffer { get; set; }

/// <summary>
///     Gets or sets the bitmap data.
/// </summary>
public BitmapData BitmapData { get; set; }

/// <summary>
///     Initializes a new instance of <see cref="LockedBitmap" />.
/// </summary>
/// <param name="bitmap">The processed bitmap.</param>
public LockedBitmap(Bitmap bitmap)
{
    this.Bitmap = bitmap;
}

/// <summary>
///     Locks a Bitmap into system memory.
/// </summary>
public unsafe void LockBits()
{
    var width = this.Bitmap.Width;
    var height = this.Bitmap.Height;

    var imageLockMode = ImageLockMode.UserInputBuffer;

    // Setting imageLockMode
    imageLockMode = imageLockMode | ImageLockMode.ReadOnly;
    imageLockMode = imageLockMode | ImageLockMode.WriteOnly;

    // Save the bouunds
    this._bounds = new Rectangle(0, 0, width, height);

    // Create Pointer
    var someBuffer = new uint[width*height];
    // Pin someBuffer
    fixed (uint* buffer = someBuffer) //pin
    {
        // Create new bitmap data.
        var temporaryData = new BitmapData
        {
            Width = width,
            Height = height,
            PixelFormat = PixelFormat.Format32bppArgb,
            Stride = width*4,
            Scan0 = (IntPtr) buffer
        };

        // Get the data
        this.BitmapData = this.Bitmap.LockBits(this._bounds, imageLockMode, PixelFormat.Format32bppArgb,
            temporaryData);
        // Set values
        this.Buffer = someBuffer;
    }
}

/// <summary>
///     Unlocks this Bitmap from system memory.
/// </summary>
public void UnlockBits()
{
    this.Bitmap.UnlockBits(this.BitmapData);
}

/// <summary>
///     Iterate through all pixel values.
/// </summary>
public unsafe void Iterate()
{
    // Dimension
    var width = this.Bitmap.Width;
    var height = this.Bitmap.Height;
    // Actual uint position
    int cp = 0;
    // Pointer at the fist uint
    var scp = (uint*)this.BitmapData.Scan0;
    // Stick the array
    fixed (uint* cb = this.Buffer)

        // Step through each pixel
        for (uint* cbp = cb, cbdest = cb + this.Buffer.Length; cbp < cbdest; cbp++)
        {
            // Get x and y from position
            var x = cp % width;
            var y = cp / width;

            var color = *cbp;

            cp++;  // Increment cp
        }
}
}

Example usage:

using (OpenFileDialog ofd = new OpenFileDialog())
{
    if (ofd.ShowDialog() == DialogResult.OK)
    {
        Image image = Image.FromFile(ofd.FileName);
        LockedBitmap locked = new LockedBitmap((Bitmap) image);
        locked.LockBits();
        locked.Iterate();
        locked.UnlockBits();
    }
}

color has now the value of the pixel's color. cp is the absolut position (1D) and (x|y) the point position in the bitmap (2D)

Upvotes: 1

Related Questions