在 C# 和 VB.NET 中開始使用 OCR

Curtis Chau
Curtis Chau
已更新: 2026年8月2日

IronOCR 是一個 C# 軟體庫,可讓 .NET 平台軟體開發人員識別和讀取影像和 PDF 文件中的文字。 這是一個純 .NET OCR 函式庫,使用已知最先進的 Tesseract 引擎。

安裝

透過NuGet包管理器安裝

在Visual Studio或使用NuGet包管理器命令行安裝IronOCR。 在 Visual Studio 中,使用下列命令導覽至控制台:

  • 工具 ->
  • NuGet 套件管理器 ->
  • 軟體套件管理器控制台

PM > Install-Package IronOcr

並查看NuGet上的IronOCR以了解更多關於版本更新和安裝的資訊。

還有其他適用於不同平台的IronOCR NuGet 套件:

IronOcr.Extensions.AdvancedScan適用於Linux和macOS

此套件是針對使用Linux和Mac的用戶,並希望獲得IronOCR的高級功能。

故障排除

透過此次軟體包的更新,IronOCR 將 OpenCV 依賴項合併到軟體包中,從而簡化了軟體包。如果目前匯入 OpenCV 相依性的開發人員遇到下列錯誤,則會出現此問題。

The type of namespace name `OpenCvSharp` could not be found(are you missing a using directive or an assembly reference)
Text

您可以安全地移除 OpenCV 命名空間,問題即可解決。

下載 IronOCR .ZIP 文件

您也可以選擇透過 .ZIP 檔案下載 IronOCR。 點擊以直接下載DLL。 下載完 .zip 檔案後:

.NET Framework 4.0+ 安裝說明:

  • 將net40資料夾中的IronOcr.dll包含到您的專案中

  • 然後添加程序集參考至:

    • 系統配置
    • 系統繪圖
    • 系統.Web

適用於 .NET Standard、.NET Core 2.0+ 和 .NET 5 的說明

  • 將netstandard2.0資料夾中的IronOcr.dll包含到您的專案中

  • 然後添加NuGet包參考至:

    • 系統繪圖.Common 4.7 或更高版本

下載 IronOCR 安裝程式(僅限 Windows 系統)

另一個選擇是下載我們的 IronOCR 安裝程序,它將安裝 IronOCR 開箱即用所需的所有資源。 請注意,此選項僅適用於 Windows 系統。 若要下載安裝程式,請點擊此處。 下載完 .zip 檔案後:

.NET Framework 4.0+ 安裝說明:

  • 將net40資料夾中的IronOcr.dll包含到您的專案中

  • 然後添加程序集參考至:

    • 系統配置
    • 系統繪圖
    • 系統.Web

適用於 .NET Standard、.NET Core 2.0+ 和 .NET 5 的說明

  • 將netstandard2.0資料夾中的IronOcr.dll包含到您的專案中

  • 然後添加NuGet包參考至:

    • 系統繪圖.Common 4.7 或更高版本

為什麼選擇 IronOCR?

IronOCR 是一個易於安裝、功能齊全且文件完善的 .NET 軟體庫。

選擇 IronOCR,無需使用任何外部網路服務、無需支付持續費用或透過網路發送機密文件,即可實現99.8% 以上的 OCR 準確率。

為什麼C#開發人員選擇IronOCR而不是Vanilla Tesseract:

  • 可以安裝為單一 DLL 檔案或 NuGet 檔案。
  • 包裝盒內含 Tesseract 5、4 和 3 引擎。
  • 準確率高達99.8% ,遠超普通 Tesseract。
  • 極速和多線程
  • 相容於 MVC、WebApp、桌面、控制台和伺服器應用程式
  • 無需處理任何可執行檔或 C++ 程式碼
  • 完全支援 PDF OCR
  • 對幾乎任何影像檔案或 PDF 檔案執行 OCR 識別
  • 完全支援 .NET Core、Standard 和 Framework
  • 支援部署在 Windows、Mac、Linux、Azure、Docker、Lambda 和 AWS 上
  • 讀取條碼與二維碼
  • 將 OCR 結果匯出為 XHTML
  • 將 OCR 資訊匯出為可搜尋的 PDF 文檔
  • 多線程支援
  • 125 種國際語言,全部透過 NuGet 或 OcrData 檔案進行管理
  • 提取圖像、座標、統計資料和字體。 不僅僅是文字。
  • 可用於在商業和專有應用程式中重新分發 Tesseract OCR。

IronOCR 在處理真實世界的影像和不完美的文件(例如照片或低解析度掃描件,這些文件可能有數位雜訊或瑕疵)時表現出色。

其他免費OCR程式庫,例如其他.NET Tesseract API和網路服務,在這些實際使用情境下表現不佳。

使用 Tesseract 5 進行 OCR - 開始以 C# 編寫程式碼

下面的程式碼範例展示了使用 C# 或 VB .NET 從圖像中讀取文字是多麼容易。

一句話

string Text = new IronTesseract().Read(@"img\Screenshot.png").Text;

可配置的 Hello World

using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();

// Add multiple images
input.LoadImage("images/sample.jpeg");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);

C# PDF OCR

同樣的方法也可用於從任何 PDF 文件中提取文字。

using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();

// We can also select specific PDF page numbers to OCR
input.LoadPdf("example.pdf", Password: "password");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);

// 1 page for every page of the PDF
Console.WriteLine($"{result.Pages.Length} Pages");

多頁TIFF檔案的OCR識別

using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("multi-frame.tiff", pageindices);
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);

條碼和二維碼

IronOCR 的一個獨特之處在於,它在掃描文字的同時,還可以讀取文件中的條碼和二維碼。 OcrResult.OcrBarcode類的實例提供開發者關於每個掃描條碼的詳細信息。

using IronOcr;

IronTesseract ocr = new IronTesseract();
ocr.Configuration.ReadBarCodes = true;

using OcrInput input = new OcrInput();
input.LoadImage("img/Barcode.png");

OcrResult Result = ocr.Read(input);
foreach (var Barcode in Result.Barcodes)
{
    // type and location properties also exposed
    Console.WriteLine(Barcode.Value);
}

對影像特定區域進行OCR識別

IronOCR 的所有掃描和讀取方法都允許我們精確指定要從頁面的哪個部分讀取文字。 這在處理標準化表格時非常有用,可以節省大量時間並提高效率。

要使用裁切區域,我們需要添加系統參考至System.Drawing,以便可以使用System.Drawing.Rectangle物件。

using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();

// Dimensions are in pixel
var contentArea = new System.Drawing.Rectangle() { X = 215, Y = 1250, Height = 280, Width = 1335 };

input.LoadImage("document.png", contentArea);

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);

低品質掃描的光學字元識別

IronOCR OcrInput類可以修復普通Tesseract無法讀取的掃描內容。

using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\Potter.tiff", pageindices);

// fixes digital noise and poor scanning
input.DeNoise();

// fixes rotation and perspective
input.Deskew();

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);

將 OCR 結果匯出為可搜尋 PDF

using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Quarterly Report";
input.LoadImage("image1.jpeg");
input.LoadImage("image2.png");
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("image3.gif", pageindices);

OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable.pdf");

TIFF 轉可搜尋 PDF

using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("example.tiff", pageindices);
ocr.Read(input).SaveAsSearchablePdf("searchable.pdf");

將 OCR 結果匯出為 HTML

using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Html Title";
input.LoadImage("image1.jpeg");

OcrResult Result = ocr.Read(input);
Result.SaveAsHocrFile("results.html");

OCR影像增強濾鏡

IronOCR提供獨特的過濾器來OcrInput物件,以提高OCR性能。

影像增強程式碼範例

using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.LoadImage("LowQuality.jpeg");

// fixes digital noise and poor scanning
input.DeNoise();

// fixes rotation and perspective
input.Deskew();

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);

OCR影像濾鏡列表

IronOCR 內建的用於增強 OCR 性能的輸入過濾器包括:

  • OcrInput.Rotate(double degrees) - 順時針旋轉圖片若干度數。若逆時針旋轉,請使用負數。
  • OcrInput.Binarize() - 此過濾器將每個像素轉換為黑色或白色,沒有中間過渡地帶,潛在地提高非常低對比度圖像的OCR性能。
  • OcrInput.ToGrayScale() - 將每個像素轉換為灰階陰影。 它可能不會提高準確率,但可以提高速度。
  • OcrInput.Contrast() - 自動增加對比度,通常提高低對比度掃描的速度和準確性。
  • OcrInput.DeNoise() - 去除數位噪音,僅當預期噪音存在時推薦使用。
  • OcrInput.Invert() - 顏色反轉(白色變為黑色,反之亦然)。
  • OcrInput.Dilate() - 增進形態學,向物件邊界添加像素,與侵蝕相反。
  • OcrInput.Erode() - 增進形態學,從物件邊界移除像素,與膨脹相反。
  • OcrInput.Deskew() - 旋轉圖片以正確定位。 由於 Tesseract 的傾斜容差有限,因此這種方法很有用。
  • OcrInput.EnhanceResolution - 增強低品質圖片的解析度。 此設定通常用於自動管理低 DPI 輸入。
  • EnhanceResolution偵測低解析度圖片(低於275 dpi),提升其解析度並銳化文字以獲得更好的OCR效果。 雖然耗時,但通常可以減少 OCR 的整體操作時間。
  • Language - 支援從22個國際語言包中選擇。
  • Strategy - 根據單詞的統計關係允許在快速且不太準確或進階(使用AI增強準確性)策略之間選擇。
  • ColorSpace - 選擇以灰階或彩色進行OCR; 雖然在某些對比度場景下彩色顯示效果可能更好,但灰階顯示通常是最佳選擇。
  • DetectWhiteTextOnDarkBackgrounds - 調整負片圖片,自動偵測和讀取深色背景上的白色文字。
  • InputImageType - 指導OCR程式庫,指定其是在處理完整文件還是片段。
  • RotateAndStraighten - 允許IronOCR妥善處理被旋轉或受透視失真影響的文件。
  • ReadBarcodes - 在文本掃描的同時自動讀取條碼和QR碼,幾乎不增加時間。
  • ColorDepth - 決定OCR過程中顏色深度的每像素位元數。 更深的加工深度可以提高質量,但也會延長加工時間。

125 種語言包

IronOCR透過語言包支援125種國際語言,這些語言包以DLL形式分發,可從本網站下載,或從NuGet包管理器獲得。

語言包括德語、法語、英語、中文、日語等。 專業語言包用於MRZ、MICR支票、金融數據、車牌等。此外,可以使用自定義Tesseract ".traineddata" 文件。

語言範例

using IronOcr;

// PM> Install IronOcr.Languages.Arabic
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.Arabic;

using OcrInput input = new OcrInput();

var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("img/arabic.gif", pageindices);
// Add image filters if needed
// In this case, even thought input is very low quality
// IronTesseract can read what conventional Tesseract cannot.
OcrResult result = ocr.Read(input);
// Console can't print Arabic on Windows easily.
// Let's save to disk instead.
result.SaveAsTextFile("arabic.txt");

多語言範例

同時使用多種語言進行OCR辨識也是可行的。這可以增強對Unicode文件中英文元資料和URL的OCR辨識效果。

using IronOcr;

// PM> Install IronOcr.Languages.ChineseSimplified
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.ChineseSimplified;

// We can add any number of languages
ocr.AddSecondaryLanguage(OcrLanguage.English);

using OcrInput input = new OcrInput();
input.LoadPdf("multi-language.pdf");
OcrResult result = ocr.Read(input);
result.SaveAsTextFile("results.txt");

詳細的OCR結果對象

IronOCR 為每次操作傳回一個 OCR 結果物件。 通常,開發者訪問Text屬性以獲取掃描的文本。 但是,結果物件包含更詳細的資訊。

using IronOcr;

IronTesseract ocr = new IronTesseract();

// Must be set to true to read barcode
ocr.Configuration.ReadBarCodes = true;
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\sample.tiff", pageindices);

OcrResult result = ocr.Read(input);
var pages = result.Pages;
var words = pages[0].Words;
var barcodes = result.Barcodes;
// Explore here to find a massive, detailed API:
// - Pages, Blocks, Paraphaphs, Lines, Words, Chars
// - Image Export, Fonts Coordinates, Statistical Data, Tables

表現

IronOCR開箱即用,無需性能調優或影像修改。

速度快速:IronOCR.2020+ 的速度比以前的版本快 10 倍,錯誤率降低 250% 以上。

了解更多

要了解更多關於C#、VB、F#或其他.NET語言的OCR資訊,請閱讀我們的社群教程,其中提供了使用IronOCR的實際範例並展示了優化程式庫的細微差異。

也有提供給 .NET 開發人員的完整API 參考。

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

閱讀更多

準備好開始了嗎?

NuGet 下載次數 6,411,693版本:2026.11剛剛發布