Using Older Versions of System.Drawing with IronOCR
.NET 4.6.1 to .NET 4.8 projects come with a built-in System.Drawing version 4.0.0. This version of System.Drawing is out of support and may contain vulnerable code.
Trying to instantiate OcrInput from System.Drawing.Image will throw "IronOcr.Exceptions.IronOcrProductException: 'Unable to parse Object [] as a valid image file.'". This is because IronOcr could not recognize System.Drawing.Image as a valid input type.
Trying to specify Image input type like OcrInput(Image: image)
will throw a "cannot convert from System.Drawing.Image to SixLabors.ImageSharp.Image" error.
Possible Solutions
Update System.Drawing.Common to version 6.0.0. The older version of System.Drawing is out of support and may contain vulnerable code.
- Use SixLabors.ImageSharp version 2.1.3.
OcrInput
can be instantiated with theSixLabors.ImageSharp.Image
type.
using IronOcr;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
class Program
{
static void Main()
{
var Ocr = new IronTesseract();
// Load the image using SixLabors.ImageSharp
Image image = Image.Load<Rgba32>("image.jpg");
// Use the image as input for OCR
using (var Input = new OcrInput(image))
{
// Perform OCR on the input
var Result = Ocr.Read(Input);
// Print the recognized text
Console.WriteLine(Result.Text);
}
}
}
using IronOcr;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
class Program
{
static void Main()
{
var Ocr = new IronTesseract();
// Load the image using SixLabors.ImageSharp
Image image = Image.Load<Rgba32>("image.jpg");
// Use the image as input for OCR
using (var Input = new OcrInput(image))
{
// Perform OCR on the input
var Result = Ocr.Read(Input);
// Print the recognized text
Console.WriteLine(Result.Text);
}
}
}
- The above code initializes an instance of
IronTesseract
, loads an image from a file usingSixLabors.ImageSharp
, and then processes the image with IronOCR to extract text.