Using Older Versions of System.Drawing with IronOCR

This article was translated from English: Does it need improvement?
Translated
View the article in English

les projets .NET 4.6.1 à .NET 4.8 intègrent la version 4.0.0 de System.Drawing. Cette version de System.Drawing n'est plus prise en charge et peut contenir du code vulnérable.

En essayant d'instancier OcrInput à partir de System.Drawing.Image, vous obtiendrez "IronOcr.Exceptions.IronOcrProductException : 'Unable to parse Object [] as a valid image file.'". En effet, IronOcr n'a pas pu reconnaître System.Drawing.Image comme un type d'entrée valide.

Capture d'écran d'erreur

Si vous essayez de spécifier le type d'entrée Image comme OcrInput(Image : image), vous obtiendrez une erreur "cannot convert from System.Drawing.Image to SixLabors.ImageSharp.Image".

Capture d'écran d'erreur de conversion

Solutions possibles

  • Mettez à jour System.Drawing.Common vers la version 6.0.0. L'ancienne version de System.Drawing n'est plus prise en charge et peut contenir du code vulnérable.

  • Utilisez SixLabors.ImageSharp version 2.1.3. OcrInput peut être instancié avec le type SixLabors.ImageSharp.Image.
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
  • Le code ci-dessus initialise une instance d'IronTesseract, charge une image à partir d'un fichier à l'aide de SixLabors.ImageSharp, puis traite l'image avec IronOCR pour en extraire le texte.
Curtis Chau
Rédacteur technique

Curtis Chau détient un baccalauréat en informatique (Université de Carleton) et se spécialise dans le développement front-end avec expertise en Node.js, TypeScript, JavaScript et React. Passionné par la création d'interfaces utilisateur intuitives et esthétiquement plaisantes, Curtis aime travailler avec des frameworks modernes ...

Lire la suite
Prêt à commencer?
Nuget Téléchargements 5,044,537 | Version : 2025.11 vient de sortir