如何在 C# 中使用輸入影像進行 OCR 處理

How to Read Images

This article was translated from English: Does it need improvement?
Translated
View the article in English

OCR,或稱光學字符識別,是一種用於從圖像中識別和提取文本的技術。 這項技術特別適用於數字化印刷文件,因為它允許您從掃描頁面、照片或其他圖像文件中提取和處理文本內容。

IronOCR 支援多種圖像格式,包括 jpg、png、gif、tiff 和 bmp。 也提供圖像過濾器來增強閱讀能力。

快速入門:使用 IronOCR 讀取圖像文件

只需一行即可使用 IronOCR 從圖像中提取文本。 此範例顯示如何通過幾個簡單步驟加載圖像並使用 IronTesseract 上的 Read 方法快速設置 OCR。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronOCR with NuGet Package Manager

    PM > Install-Package IronOcr

  2. Copy and run this code snippet.

    var result = new IronTesseract().Read(new OcrImageInput("Potter.png"));
  3. Deploy to test on your live environment

    Start using IronOCR in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程(5 步)

  1. 下載用於閱讀圖像的 C# 庫
  2. 支持多種格式的圖像,包括 jpg、png、gif、tiff 和 bmp
  3. 實例化 OcrImageInput 類以輸入圖像
  4. 使用 Read 方法對輸入圖像執行 OCR
  5. 指定裁剪區域以定義閱讀區域


讀取圖像示例

首先實例化 IronTesseract 類以啟用 OCR。 利用 'using' 語句創建 OcrImageInput 對象,指定圖像文件路徑。 這確保當資源不再需要時能正確釋放。 IronOCR 支持多種格式的輸入圖像,包括 jpg、png、gif、tiff 和 bmp。 最後,使用 Read 方法執行 OCR。

請注意 從 2025.6 版開始:

  • 加載 TIFF 圖像現在始終提供更快的性能。
  • 閱讀 TIFF 圖像的性能改進取決於機器的 GPU。 某些用戶可能會看到高達兩倍的速度,而其他用戶可能會看到與以前版本類似的性能
: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);
Imports IronOcr

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Add image
Private imageInput = New OcrImageInput("Potter.png")

' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
$vbLabelText   $csharpLabel
class="content-img-align-center">
class="center-image-wrapper"> 閱讀 PNG 圖像

請參閱 如何閱讀多幀/頁 GIF 和 TIFF 文章以了解更多關於閱讀 TIFF 和 GIF 圖像的信息。

將圖像匯入為位元組

除了普通的文件路徑,OcrImageInput 類還接受位元組形式的圖像信息、AnyBitmap、流,以及圖像。 AnyBitmap 是 IronSoftware.Drawing.AnyBitmap 的位圖對象。

:path=/static-assets/ocr/content-code-examples/how-to/input-images-import-byte.cs
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
Private ocrTesseract As New IronTesseract()

' Read byte from file
Private data() As Byte = File.ReadAllBytes("Potter.tiff")

' Import image byte
Private imageInput = New OcrImageInput(data)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
$vbLabelText   $csharpLabel

指定掃描區域

實例化 OcrImageInput 類時還接受 CropRectangle。 這允許您指定圖像文檔中應 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)
$vbLabelText   $csharpLabel

OCR 結果

class="content-img-align-center">
class="center-image-wrapper"> 閱讀特定區域

常見問題解答

如何在.NET C#中對影像進行OCR辨識?

您可以使用 IronOCR 函式庫在 .NET C# 中對影像進行 OCR 辨識。首先從 NuGet 下載 IronOCR,實例化IronTesseract類,然後使用OcrImageInput類別匯入映像。接下來,應用Read方法從圖像中提取文字。

.NET 中的 OCR 可以處理哪些影像格式?

IronOCR支援多種影像格式進行OCR處理,包括jpg、png、gif、tiff和bmp。這使其能夠靈活地與不同類型的圖像檔案整合。

如何在 C# 中指定影像的特定區域進行 OCR 辨識?

在 IronOCR 中,您可以在實例化OcrImageInput類別時使用CropRectangle來定義要處理的影像特定區域。這可以透過將 OCR 操作集中在特定區域來提高效能。

是否可以在 C# 中從位元組數組中讀取圖像以進行 OCR 識別?

是的,IronOCR 允許您以位元組數組、AnyBitmap、Stream 或 Image 格式輸入圖像。這種靈活性使您能夠對不同格式的影像資料執行 OCR 識別。

如何在C#中確保OCR操作期間的高效記憶體管理?

為了在使用 IronOCR 進行 OCR 操作時高效管理內存,請在創建OcrImageInput物件時使用「using」語句。這樣可以確保在不再需要資源時,資源能夠被正確釋放。

我可以用 C# 處理多幀或多頁影像進行 OCR 辨識嗎?

是的,IronOCR 支援讀取多幀或多頁影像,例如 GIF 和 TIFF 格式。此功能可讓您對包含多個影格或頁面的複雜影像檔案執行 OCR 辨識。

如何使用圖像濾波器增強OCR的文字辨識能力?

IronOCR 提供影像濾鏡,可用於增強文字辨識能力。透過套用這些濾鏡,您可以提高各種影像格式的 OCR 辨識準確性和可靠性。

在 C# 中使用 IronOCR 進行影像讀取有哪些好處?

IronOCR 為 C# 中的影像讀取提供了許多優勢,包括支援多種影像格式、可指定掃描區域以及能夠以位元組或串流的形式輸入影像。這些特性使得 .NET 應用程式能夠實現高效靈活的 OCR 處理。

Curtis Chau
技術作家

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

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

審核人

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 70

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 70
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

">

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 84

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 84
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once