Utilisation d'anciennes versions de System.Drawing avec le logiciel OCR IronOCR
Les projets .NET 4.6.1 à .NET 4.8 utilisant ce logiciel OCR intègrent une 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.
La tentative d'instanciation d'OcrInput à partir de System.Drawing.Image générera l'erreur suivante : " IronOcr.Exceptions.IronOcrProductException : 'Impossible d'analyser l'objet [] comme un fichier image valide.' ". Cela s'explique par le fait qu'IronOcr n'a pas pu reconnaître System.Drawing.Image comme un type d'entrée valide.

Toute tentative de spécification d'un type d'entrée Image tel que OcrInput(Image: image) générera une erreur " impossible de convertir de System.Drawing.Image en SixLabors.ImageSharp.Image ".

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.
OcrInputpeut être instancié avec le typeSixLabors.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
- Le code ci-dessus initialise une instance de
IronTesseract, charge une image à partir d'un fichier en utilisantSixLabors.ImageSharp, puis traite l'image avec IronOCR pour extraire le texte.

