title: How can i use LockBits with a Bitmap to scan for white pixels and then write to a new bitmap all the non white pixels?
body: <p>This is what i did in form1 constructor:</p>
  
  <pre><code>Bitmap bmp2 = new Bitmap(@"e:\result1001.jpg");
  CropImageWhiteAreas.ImageTrim(bmp2);
  bmp2.Save(@"e:\result1002.jpg");
  bmp2.Dispose();
  </code></pre>
  
  <p>The class CropImageWhiteAreas:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Drawing;
  using System.Drawing.Imaging;
  using System.Runtime.InteropServices;
  
  namespace Test
  {
      class CropImageWhiteAreas
      {
  
          public static Bitmap ImageTrim(Bitmap img)
          {
              //get image data
              BitmapData bd = img.LockBits(new Rectangle(Point.Empty, img.Size),
              ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
              int[] rgbValues = new int[img.Height * img.Width];
              Marshal.Copy(bd.Scan0, rgbValues, 0, rgbValues.Length);
              img.UnlockBits(bd);
  
  
              #region determine bounds
              int left = bd.Width;
              int top = bd.Height;
              int right = 0;
              int bottom = 0;
  
              //determine top
              for (int i = 0; i &lt; rgbValues.Length; i++)
              {
                  int color = rgbValues[i] &amp; 0xffffff;
                  if (color != 0xffffff)
                  {
                      int r = i / bd.Width;
                      int c = i % bd.Width;
  
                      if (left &gt; c)
                      {
                          left = c;
                      }
                      if (right &lt; c)
                      {
                          right = c;
                      }
                      bottom = r;
                      top = r;
                      break;
                  }
              }
  
              //determine bottom
              for (int i = rgbValues.Length - 1; i &gt;= 0; i--)
              {
                  int color = rgbValues[i] &amp; 0xffffff;
                  if (color != 0xffffff)
                  {
                      int r = i / bd.Width;
                      int c = i % bd.Width;
  
                      if (left &gt; c)
                      {
                          left = c;
                      }
                      if (right &lt; c)
                      {
                          right = c;
                      }
                      bottom = r;
                      break;
                  }
              }
  
              if (bottom &gt; top)
              {
                  for (int r = top + 1; r &lt; bottom; r++)
                  {
                      //determine left
                      for (int c = 0; c &lt; left; c++)
                      {
                          int color = rgbValues[r * bd.Width + c] &amp; 0xffffff;
                          if (color != 0xffffff)
                          {
                              if (left &gt; c)
                              {
                                  left = c;
                                  break;
                              }
                          }
                      }
  
                      //determine right
                      for (int c = bd.Width - 1; c &gt; right; c--)
                      {
                          int color = rgbValues[r * bd.Width + c] &amp; 0xffffff;
                          if (color != 0xffffff)
                          {
                              if (right &lt; c)
                              {
                                  right = c;
                                  break;
                              }
                          }
                      }
                  }
              }
  
              int width = right - left + 1;
              int height = bottom - top + 1;
              #endregion
  
              //copy image data
              int[] imgData = new int[width * height];
              for (int r = top; r &lt;= bottom; r++)
              {
                  Array.Copy(rgbValues, r * bd.Width + left, imgData, (r - top) * width, width);
              }
  
              //create new image
              Bitmap newImage = new Bitmap(width, height, PixelFormat.Format32bppArgb);
              BitmapData nbd
                  = newImage.LockBits(new Rectangle(0, 0, width, height),
                      ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
              Marshal.Copy(imgData, 0, nbd.Scan0, imgData.Length);
              newImage.UnlockBits(nbd);
  
              return newImage;
          }
  
      }
  }
  </code></pre>
  
  <p>I also tried before it Peter solution.
  In both the result is(This is a screenshot of my facebook after uploaded the image) still the white areas around:</p>
  
  <p><img src="https://i.stack.imgur.com/c43S2.jpg" alt="Facebook Screenshot"></p>
  
  <p>You can the rectangle around the image i just uploaded and see what i mean by white area around.</p>