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

  • SEARCH
  • Home
  • 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 8557855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:40:12+00:00 2026-06-11T15:40:12+00:00

I am trying to increase my image detection class using lockbits, yet this cause

  • 0

I am trying to increase my image detection class using lockbits, yet this cause problems with the code and thus it does not run. How can i go about using lockbits and getpixel at the same time in order to speed up image detection, or can someone show me an alternative which is just as fast?

code:

static IntPtr Iptr = IntPtr.Zero;
    static BitmapData bitmapData = null;
    static public byte[] Pixels { get; set; }
    static public int Depth { get; private set; }
    static public int Width { get; private set; }
    static public int Height { get; private set; }

    static public void LockBits(Bitmap source)

    {
            // Get width and height of bitmap
            Width = source.Width;
            Height = source.Height;

            // get total locked pixels count
            int PixelCount = Width * Height;

            // Create rectangle to lock
            Rectangle rect = new Rectangle(0, 0, Width, Height);

            // get source bitmap pixel format size
            Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);


            // Lock bitmap and return bitmap data
            bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,
                                         source.PixelFormat);

            // create byte array to copy pixel values
            int step = Depth / 8;
            Pixels = new byte[PixelCount * step];
            Iptr = bitmapData.Scan0;

            // Copy data from pointer to array
            Marshal.Copy(Iptr, Pixels, 0, Pixels.Length);

    }


     static public bool SimilarColors(int R1, int G1, int B1, int R2, int G2, int B2, int Tolerance)
    {
        bool returnValue = true;
        if (Math.Abs(R1 - R2) > Tolerance || Math.Abs(G1 - G2) > Tolerance || Math.Abs(B1 - B2) > Tolerance)
        {
            returnValue = false;
        }
        return returnValue;
    }


     public bool findImage(Bitmap small, Bitmap large, out Point location)
     {
         unsafe
         {
             LockBits(small);
             LockBits(large);
             //Loop through large images width
             for (int largeX = 0; largeX < large.Width; largeX++)
             {
                 //And height
                 for (int largeY = 0; largeY < large.Height; largeY++)
                 {
                     //Loop through the small width
                     for (int smallX = 0; smallX < small.Width; smallX++)
                     {
                         //And height
                         for (int smallY = 0; smallY < small.Height; smallY++)
                         {
                             //Get current pixels for both image
                             Color currentSmall = small.GetPixel(smallX, smallY);
                             Color currentLarge = large.GetPixel(largeX + smallX, largeY + smallY);
                             //If they dont match (i.e. the image is not there)

                             if (!colorsMatch(currentSmall, currentLarge))
                                 //Goto the next pixel in the large image

                                 goto nextLoop;
                         }
                     }
                     //If all the pixels match up, then return true and change Point location to the top left co-ordinates where it was found
                     location = new Point(largeX, largeY);
                     return true;
                 //Go to next pixel on large image
                 nextLoop:
                     continue;
                 }
             }
             //Return false if image is not found, and set an empty point
             location = Point.Empty;
             return false;
         }
     }
  • 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-06-11T15:40:13+00:00Added an answer on June 11, 2026 at 3:40 pm

    You wouldn’t want to rely on getPixel() for image processing; it’s okay to make an occasional call to get a point value (e.g. on mouseover), but in general it’s preferable to do image processing in image memory or in some 2D array that you can convert to a Bitmap when necessary.

    To start, you might try writing a method that using LockBits/UnlockBits to extract an array that is convenient to manipulate. Once you’re done manipulating the array, you can write it back to a bitmap using a different LockBits/UnlockBits function.

    Here’s some sample code I’ve used in the past. The first function returns a 1D array of values from a Bitmap. Since you know the bitmap’s width, you can convert this 1D array to a 2D array for further processing. Once you’re done processing, you can call the second function to convert the (modified) 1D array into a bitmap again.

    public static byte[] Array1DFromBitmap(Bitmap bmp){
        if (bmp == null) throw new NullReferenceException("Bitmap is null");
    
        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
        BitmapData data = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
        IntPtr ptr = data.Scan0;
    
        //declare an array to hold the bytes of the bitmap
        int numBytes = data.Stride * bmp.Height;
        byte[] bytes = new byte[numBytes];
    
        //copy the RGB values into the array
        System.Runtime.InteropServices.Marshal.Copy(ptr, bytes, 0, numBytes);
    
        bmp.UnlockBits(data);
    
        return bytes;           
    }
    
    public static Bitmap BitmapFromArray1D(byte[] bytes, int width, int height)
    {
        Bitmap grayBmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
        Rectangle grayRect = new Rectangle(0, 0, grayBmp.Width, grayBmp.Height);
        BitmapData grayData = grayBmp.LockBits(grayRect, ImageLockMode.ReadWrite, grayBmp.PixelFormat);
        IntPtr grayPtr = grayData.Scan0;
    
        int grayBytes = grayData.Stride * grayBmp.Height;
        ColorPalette pal = grayBmp.Palette;
    
        for (int g = 0; g < 256; g++){
            pal.Entries[g] = Color.FromArgb(g, g, g);
        }
    
        grayBmp.Palette = pal;
    
        System.Runtime.InteropServices.Marshal.Copy(bytes, 0, grayPtr, grayBytes);
    
        grayBmp.UnlockBits(grayData);
        return grayBmp;
    }
    

    These methods makes assumptions about the Bitmap pixel format that may not work for you, but I hope the general idea is clear: use LockBits/UnlockBits to extract an array of bytes from a Bitmap so that you can write and debug algorithms most easily, and then use LockBits/UnlockBits again to write the array to a Bitmap again.

    For portability, I would recommend that your methods return the desired data types rather than manipulating global variables within the methods themselves.

    If you’ve been using getPixel(), then converting to/from arrays as described above could speed up your code considerably for a small investment of coding effort.

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

Sidebar

Related Questions

I have this AJAX loader, created using CSS. And I m trying to increase
I am trying to increase the size of the image by 20. So I
I'm currently debugging some fairly complex persistence code, and trying to increase test coverage
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
This is the processed image and I can't increase the bwareaopen() as it won't
I am trying to transfer an image using TCP sockets using linux. I have
I'm trying to increase the row each time around the loop, but it's not
I am trying to upload the image using the webservices and Base64string. But each
I am trying to download an image using a web service. The actual image
I am trying to increase or decrease the displayed texts font. but can only

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.