使用舊版的 System.Drawing 和 IronOCR

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 可以使用SixLabors.ImageSharp.Image類型進行實例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
  • 上述程式碼初始化IronTesseract實例,使用SixLabors.ImageSharp從檔案載入影像,然後使用 IronOCR 處理影像以擷取文字。
Curtis Chau
技術撰稿人

Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。

準備好開始了嗎?
Nuget 下載 5,384,824 | 版本: 2026.2 剛剛發布