title: How to auto crop an image?
body: <p>I am trying to make a scanning application. That application will scan the document and will display image in a picture box. The problem I am facing is image (Documents image which kept in the scanner or say ‘real ’image) is displaying inside another image with some background (That background color is also changing) its look like this image.<br>
  <img src="https://i.stack.imgur.com/wvmQc.jpg" alt="enter image description here"><br>
  I have tried lot of things but nothing giving me a perfect result I tried it with a forge.net. Here is the code I have tried.</p>
  
  <pre><code>public static System.Drawing.Image AforgeAutoCrop(Bitmap selectedImage)
      {
          Bitmap autoCropImage = null;
      try
      {
  
          autoCropImage = selectedImage;
          // create grayscale filter (BT709)
          Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
          Bitmap grayImage = filter.Apply(autoCropImage);
          // create instance of skew checker
          DocumentSkewChecker skewChecker = new DocumentSkewChecker();
          // get documents skew angle
          double angle = skewChecker.GetSkewAngle(grayImage);
          // create rotation filter
          RotateBilinear rotationFilter = new RotateBilinear(-angle);
          rotationFilter.FillColor = Color.White;
          // rotate image applying the filter
          Bitmap rotatedImage = rotationFilter.Apply(grayImage);
          new ContrastStretch().ApplyInPlace(grayImage);
          new Threshold(100).ApplyInPlace(grayImage);
          BlobCounter bc = new BlobCounter();
          bc.FilterBlobs = true;
          // bc.MinWidth = 500;
          //bc.MinHeight = 500;
          bc.ProcessImage(grayImage);
          Rectangle[] rects = bc.GetObjectsRectangles();
          MemoryStream writeName = new MemoryStream();
          if (rects.Length == 0)
          {
              System.Windows.Forms.MessageBox.Show("No rectangle found in image ");
          }
          else if (rects.Length == 1)
          {
              Bitmap cropped = new Crop(rects[0]).Apply(autoCropImage);
              autoCropImage = cropped;
             // pictureBox1.Image = cropped;
          }
          else if (rects.Length &gt; 1)
          {
              // get largets rect
              Console.WriteLine("Using largest rectangle found in image ");
              var r2 = rects.OrderByDescending(r =&gt; r.Height * r.Width).ToList();
              //var r2 = rects.OrderByDescending(r =&gt; r.Height &lt; 1500 &amp;&amp; r.Width &lt; 1000).ToList();
              Bitmap cropped = new Crop(r2[0]).Apply(autoCropImage);
  
              Graphics gr = Graphics.FromImage(cropped);
              gr.DrawRectangles(new Pen(Color.Red), rects);
              autoCropImage = cropped;
             // pictureBox1.Image = cropped;
  
          }
          else
          {
              Console.WriteLine("Huh? on image ");
          }
      }
      catch(Exception ex)
      {
          MessageBox.Show(ex.Message);
      }
  
      return autoCropImage;
      }
  </code></pre>