如何從C#中的System.Drawing物件讀取
IronOCR通過將OcrImageInput中,為.NET應用程式提供無縫的OCR功能,適用於Windows、macOS和Linux平台。
System.Drawing.Bitmap是.NET Framework中的一個類,用於處理點陣圖影像。 它提供建立、操作和顯示點陣圖影像的方法和屬性。
System.Drawing.Image是.NET Framework中所有GDI+影像物件的基礎類。 它是各種影像型別的父類,包括System.Drawing.Bitmap。
IronSoftware.Drawing.AnyBitmap是IronDrawing中的一個點陣圖類,這是一個由Iron Software開發的開源程式庫。 它幫助C#軟體工程師替換.NET專案中的System.Drawing.Common,適用於Windows、macOS和Linux平台。
快速入門:從System.Drawing.Bitmap讀取文字
透過單一語句建立OcrImageInput封裝,提取所有文字。 這個快速入門範例展示了IronOCR如何在最小設置下將影像轉換為可讀取文字。
如何從C#中的System.Drawing物件讀取
- 下載用於讀取System.Drawing物件的C#程式庫
- 獲取System.Drawing物件,如
Bitmap和Image - 使用獲取的資料構造
OcrImageInput類 - 在Linux和macOS上使用Iron Software的
AnyBitmap - 通過指定裁剪區域來定義讀取區域
如何從System.Drawing.Bitmap讀取?
首先,實例化IronTesseract類以執行OCR。 從多種方法之一建立System.Drawing.Bitmap。 在程式碼範例中,使用了檔案路徑。
接下來,使用System.Drawing.Bitmap物件的影像傳遞給它。 最後,使用Read方法執行OCR。
:path=/static-assets/ocr/content-code-examples/how-to/input-system-drawing-read-bitmap.cs
using IronOcr;
using System.Drawing;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Read image file to Bitmap
Bitmap bitmap = new Bitmap("Potter.tiff");
// Import System.Drawing.Bitmap
using var imageInput = new OcrImageInput(bitmap);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
Imports IronOcr
Imports System.Drawing
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Read image file to Bitmap
Private bitmap As New Bitmap("Potter.tiff")
' Import System.Drawing.Bitmap
Private imageInput = New OcrImageInput(bitmap)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
為什麼using語句對OcrImageInput很重要?
在處理IDisposable,這意味著它持有未管理的資源,這些資源需要在用完物件後釋放。 如果沒有using語句,這些資源可能無法及時釋放,可能導致記憶體洩漏或檔案鎖定。 在批次處理多張影像時,這一點尤為重要。 有關IronOCR中正確資源管理的更多詳細資訊,請參閱我們的API參考資料。
常見的Bitmap載入方法有哪些?
Bitmap除了我們範例中使用的檔案路徑構造函式之外,還提供了幾種載入方法。 您可以從流(Bitmap)。 在處理網頁應用程式時,從流載入在處理上傳的檔案時特別有用。 對於嵌入的資源,您可以使用Resources。 IronOCR通過source來源。 有關不同輸入方法的更多資訊,請參閱我們的影像(jpg, png, gif, tiff, bmp)指南。
我應該何時處理Bitmap物件?
Bitmap的處理時機取決於您的應用程式工作流程。 如果您僅需ocrResult之後立即處理它。 不過,如果您需要執行多次操作或顯示影像,應保持它的存活至所有操作完成。 始終使用using語句或try-finally塊來確保處理。 請記得bitmap。 對於涉及多次影像操作的複雜場景,請考慮我們的OCR影像優化濾鏡範例。
如何從System.Drawing.Image讀取?
從Read方法進行標準的OCR過程。
:path=/static-assets/ocr/content-code-examples/how-to/input-system-drawing-read-image.cs
using IronOcr;
using Image = System.Drawing.Image;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Open image file as Image
Image image = Image.FromFile("Potter.tiff");
// Import System.Drawing.Image
using var imageInput = new OcrImageInput(image);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
Imports IronOcr
Imports Image = System.Drawing.Image
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Open image file as Image
Private image As Image = Image.FromFile("Potter.tiff")
' Import System.Drawing.Image
Private imageInput = New OcrImageInput(image)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
影像和點陣圖在OCR中的區別是什麼?
雖然Image是一個抽象基類,可以表示各種影像格式,包括JPEG、PNG、GIF和TIFF。 對於OCR目的,IronOCR通過Image在處理不同格式時提供了更多的靈活性。 Image更適合一般影像處理。 這兩者均能與IronOCR的先進Tesseract 5引擎良好合作。選擇取決於您的應用程式需求而非OCR效能。
為什麼使用Image.FromFile而非其他載入方法?
Image.FromFile是從磁碟載入影像最簡單且最直接的方法。 它會自動檢測影像格式並處理檔案讀取過程。 其他方法如MemoryStream更適用於網頁應用程式或處理記憶體流時。 FileStream被處理,這在多執行緒應用程式中可能需要考量。 對於需要高效能或並發存取的生產場景,考慮首先將影像載入記憶體流。 我們的多執行緒Tesseract OCR範例展示了並發影像處理的最佳實踐。
如何從IronSoftware.Drawing.AnyBitmap讀取?
同樣,在建立或獲得OcrInput類。 構造函式會處理所有必要的步驟以導入資料。 下面的程式碼範例展示了這一點。
:path=/static-assets/ocr/content-code-examples/how-to/input-system-drawing-read-anybitmap.cs
using IronOcr;
using IronSoftware.Drawing;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Open image file as AnyBitmap
AnyBitmap anyBitmap = AnyBitmap.FromFile("Potter.tiff");
// Import IronSoftware.Drawing.AnyBitmap
using var imageInput = new OcrImageInput(anyBitmap);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
Imports IronOcr
Imports IronSoftware.Drawing
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Open image file as AnyBitmap
Private anyBitmap As AnyBitmap = AnyBitmap.FromFile("Potter.tiff")
' Import IronSoftware.Drawing.AnyBitmap
Private imageInput = New OcrImageInput(anyBitmap)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
為什麼選擇AnyBitmap而非System.Drawing類?
Bitmap類提供了更強的跨平台相容性。 雖然Bitmap在.NET 6+的非Windows平台上支援有限,但AnyBitmap在Windows、Linux和macOS上都運行順暢。 它提供一致的API,沒有平台特定的依賴項,非常適合雲部署及容器化應用程式。 AnyBitmap還提供了專為影像處理任務設計的更好的記憶體管理和效能優化。 有關詳細的相容性資訊,請參閱我們的相容性文件。
AnyBitmap支援哪些平台?
AnyBitmap支援所有.NET運行的平台:Windows(x86, x64, ARM)、Linux(包括Docker的Alpine Linux)和macOS(含Intel和Apple Silicon)。 這種廣泛的平台支援使其成為需要在多樣環境中運行的現代.NET應用程式的推薦選擇。 特別適合在AWS Lambda或Azure Functions上的雲部署。 在我們專門針對Linux、macOS和Docker環境的指南中了解更多平台特定的設置。
AnyBitmap如何處理記憶體管理?
AnyBitmap透過自動垃圾回收整合和顯式釋放模式實現高效的記憶體管理。 它使用頻繁分配緩衝區的記憶體池,並實現寫時複製語義以提高效能。 與可能持有檔案鎖的AnyBitmap將影像完全載入記憶體,以防止檔案存取問題。 它還在高吞吐量情景下提供更好的記憶體使用控制。 對於處理大量影像的應用程式,AnyBitmap的記憶體效率可顯著減少整體記憶體佔用。 請參閱我們的System.Drawing.Common替代方案指南以獲得遷移提示。
如何指定掃描區域?
在OcrInput類的構建中,您可以指定要掃描的區域。 這使您可以定義影像文件的特定區域進行OCR。 根據影像文件來看,指定掃描區域可以顯著提高效能。 在提供的程式碼範例中,只有章節號和標題被提取。
:path=/static-assets/ocr/content-code-examples/how-to/input-images-read-specific-region.cs
using IronOcr;
using IronSoftware.Drawing;
using System;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Specify crop region
Rectangle scanRegion = new Rectangle(800, 200, 900, 400);
// Add image
using var imageInput = new OcrImageInput("Potter.tiff", ContentArea: scanRegion);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Output the result to console
Console.WriteLine(ocrResult.Text);
Imports IronOcr
Imports IronSoftware.Drawing
Imports System
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Specify crop region
Private scanRegion As New Rectangle(800, 200, 900, 400)
' Add image
Private imageInput = New OcrImageInput("Potter.tiff", ContentArea:= scanRegion)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Output the result to console
Console.WriteLine(ocrResult.Text)
我應何時使用區域掃描以獲得更好效能?
當您僅需要文字在一致文件佈局的特定區域時,區域掃描會顯著提高效能。 常見的應用情況包括提取標題、表單字段、發票總金額或ID卡資訊。 對於文字只佔一小部分的大型圖像,效能提升最顯著。 例如,對於3000x4000像素的發票,僅掃描總金額區域可以比全頁OCR快10到20倍。 區域掃描同時通過消除其他區域的潛在噪音提高準確性。 查看我們的內容區域&裁剪區域與PDFs指南,獲得更多基於區域的範例。
如何確定我的區域的正確坐標?
確定坐標需要了解Rectangle使用(X, Y, Width, Height)格式,其中(0,0)為左上角。 首先,在顯示光標坐標的影像編輯器中打開您的影像。 或者,使用IronOCR的除錯功能來可視化檢測到的文字區域。 對於動態佈局,考慮首先使用IronOCR進行全掃描,然後分析OcrResult以程式化地找到文字位置。 我們的除錯時突出顯示文字範例展示了如何可視化OCR區域以進確定坐標準確。
如果區域超出影像邊界會發生什麼?
當指定的區域超出影像邊界時,IronOCR會自動將其裁剪到有效影像區域。 例如,如果您的影像是1000x1000像素,且您指定矩形在(900, 900, 200, 200),IronOCR只會處理從(900, 900)到(1000, 1000)的區域。 這種自動裁剪防止了錯誤,但如果您的坐標不正確可能導致文字提取不完整。 請始終根據實際影像尺寸驗證您的區域。 對於動態影像大小,請將區域計算為百分比,而不是固定像素。 影像OCR區域指南提供了更多安全區域處理的範例。
OCR結果
using
OcrImageInput
OcrImageInput
IDisposable
using
System.Drawing.Bitmap
Bitmaps
new Bitmap(stream)
Images
new Bitmap(image)
new Bitmap(width, height)
Assembly.GetManifestResourceStream()
Bitmap
OcrImageInput
Bitmap
OcrImageInput
using
OcrImageInput
Bitmap
OcrImageInput
System.Drawing.Image
OcrImageInput
Image
Read
System.Drawing.Bitmap
System.Drawing.Image
OcrImageInput
Image
Bitmap
Image
Image.FromFile
Image.FromStream
Image.FromFile
Image
AnyBitmap
OcrImageInput
AnyBitmap
System.Drawing
System.Drawing.Common
AnyBitmap
AnyBitmap
AnyBitmap
AnyBitmap
System.Drawing.Bitmap
AnyBitmap
AnyBitmap
OcrImageInput
Rectangle
OcrResult
常見問題
如何在 C# 中使用 OCR 從 System.Drawing.Bitmap 中提取文字?
IronOCR 使從 System.Drawing.Bitmap 物件中提取文字變得簡單。首先,實例化 IronTesseract 類,然後使用 using 語句將您的 Bitmap 包裝在 OcrImageInput 物件中,最後調用 Read 方法。最少程式碼是:var result = new IronOcr.IronTesseract().Read(new IronOcr.OcrImageInput(bitmap));
為什麼在使用 OcrImageInput 時 using 語句很重要?
using 語句非常重要,因為 OcrImageInput 實現了 IDisposable 並持有需要適當清理的非受控資源。沒有它,您可能會面臨記憶體洩漏或文件鎖定的風險,特別是在處理多個影像時。IronOCR 的 OcrImageInput 需要適當的處置以確保在您的 .NET 應用程式中有效的資源管理。
我可以對 System.Drawing.Image 物件進行 OCR 嗎?
是的,自從 Image 作為 Bitmap 的基類,IronOCR 支持對 System.Drawing.Image 物件進行 OCR。只需像處理 Bitmap 一樣將 Image 物件包裝在 OcrImageInput 中,IronOCR 將順利提取文字,支持 Windows、macOS 和 Linux 平台。
什麼是 IronSoftware.Drawing.AnyBitmap,它與 OCR 有何關聯?
IronSoftware.Drawing.AnyBitmap 是 IronDrawing 中的一個位圖類,是開源程式庫,用於替換 .NET 專案中的 System.Drawing.Common。它提供了對 Windows、macOS 和 Linux 的跨平台相容性,使其在需要一致的影像處理時,與 IronOCR 配合使用在不同操作系統中非常合適。
我可以指定影像的特定區域進行文字提取嗎?
是的,IronOCR 允許您透過指定裁剪區域來定義特定的閱讀區域。此功能使您能夠將 OCR 處理專注於 System.Drawing 物件的特定部分,從而提高效能和精確度,特別是在您只需要影像特定部分的文字時。
IronOCR支援多種語言嗎?
IronOCR支援多種語言,使其成為全球需要不同語言文字識別的應用程式的多功能工具。
IronOCR能整合到現有的應用程式中嗎?
IronOCR被設計成可以輕鬆地整合到現有應用程式中,使用C#允許開發人員以最小的努力為其軟體新增OCR功能。
使用IronOCR進行文件管理的好處是什麼?
使用IronOCR進行文件管理通過將掃描的文件轉換為可搜索和可編輯的文字來簡化工作流程,減少手動資料輸入的需求並提高文件的可存取性。
IronOCR如何提高資料精確性?
IronOCR通過其先進的識別算法和影像校正功能提高資料精確性,確保文字提取過程既可靠又精確。
IronOCR有免費試用版嗎?
有的,Iron Software提供IronOCR的免費試用版,允許使用者在做出購買決定前測試其功能和能力。

