title: Image processing with lockbits, alternative to getpixel?
body: <p>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?</p>
  
  <p>code:</p>
  
  <pre><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) &gt; Tolerance || Math.Abs(G1 - G2) &gt; Tolerance || Math.Abs(B1 - B2) &gt; 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 &lt; large.Width; largeX++)
               {
                   //And height
                   for (int largeY = 0; largeY &lt; large.Height; largeY++)
                   {
                       //Loop through the small width
                       for (int smallX = 0; smallX &lt; small.Width; smallX++)
                       {
                           //And height
                           for (int smallY = 0; smallY &lt; 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;
           }
       }
  </code></pre>