如何使用IronOCR在C#中讀取圖像
IronOCR使用光學字元辨識技術從影像中提取文字,支持JPG、PNG、GIF、TIFF和BMP格式。 安裝 NuGet 套件後,基本的文字擷取只需要一行程式碼。
OCR(光學字元辨識)技術可從影像中辨識並擷取文字。 它透過從掃描的頁面、照片或其他影像檔案中擷取文字內容,將印刷文件數位化。 IronOCR使用來自Tesseract 5的先進機器學習演算法,結合專有的影像預處理技術,以達到高度準確性。
該程式庫支持jpg、png、gif、tiff和bmp格式。 圖片濾鏡可透過自動修正常見的品質問題來增強閱讀能力。 IronOCR將Tesseract 5與先進的預處理技術結合起來,可以在不同的影像質量和格式下提供準確的結果,無論是高解析度的掃描還是壓縮的網路影像。
用一行代碼從圖像中萃取文字。 此範例載入影像並使用Read方法在IronTesseract上讀取其文字。 該函式庫可自動處理影像預處理和文字擷取。
-
1使用 IronOCR 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
var result = new IronTesseract().Read(new OcrImageInput("Potter.png"));Dim result = (New IronTesseract()).Read(New OcrImageInput("Potter.png"))Dim result = (New IronTesseract()).Read(New OcrImageInput("Potter.png")) -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronOCR,並使用 免費試用
最小工作流程(5 個步驟)
- 下載用於讀取影像的C#程式庫
- 支援 jpg、png、gif、tiff 及 bmp 格式的圖片
- 實例化OcrImageInput類別以輸入影像
- 使用
Read方法對輸入影像執行 OCR 操作 - 指定作物區域以定義讀取區域
如何使用IronOCR讀取影像?
首先實例化IronTesseract類。 使用'using'語句創建一個OcrImageInput物件,並指定影像文件路徑。 這可確保妥善的資源處理。 IronOCR 支援 jpg、png、gif、tiff 和 bmp 格式。 使用Read方法執行OCR。 圖庫會自動偵測影像格式,並應用適當的預處理。
對於新用戶,請參閱Windows安裝指南或探索NuGet套件選項。 對於跨平台開發,請查看Linux設置或macOS安裝。
/* :path=/static-assets/ocr/content-code-examples/how-to/input-images-read.cs */
using IronOcr;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("Potter.png");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Display the extracted text
Console.WriteLine(ocrResult.Text);
// Get confidence level
double confidence = ocrResult.Confidence;
Console.WriteLine($"Confidence: {confidence}%");/* :path=/static-assets/ocr/content-code-examples/how-to/input-images-read.cs */
using IronOcr;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("Potter.png");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Display the extracted text
Console.WriteLine(ocrResult.Text);
// Get confidence level
double confidence = ocrResult.Confidence;
Console.WriteLine($"Confidence: {confidence}%");Imports IronOcr
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add image
Using imageInput As New OcrImageInput("Potter.png")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Display the extracted text
Console.WriteLine(ocrResult.Text)
' Get confidence level
Dim confidence As Double = ocrResult.Confidence
Console.WriteLine($"Confidence: {confidence}%")
End UsingImports IronOcr
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add image
Using imageInput As New OcrImageInput("Potter.png")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Display the extracted text
Console.WriteLine(ocrResult.Text)
' Get confidence level
Dim confidence As Double = ocrResult.Confidence
Console.WriteLine($"Confidence: {confidence}%")
End Using
訪問如何讀取多幀/頁的GIF和TIFF以閱讀TIFF和GIF影像。 對於多頁面,請參閱多頁TIFF處理範例。
為什麼置信度水平很重要?
置信度表示 IronOCR 對於擷取文字精確度的肯定程度。 85% 以上的值通常表示結果可靠。 分數較低者可能需要影像預處理或手動審查。 使用信心分數自動標記文件以進行人工驗證或觸發額外的圖像優化濾鏡。
什麼時候應該使用不同的影像格式?
PNG 和 TIFF 格式因無損壓縮而提供最佳的 OCR 效果。 單頁文件使用 PNG,多頁掃描則使用 TIFF。 JPEG 適用於照片,但可能會產生壓縮假象。 BMP 提供未壓縮的品質,但檔案大小較大。 GIF 適合色彩有限的簡單圖形。 了解更多格式特定的優化。
常見的圖像讀取錯誤是什麼?
常見的錯誤包括影像解析度低(低於 200 DPI)、文字歪斜、對比度差或不支援的語言。 IronOCR 提供許多問題的自動修正功能,但嚴重的問題可能需要手動預先處理。 查看我們的故障排除指南以獲得解決方案。
如何將影像以位元組導入?
OcrImageInput類別接受影像作為檔案路徑、位元組、AnyBitmap、Stream或Image對象。 AnyBitmap是一個來自IronSoftware.Drawing.AnyBitmap的位元圖物件。 這種靈活性使得能夠簡單地與各種資料源整合,包括資料庫、網路API和雲存儲。
這種靈活性有助於處理資料庫、Web 服務或記憶體串流的影像。 對於高級流處理,請參考具有輸入流的OCR。 System.Drawing整合指南提供了對舊代碼兼容性的其他示例。
using IronOcr;
using System.IO;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Read byte from file
byte[] data = File.ReadAllBytes("Potter.tiff");
// Import image byte
using var imageInput = new OcrImageInput(data);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);using IronOcr;
using System.IO;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Read byte from file
byte[] data = File.ReadAllBytes("Potter.tiff");
// Import image byte
using var imageInput = new OcrImageInput(data);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);Imports IronOcr
Imports System.IO
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Read byte from file
Dim data As Byte() = File.ReadAllBytes("Potter.tiff")
' Import image byte
Using imageInput As New OcrImageInput(data)
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
End UsingImports IronOcr
Imports System.IO
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Read byte from file
Dim data As Byte() = File.ReadAllBytes("Potter.tiff")
' Import image byte
Using imageInput As New OcrImageInput(data)
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
End Using什麼時候應該選擇使用位元組陣列而不是文件路徑?
當影像來自資料庫、Web 服務或加密來源時,位元組陣列的效果最佳。 它們提供更好的安全性,因為檔案不需要暫存磁碟。 在雲端應用程式、微服務或處理敏感文件時使用位元組陣列。 檔案路徑在本機批次處理大型影像集時仍能保持較高的效率。
using IronOcr;
using IronSoftware.Drawing;
using System;
using System.IO;
// Method 1: From URL
var imageFromUrl = AnyBitmap.FromUri(new Uri("https://example.com/document.jpg"));
using var urlInput = new OcrImageInput(imageFromUrl);
// Method 2: From Stream
using var fileStream = File.OpenRead("document.png");
using var streamInput = new OcrImageInput(fileStream);
// Method 3: From System.Drawing (with IronSoftware.Drawing)
var bitmap = AnyBitmap.FromFile("scan.bmp");
using var bitmapInput = new OcrImageInput(bitmap);
// Process any of these inputs
IronTesseract ocr = new IronTesseract();
OcrResult result = ocr.Read(bitmapInput);using IronOcr;
using IronSoftware.Drawing;
using System;
using System.IO;
// Method 1: From URL
var imageFromUrl = AnyBitmap.FromUri(new Uri("https://example.com/document.jpg"));
using var urlInput = new OcrImageInput(imageFromUrl);
// Method 2: From Stream
using var fileStream = File.OpenRead("document.png");
using var streamInput = new OcrImageInput(fileStream);
// Method 3: From System.Drawing (with IronSoftware.Drawing)
var bitmap = AnyBitmap.FromFile("scan.bmp");
using var bitmapInput = new OcrImageInput(bitmap);
// Process any of these inputs
IronTesseract ocr = new IronTesseract();
OcrResult result = ocr.Read(bitmapInput);Imports IronOcr
Imports IronSoftware.Drawing
Imports System
Imports System.IO
' Method 1: From URL
Dim imageFromUrl = AnyBitmap.FromUri(New Uri("https://example.com/document.jpg"))
Using urlInput As New OcrImageInput(imageFromUrl)
' Method 2: From Stream
Using fileStream = File.OpenRead("document.png")
Using streamInput As New OcrImageInput(fileStream)
' Method 3: From System.Drawing (with IronSoftware.Drawing)
Dim bitmap = AnyBitmap.FromFile("scan.bmp")
Using bitmapInput As New OcrImageInput(bitmap)
' Process any of these inputs
Dim ocr As New IronTesseract()
Dim result As OcrResult = ocr.Read(bitmapInput)
End Using
End Using
End Using
End UsingImports IronOcr
Imports IronSoftware.Drawing
Imports System
Imports System.IO
' Method 1: From URL
Dim imageFromUrl = AnyBitmap.FromUri(New Uri("https://example.com/document.jpg"))
Using urlInput As New OcrImageInput(imageFromUrl)
' Method 2: From Stream
Using fileStream = File.OpenRead("document.png")
Using streamInput As New OcrImageInput(fileStream)
' Method 3: From System.Drawing (with IronSoftware.Drawing)
Dim bitmap = AnyBitmap.FromFile("scan.bmp")
Using bitmapInput As New OcrImageInput(bitmap)
' Process any of these inputs
Dim ocr As New IronTesseract()
Dim result As OcrResult = ocr.Read(bitmapInput)
End Using
End Using
End Using
End Using為什麼記憶體管理對影像位元組很重要?
大型影像會消耗大量記憶體,尤其是同時處理多個文件時。 使用「使用」語句可確保資源處理得宜。 在單一Read呼叫中,設置IronTesseract.MaxDegreeOfParallelism以限制一次處理多頁數量,並降低峰值記憶體。 多執行緒指引詳細介紹了此屬性及相關MultiThreaded設定。
不同輸入類型的性能影響是什麼?
檔案路徑可為本機檔案提供最快的效能,因為 IronOCR 會直接讀取資料。 位元組陣列需要將整個影像載入記憶體,但具有彈性。 Streams 透過增量讀取資料來平衡記憶體使用量與效能。 查看我們的性能調整指南以獲取有關大型批次的最佳性能技巧。
如何指定掃描區域?
在實例化OcrImageInput時傳入CropRectangle以指定要處理的影像區域。 限制掃描範圍可大幅提升效能。 以下範例僅讀出章節編號和標題。 這種技術在專注於特定的文件區域時,可以減少處理時間。
對於複雜的佈局或多個區域,請參考影像OCR區域。 內容區域指南解釋了高級區域選擇技術。
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);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
Dim ocrTesseract As New IronTesseract()
' Specify crop region
Dim scanRegion As New Rectangle(800, 200, 900, 400)
' Add image
Using imageInput As New OcrImageInput("Potter.tiff", ContentArea:=scanRegion)
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Output the result to console
Console.WriteLine(ocrResult.Text)
End UsingImports IronOcr
Imports IronSoftware.Drawing
Imports System
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Specify crop region
Dim scanRegion As New Rectangle(800, 200, 900, 400)
' Add image
Using imageInput As New OcrImageInput("Potter.tiff", ContentArea:=scanRegion)
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Output the result to console
Console.WriteLine(ocrResult.Text)
End Using為什麼指定區域可以提高性能?
僅處理相關的影像區域可以減少計算負擔。 OCR 引擎會分析輸入區域中的每個像素,因此較小的區域意味著較快的處理速度。 此方法還可消除目標文字區域以外的頁眉、頁腳或裝飾元素的潛在干擾,從而提高精確度。

什麼時候應該使用多個掃描區域?
對於具有不同文字區域的文件,如表格、發票或多列版面,請使用多重區域。 每個區域分開處理,以維持邏輯的文字流程。 此方法適用於提取表格數據或從結構化文件中讀取特定字段。
座標系統的約定是什麼?
IronOCR 使用標準像素座標,原點 (0,0) 位於左上角。 X 向右增加,Y 向下增加。 矩形參數為 (X、Y、寬度、高度)。 若要精確選擇區域,請使用影像編輯工具來識別像素座標,或在應用程式中實作可視化的區域選擇器。
如何應用高級影像處理?
IronOCR 提供全面的影像預處理功能,以提升 OCR 精確度。 在處理低品質影像、掃描文件或具有挑戰性的條件時,應用篩選條件。 濾鏡精靈幫助您確定針對特定影像的最佳濾鏡組合。
using IronOcr;
IronTesseract ocr = new IronTesseract();
using var input = new OcrImageInput("low-quality-scan.jpg");
// Apply image enhancement filters
input.Deskew(); // Correct image rotation
input.DeNoise(); // Remove background noise
input.Binarize(); // Convert to black and white
input.EnhanceResolution(300); // Adjust DPI for better accuracy
// Configure for better accuracy
ocr.Configuration.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
ocr.Language = OcrLanguage.English;
OcrResult result = ocr.Read(input);using IronOcr;
IronTesseract ocr = new IronTesseract();
using var input = new OcrImageInput("low-quality-scan.jpg");
// Apply image enhancement filters
input.Deskew(); // Correct image rotation
input.DeNoise(); // Remove background noise
input.Binarize(); // Convert to black and white
input.EnhanceResolution(300); // Adjust DPI for better accuracy
// Configure for better accuracy
ocr.Configuration.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
ocr.Language = OcrLanguage.English;
OcrResult result = ocr.Read(input);Imports IronOcr
Dim ocr As New IronTesseract()
Using input = New OcrImageInput("low-quality-scan.jpg")
' Apply image enhancement filters
input.Deskew() ' Correct image rotation
input.DeNoise() ' Remove background noise
input.Binarize() ' Convert to black and white
input.EnhanceResolution(300) ' Adjust DPI for better accuracy
' Configure for better accuracy
ocr.Configuration.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
ocr.Language = OcrLanguage.English
Dim result As OcrResult = ocr.Read(input)
End UsingImports IronOcr
Dim ocr As New IronTesseract()
Using input = New OcrImageInput("low-quality-scan.jpg")
' Apply image enhancement filters
input.Deskew() ' Correct image rotation
input.DeNoise() ' Remove background noise
input.Binarize() ' Convert to black and white
input.EnhanceResolution(300) ' Adjust DPI for better accuracy
' Configure for better accuracy
ocr.Configuration.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
ocr.Language = OcrLanguage.English
Dim result As OcrResult = ocr.Read(input)
End Using了解影像優化篩選和修正低質量掃描。 如需顏色校正需求,請參見影像顏色校正指南。
什麼時候應用影像預處理篩選?
在處理掃描文件、文字照片或有品質問題的影像時,請套用篩選條件。 常見的情況包括修正歪斜的頁面、移除影印本的背景雜訊或強化褪色的文字。 DPI設置指南有助於優化解析度相關問題。
為什麼濾鏡順序很重要?
篩選順序對結果有顯著的影響。 首先應用旋轉校正 (Deskew),接著是去噪,再增強對比度。 二值化通常應置於最後。不正確的順序可能放大問題,例如,先銳化後去噪會增加噪音的可見性。 測試不同的順序以獲得最佳效果。
常見的預處理錯誤是什麼?
過度加工是最常見的錯誤。 過度的銳化會造成假象,強烈的去噪會移除精細的文字細節,而不當的二值化臨界值會遺失資訊。 從最少的預處理開始,僅在需要時加入篩選條件。 影像質量校正指南提供了詳細的最佳實踐。
如何優化性能?
在處理多個影像或大批量時,請考慮這些最佳化:
- 重用
IronTesseract實例:針對多個操作創建一個實例 - 指定掃描區域:限制OCR於相關影像區域以獲得性能提升
- 使用合適的影像格式:PNG和TIFF比JPEG提供更好的結果
- 選擇性地應用預處理:僅在需要時使用篩選
- 實現並行處理:利用多核心CPU進行批量操作,當記憶體有限時,使用
MaxDegreeOfParallelism加以限制
關於高性能場景,請參閱多執行緒指南和快速OCR配置。 進度追蹤功能幫助監控長時間運行的操作。
為什麼重用實例可以提高性能?
IronTesseract初始化加載語言數據並配置OCR引擎,這增加了可測量的啟動負擔。 重複使用實例可消除後續作業的這項開銷。 為網路應用程式建立單一實體,或為批次處理建立共用實體,以達到效率最大化。
什麼時候應該使用並行處理?
並行處理有利於具有多個獨立影像的場景。 同時處理不同的頁面或文件,但避免在同一影像上進行平行作業。 現代CPU可以處理多個同時運行的OCR操作。 監控記憶體使用情況,因為每個操作佔用的記憶體取決於影像大小,並使用IronTesseract.MaxDegreeOfParallelism限制IronOCR在單一Read中的並發性。
記憶體使用的考量是什麼?
OCR操作通常需要數倍於影像檔案大小的RAM進行處理,因為每個並行讀取的頁面使用其自身的原生Tesseract引擎。設定MaxDegreeOfParallelism為較低值,或將MultiThreaded設為false以進行順序讀取,以便在不撰寫自身節流的情況下限制該內存佔用。 如果您也從自己的代碼中同時分派文件,同樣也限制外部的並發量。 中止令牌範例展示了如何取消記憶體密集的操作。
下一步是什麼?
利用這些資源從更複雜的情境中提取文字:
- 從PDF中讀取文本 - 使用OCR處理PDF文件
- 從截圖中提取數據 - 捕獲並讀取螢幕內容
- 處理掃描的文件 - 處理多頁掃描文件
- 使用System.Drawing物件 - 與現有的.NET影像代碼整合
- 讀取多種語言 - 提取超過125種語言的文本
- 處理特定的文件類型 - 優化護照、發票等
常見問題
IronOCR支持哪些圖像格式的文本擷取?
IronOCR支持從各種圖片格式中擷取文本,包括JPG、PNG、GIF、TIFF和BMP。
IronOCR如何利用機器學習進行OCR?
IronOCR結合了Tesseract 5的先進機器學習算法和專利影像預處理技術,以提高不同影像質量和格式的準確性。
我可以用IronOCR處理多頁TIFF和GIF文件嗎?
可以,IronOCR可以處理多頁的TIFF和GIF文件。它可以高效率地處理每一頁以準確擷取文本。
為什麼信心水準在OCR中很重要?
信心水準顯示了IronOCR對擷取文本準確性的確定程度。超過85%的信心水準通常表示結果可靠,而較低的分數可能需要額外的人工審核。
如何優化IronOCR的圖像處理性能?
您可以通過指定掃描範圍、重用IronTesseract實例、利用合適的圖像格式、選擇性地應用預處理,並利用並行處理進行批量操作來優化性能。
使用位元組陣列作為影像輸入的優勢是什麼?
位元組陣列對於從資料庫、網路服務或加密來源獲取的圖像有益,因其能避免臨時磁碟存儲,提供增強的安全性。它們在雲應用或安全環境中處理時效果最佳。
什麼時候應用圖像預處理濾鏡?
對於具有質量問題的圖像,如傾斜頁面、背景噪音或褪色文本,應用例如去傾斜、去噪和二值化的預處理濾鏡,以提高OCR準確性。
指定掃描範圍如何改善OCR性能?
指定掃描範圍將OCR處理限制於圖像的相關部分,減少計算負擔和處理時間,並通過僅聚焦於目標文本區域來提高準確性。
使用IronOCR時應遵循哪些記憶體管理實踐?
在處理大圖像時使用'using'語句釋放資源,實施隊列系統進行批量處理,並監控記憶體使用以同時處理多份文件。
為什麼我要使用並行處理進行OCR任務?
並行處理適於處理多個獨立的圖像文件,讓您能最大化CPU使用並加速批量操作,同時管理記憶體限制。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。