在IronOCR中使用舊版本的System.Drawing
This article was translated from English: Does it need improvement?
Translated
View the article in English
.NET 4.6.1至.NET 4.8的專案搭載內建的System.Drawing版本4.0.0。此版本的System.Drawing不再受支援,可能包含漏洞程式碼。
嘗試從System.Drawing.Image實例化OcrInput時,將拋出"IronOcr.Exceptions.IronOcrProductException:'無法將Object []解析為有效的圖像文件。'"。 這是因為IronOCR無法識別System.Drawing.Image作為有效的輸入型別。

嘗試指定圖像輸入型別如OcrInput(Image: image)將拋出"無法從System.Drawing.Image轉換為SixLabors.ImageSharp.Image"錯誤。

可能的解決方案
-
將System.Drawing.Common更新至版本6.0.0。舊版本的System.Drawing已不再受支援且可能包含漏洞程式碼。
- 使用SixLabors.ImageSharp版本2.1.3。可以使用
OcrInput。
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
- 上述程式碼初始化
SixLabors.ImageSharp從文件中載入圖像,然後使用IronOCR處理該圖像以提取文字。
準備開始了嗎?
Nuget 下載 6,136,090 | 版本: 2026.7 剛剛發布

