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.

Error Screenshot

Trying to specify Image input type like OcrInput(Image: image) will throw a "cannot convert from System.Drawing.Image to SixLabors.ImageSharp.Image" error.

Conversion Error Screenshot

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 the SixLabors.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);
        }
    }
}
Imports IronOcr
Imports SixLabors.ImageSharp
Imports SixLabors.ImageSharp.PixelFormats

Friend Class Program
	Shared Sub Main()
		Dim Ocr = New IronTesseract()

		' Load the image using SixLabors.ImageSharp
		Dim image As Image = System.Drawing.Image.Load(Of Rgba32)("image.jpg")

		' Use the image as input for OCR
		Using Input = New OcrInput(image)
			' Perform OCR on the input
			Dim Result = Ocr.Read(Input)

			' Print the recognized text
			Console.WriteLine(Result.Text)
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel
  • The above code initializes an instance of IronTesseract, loads an image from a file using SixLabors.ImageSharp, and then processes the image with IronOCR to extract text.
Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.
Talk to an Expert Five Star Trust Score Rating

Ready to Get Started?

Nuget Passed