Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7592659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:57:54+00:00 2026-05-30T20:57:54+00:00

When I use the following code, it takes around 3-5 seconds before the loop

  • 0

When I use the following code, it takes around 3-5 seconds before the loop is done if the image I am searching for in the image is not found. While this is searching the rest of the program is paused, my timers gets out of sync and it looks like the program freezes for a few seconds. The images are not very big, “printscreen” is around 344×354 and “Ok” is around 15×7. I know it is because of the for-loops, but is there a better way to do this or can I runt his part of the program besides the rest of the program in some way, so the program won’t freeze for a few seconds.

// Ok is the image I am searching for.
// printscreen is the image I am searching in.

Bitmap Ok = new Bitmap(Properties.Resources.popupok1);
int Count = 0;
for (int x = 0; x < printscreen.Width; x++)
{
    for (int y = 0; y < printscreen.Height; y++)
    {
        Count = 0;
        if (printscreen.GetPixel(x, y) == Ok.GetPixel(0, 0) && 
            printscreen.GetPixel(x + 1, y) == Ok.GetPixel(1, 0))
        {
            for (int OkX = 0; OkX <= Ok.Width; OkX++)
            {
                for (int OkY = 0; OkY <= Ok.Height; OkY++)
                {
                    try
                    {
                        if (printscreen.GetPixel(x + OkX, y + OkY) != Ok.GetPixel(OkX, OkY))
                        {
                            OkX = Ok.Width;
                            OkY = Ok.Height;
                        }
                        else
                        {
                            Count += 1;
                        }
                        if (Count == 105)
                        {
                            X = x;
                            Y = y;
                            OkX = Ok.Width;
                            OkY = Ok.Height;
                            x = printscreen.Width - 1;
                            y = printscreen.Height - 1;
                            Console.Add("Ok button found.");
                            Console.Add("");
                            ConsoleUpdate();
                        }
                    }
                    catch { }
                }
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:57:56+00:00Added an answer on May 30, 2026 at 8:57 pm

    The performance issue is caused by GetPixels/SetPixels, which is an extraordinarily slow way to access the data in an .NET Bitmap. Instead, I would look into the Bitmap.LockBits method to get a pointer to the bitmap and manipulate data directly. It will be an order of magnitude faster.

    See MSDN:

    The following code example demonstrates how to use the PixelFormat, Height, Width, and Scan0 properties; the LockBits and UnlockBits methods; and the ImageLockMode enumeration. This example is designed to be used with Windows Forms. To run this example, paste it into a form and handle the form’s Paint event by calling the LockUnlockBitsExample method, passing e as PaintEventArgs.

    private void LockUnlockBitsExample(PaintEventArgs e)
    {
    
        // Create a new bitmap.
        Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");
    
        // Lock the bitmap's bits.  
        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
        System.Drawing.Imaging.BitmapData bmpData = 
            bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
            bmp.PixelFormat);
    
        // Get the address of the first line.
               IntPtr ptr = bmpData.Scan0;
    
        // Declare an array to hold the bytes of the bitmap.
        // This code is specific to a bitmap with 24 bits per pixels.
        int bytes = bmp.Width * bmp.Height * 3;
        byte[] rgbValues = new byte[bytes];
    
        // Copy the RGB values into the array.
        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
    
        // Set every red value to 255.  
        for (int counter = 2; counter < rgbValues.Length; counter+=3)
            rgbValues[counter] = 255;
    
        // Copy the RGB values back to the bitmap
        System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
    
        // Unlock the bits.
        bmp.UnlockBits(bmpData);
    
        // Draw the modified image.
        e.Graphics.DrawImage(bmp, 0, 150);
    
    }
    

    If you want to go even faster rather than copy the array out, manipulate it and copy it back, you can operate on the bitmap in place using an unsafe pointer. In this case, the inner part would change as follows:

        // Lock the bitmap's bits.  
        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
        System.Drawing.Imaging.BitmapData bmpData = 
            bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
            bmp.PixelFormat);
    
        // Get the address of the first line.
        IntPtr ptr = bmpData.Scan0;
    
        // Declare an array to hold the bytes of the bitmap.
        // This code is specific to a bitmap with 24 bits per pixels.
        int bytes = bmp.Width * bmp.Height * 3;
        unsafe
        {
            byte* rgbValues = (byte*)ptr;
    
            // Set every red value to 255.  
            for (int counter = 2; counter < bytes counter+=3)
                rgbValues[counter] = 255;
        } 
    
        // Unlock the bits.
        bmp.UnlockBits(bmpData);
    

    Just take care to note the PixelFormat of the bitmap. The example above assumes its 24 bits per pixel BGR. In actual fact many bitmaps are BGRA (32bits per pixel) so you will need to modify four bytes for Blue, Gree, Red, Alpha in that order per pixel.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Use the following code to load png image: UIImage *imageBack1 = [UIImage imageNamed:@Bar1.png];
I use the following code to load an image into an scroll view. The
Loading the image one time works as done in the following steps (for code
I use the following code to create countdowns in Javascript. n is the number
I use the following code to compile a cpp file to object file. g++
I use the following code try to create an array of string vectors, I
I use the following code to layout network drives on a system. I want
I use the following code: Calendar calendar = new GregorianCalendar(0,0,0); calendar.set(Calendar.YEAR, 1942); calendar.set(Calendar.MONTH, 3);
I use the following code to allocate a Console for a WinForm application. The
I use the following code: - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSHTTPURLResponse *)response {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.