使用舊版的 System.Drawing 和 IronOCR
.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)將會拋出一個"cannot convert from System.Drawing.Image to SixLabors.ImageSharp.Image"錯誤。

可能的解決方案
-
將System.Drawing.Common更新到目前支援的版本。 舊版本的System.Drawing不再受支援,可能包含漏洞代碼。
-
使用
OcrInput.LoadImage載入圖像,該方法接受檔案路徑byte[]、Stream或IronSoftware.Drawing.AnyBitmap。 在當前的IronOCR中,OcrInput有一個無參數的構造函數,並在內部處理圖像加載,因此您不需要SixLabors.ImageSharp或System.Drawing來準備輸入。using IronOcr; class Program { static void Main() { var Ocr = new IronTesseract(); // Load the image directly with IronOCR using (var Input = new OcrInput()) { Input.LoadImage("image.jpg"); // Perform OCR on the input var Result = Ocr.Read(Input); // Print the recognized text Console.WriteLine(Result.Text); } } }Imports IronOcr Class Program Shared Sub Main() Dim Ocr = New IronTesseract() ' Load the image directly with IronOCR Using Input = New OcrInput() Input.LoadImage("image.jpg") ' Perform OCR on the input Dim Result = Ocr.Read(Input) ' Print the recognized text Console.WriteLine(Result.Text) End Using End Sub End ClassImports IronOcr Class Program Shared Sub Main() Dim Ocr = New IronTesseract() ' Load the image directly with IronOCR Using Input = New OcrInput() Input.LoadImage("image.jpg") ' Perform OCR on the input Dim Result = Ocr.Read(Input) ' Print the recognized text Console.WriteLine(Result.Text) End Using End Sub End Class -
上述程式碼初始化
IronTesseract的實例,使用OcrInput.LoadImage從檔案載入圖像,然後使用IronOCR處理圖像以提取文本。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。