如何從 System.Drawing 物件讀取
System.Drawing.Bitmap 是 .NET Framework 中用來處理點陣圖影像的類別。它提供了創建、操作和顯示點陣圖影像的方法和屬性。
System.Drawing.Image 是 .NET Framework 中所有 GDI+ 圖像物件的基類。它是各種圖像類型的父類,包括 System.Drawing.Bitmap。
IronSoftware.Drawing.AnyBitmap 是一個點陣圖類 鐵繪圖由Iron Software最初開發的一個開源庫。它幫助C#軟體工程師在Windows、macOS和Linux平台上的.NET項目中替換System.Drawing.Common
。
如何從 System.Drawing 物件讀取
- 下載用於讀取 System.Drawing 物件的 C# 程式庫
- 獲取 System.Drawing 物件,例如 Bitmap 和 Image
- 使用獲取的數據構建OcrImageInput類
- 利用 Iron Software 的 AnyBitmap 用於 Linux 和 macOS
- 通過指定裁剪區域來定義閱讀區域
立即開始在您的專案中使用IronPDF,並享受免費試用。
查看 IronOCR 上 Nuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變OCR。
Install-Package IronOcr
請考慮安裝 IronOCR DLL 直接下載並手動安裝到您的專案或GAC表單: IronOcr.zip
手動安裝到您的項目中
下載DLL讀取 System.Drawing.Bitmap 示例
首先,實例化 IronTesseract 類別以執行 OCR。從各種方法中創建一個 System.Drawing.Bitmap。在代碼示例中,我決定使用檔案路徑。
接下來,使用 'using' 聲明來創建 OcrImageInput 對象,將圖像從 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)
讀取 System.Drawing.Image 範例
從 System.Drawing.Image 讀取就像使用圖像建立 OcrImageInput 物件一樣簡單,接著使用 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)
讀取 IronSoftware.Drawing.AnyBitmap 範例
同樣,在創建或獲取 AnyBitmap 對象後,您可以構建 OcrImageInput 類。構造函式將負責導入數據的所有必要步驟。下面的程式碼範例演示了這一點。
: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)
指定掃描區域
在構建 OcrImageInput 類時,您可以指定要掃描的區域。這允許您定義影像文件的特定區域進行光學字符識別(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)