How to load ‘System.Drawing.Image’ C# object to FCEngine

It is possible to load the ‘System.Drawing.Image’ C# object to the FCEngine.ImageFile.
See the code snippet with comments below as an example:

// This is how System.Drawing.Image is stored to MemoryStream.


// Pass the System.Drawing.Image as a parameter into RescaleAndRecognize function below:
private IDocument RescaleAndRecognize( System.Drawing.Image srcImage, double scale )
{
  // get the height and width from System.Drawing.Image with scale
  int newWidth = ( int )Math.Round( srcImage.Width * scale );
  int newHeight = ( int )Math.Round( srcImage.Height * scale );
  // create a new Bitmap with the height and width from scaled System.Drawing.Image
  using( var newImage = new Bitmap( newWidth, newHeight ) ) {
    using( var g = Graphics.FromImage( newImage ) ) {
      g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
      // draw the new image from System.Drawing.Image with scaled width and height
      g.DrawImage( srcImage, new Rectangle( 0, 0, newWidth, newHeight ) );
    }
    // set new image resolution
    newImage.SetResolution( 300, 300 );
    // save the new image in MemoryStream
    using( var stream = new System.IO.MemoryStream() ) {
      newImage.Save( stream, System.Drawing.Imaging.ImageFormat.Bmp );
      stream.Seek( 0, System.IO.SeekOrigin.Begin );
      return RecognizeFromMemoryStream( stream );
    }
  }
}

Was this article helpful?

0 out of 0 found this helpful

Have more questions? Submit a request

Recently viewed