如何在 C# 中從螢幕截圖中提取文字
許多在外的人可能會想知道"什麼是OCR截圖?"。其他人可能會想知道如何將任何文字的截圖轉換為可編輯的數位文字格式或轉換為.txt或.doc格式。 如果您是這些人之一,那麼不用擔心,因為我們有完美的解決方案給您。
在本文中,我們將討論不同的工具,這些工具將使您能夠對截圖進行OCR,即光學字元識別。
市面上有很多OCR工具,但今天我們將使用IronOCR從截圖中提取文字。
1. IronOCR
IronOCR 是一個用於C#和VB.NET編程語言的軟體程式庫,旨在使開發人員能夠在其應用程式中新增OCR(光學字元識別)功能。 該程式庫可用於識別圖像中的文字並將其轉換為可機器識別的文字。 該程式庫建立在被認為是目前最準確的OCR引擎之一的Tesseract OCR引擎之上。
IronOCR可用於從多種不同文件格式的圖像中讀取文字,包括PNG、JPG、TIFF和PDF。 它還提供一系列先進的文字識別功能,例如能夠識別多種語言,以及能夠從旋轉或扭曲的圖像中識別文字。 此外,開發人員可以使用IronOCR快速整合OCR功能到他們的應用程式中,因為它提供了一個簡單、易於使用的API,可以從C#或VB.NET程式碼中調用。 使用IronOCR,您可以選擇您的OCR語言,並對圖像、數字PDF文件和掃描的PDF文件進行OCR。
IronOCR被認為是開發人員希望在他們的應用程式中新增OCR功能的一個不錯的選擇。 它是開源的,易於使用和整合,快速、準確,並始終保持最新的OCR技術。
2. IronOCR功能
IronOCR提供了廣泛的功能,從而幫助開發人員將OCR功能整合到他們的應用程式中。 IronOCR的一些主要功能包括:
- 多語言支持:IronOCR可以識別超過60種語言的文字,包括英語、西班牙語、德語、法語、義大利語和中文。
- 自動檢測文字方向:IronOCR可以自動檢測圖像中文字的方向,即使圖像已被旋轉或扭曲。
- 支持多種圖像格式:IronOCR可以從多種不同文件格式的圖像中讀取文字,包括PNG、JPG、TIFF和PDF。
- 可自定義的識別設置:開發人員可以自定義識別設置,以提高特定型別圖像或用例的識別準確性。
- 能夠從掃描的文件和包含多頁的PDF中識別文字。
- 快速識別和高準確性:IronOCR使用的是Tesseract OCR引擎,這是當下最準確和廣泛使用的OCR引擎之一。
- 易於使用的API:IronOCR提供了一個簡單、易於使用的API,可以從C#或VB.NET程式碼中調用,這使得將OCR功能整合到任何應用程式中變得簡單。
總的來說,IronOCR是一個強大的工具,提供了很廣泛的功能,可以幫助開發人員將OCR功能新增到他們的應用程式中。
3. 在Visual Studio中建立新專案
打開Visual Studio並進入文件選單。 選擇"新建專案",然後選擇控制台應用程式。
輸入專案名稱並在適當的文字框中選擇路徑。 然後,點擊建立按鈕。 選擇所需的 .NET Framework,如下圖所示:
在Visual Studio中建立新專案
Visual Studio專案現在將生成控制台應用程式的結構。 完成後,它將打開program.cs文件,您可以在其中編寫和執行源程式碼。
由Visual Studio的新專案向導生成的program.cs文件
現在我們可以新增IronOCR程式庫並測試該程式。
4. 安裝IronOCR
在Visual Studio中,您可以輕鬆地將IronOCR與您的C#專案進行整合。
IronOCR提供多種整合到C# .NET專案的方法。 在這裡,我們將討論其中一個:使用NuGet套件管理器安裝IronOCR。
在Visual Studio中,進入工具 > NuGet套件管理器 > 套件管理器控制台
NuGet套件管理器使用者介面
點擊後,新的控制台將出現在Visual Studio窗口的底部。 在控制台中輸入以下命令並按回車。
Install-Package IronOcr
IronOCR將在幾秒鐘內安裝完成。
5. 使用IronOCR對截圖進行OCR
IronOCR是一個強大的OCR程式庫,可以用來從截圖中識別文字。 利用IronOCR,您可以截取文字的截圖,然後使用程式庫的OCR功能將截圖中的文字轉換為可編輯的數位格式。 以下是您可以如何使用IronOCR在C#中對截圖進行OCR的例子。 要對截圖進行OCR,只需捕捉截圖並運行以下程式碼以將文字提取到您想要的任何輸出格式。
using IronOcr;
using System;
class Program
{
static void Main()
{
// Create an instance of IronTesseract, the core OCR engine
var ocr = new IronTesseract();
// Perform OCR on the specified image file
var result = ocr.Read("ocr.png");
// Output the recognized text to the console
Console.WriteLine(result.Text);
}
}
using IronOcr;
using System;
class Program
{
static void Main()
{
// Create an instance of IronTesseract, the core OCR engine
var ocr = new IronTesseract();
// Perform OCR on the specified image file
var result = ocr.Read("ocr.png");
// Output the recognized text to the console
Console.WriteLine(result.Text);
}
}
Imports IronOcr
Imports System
Friend Class Program
Shared Sub Main()
' Create an instance of IronTesseract, the core OCR engine
Dim ocr = New IronTesseract()
' Perform OCR on the specified image file
Dim result = ocr.Read("ocr.png")
' Output the recognized text to the console
Console.WriteLine(result.Text)
End Sub
End Class
輸入圖像文件
用作輸入的樣本截圖
文字輸出
- IRONOCR for NET
- The C# OCR Library
- OCR for C# to scan and read images & PDFs
- NET OCR library with 125+ global language packs
- Output as text, structured data, or searchable PDFs
- Supports NET 6, 5, Core, Standard, Framework
6. 使用IronOCR對特定區域進行OCR
IronOCR允許您在圖像的特定區域內進行OCR。 當圖像包含多個文字區域時,這可以很有用,並且您只需要識別特定區域內的文字。 下面顯示了一個範例程式碼。
using IronOcr;
using IronSoftware.Drawing;
using System;
class Program
{
static void Main()
{
var ocrTesseract = new IronTesseract();
using (var ocrInput = new OcrInput())
{
// Define the rectangle to crop the image for OCR
var contentArea = new CropRectangle(x: 0, y: 0, width: 350, height: 150);
// Add the image with the specified cropping area
ocrInput.AddImage("ocr.png", contentArea);
// Perform the OCR operation on the defined area
var ocrResult = ocrTesseract.Read(ocrInput);
// Output the recognized text
Console.WriteLine(ocrResult.Text);
}
}
}
using IronOcr;
using IronSoftware.Drawing;
using System;
class Program
{
static void Main()
{
var ocrTesseract = new IronTesseract();
using (var ocrInput = new OcrInput())
{
// Define the rectangle to crop the image for OCR
var contentArea = new CropRectangle(x: 0, y: 0, width: 350, height: 150);
// Add the image with the specified cropping area
ocrInput.AddImage("ocr.png", contentArea);
// Perform the OCR operation on the defined area
var ocrResult = ocrTesseract.Read(ocrInput);
// Output the recognized text
Console.WriteLine(ocrResult.Text);
}
}
}
Imports IronOcr
Imports IronSoftware.Drawing
Imports System
Friend Class Program
Shared Sub Main()
Dim ocrTesseract = New IronTesseract()
Using ocrInput As New OcrInput()
' Define the rectangle to crop the image for OCR
Dim contentArea = New CropRectangle(x:= 0, y:= 0, width:= 350, height:= 150)
' Add the image with the specified cropping area
ocrInput.AddImage("ocr.png", contentArea)
' Perform the OCR operation on the defined area
Dim ocrResult = ocrTesseract.Read(ocrInput)
' Output the recognized text
Console.WriteLine(ocrResult.Text)
End Using
End Sub
End Class
輸出
- IRONOCR for NET
- The C# OCR Library
- OCR for C# to scan and read images & PDFs
- NET OCR library with 125+ global language packs
7. 使用IronOCR對圖像進行OCR
要對圖像進行OCR並將識別的文字保存為.txt文件,您可以使用以下程式碼。
using IronOcr;
using System;
class Program
{
static void Main()
{
var ocr = new IronTesseract();
using (var input = new OcrInput("ocr.png"))
{
// Perform OCR on the image
var result = ocr.Read(input);
// Save the recognized text to a .txt file
result.SaveAsTextFile("output.txt");
}
}
}
using IronOcr;
using System;
class Program
{
static void Main()
{
var ocr = new IronTesseract();
using (var input = new OcrInput("ocr.png"))
{
// Perform OCR on the image
var result = ocr.Read(input);
// Save the recognized text to a .txt file
result.SaveAsTextFile("output.txt");
}
}
}
Imports IronOcr
Imports System
Friend Class Program
Shared Sub Main()
Dim ocr = New IronTesseract()
Using input = New OcrInput("ocr.png")
' Perform OCR on the image
Dim result = ocr.Read(input)
' Save the recognized text to a .txt file
result.SaveAsTextFile("output.txt")
End Using
End Sub
End Class
生成的輸出文件的內容如下所示:
生成的output.txt文件的內容
8. 了解更多
閱讀圖像文字提取教程以獲取更多有關如何對圖像進行OCR的資訊。
IronOCR是設計為處理不同型別文件的五個.NET程式庫之一的套件的一部分。 您可以用兩份授權的價格購買全部五個程式庫。
常見問題
如何使用C#中的OCR從截圖中提取文字?
您可以使用IronOCR在C#中透過其簡單的API,將截圖轉換為可編輯的數位文字格式來提取文字。首先,透過Visual Studio中的NuGet安裝IronOCR,然後使用IronOCR提供的程式碼範例在您的截圖圖片上執行OCR。
什麼是光學字元識別(OCR)?
光學字元識別(OCR)是一種技術,可以將不同型別的文件,例如掃描的紙質文件、PDF文件或由數位相機拍攝的圖片,轉換為可編輯和可搜尋的資料。IronOCR是一個C#程式庫,可以為應用程式提供OCR功能。
IronOCR可以處理多種語言的OCR嗎?
是的,IronOCR支持超過60種語言的文字識別,這使其在國際應用中用途廣泛。它提供設置語言偏好的選項,以確保精確的文字提取。
IronOCR支持哪些圖像格式的OCR?
IronOCR支持多種圖像格式的OCR,包括PNG、JPG、TIFF和PDF。這種靈活性允許開發者在不需要手動轉換格式的情況下處理各種圖像來源。
文字方向如何影響OCR的準確性?
文字方向會對OCR的準確性產生很大影響。IronOCR自動檢測並校正圖像中的文字方向,保證旋轉或歪斜的文字得以精確識別並轉換為數位格式。
如何在C#專案中安裝IronOCR?
若要在C#專案中安裝IronOCR,請使用Visual Studio中的NuGet程式包管理器。搜尋IronOCR並將其安裝到您的專案中,以開始使用其從圖像中提取文字的OCR功能。
使用IronOCR進行文字識別有何優勢?
IronOCR提供多項優勢,包括多語言支持、文字方向自動校正、多種圖像格式支持,以及可自定義設置以提高識別準確性。它的簡單API促進了C#應用程式的輕鬆整合。
IronOCR適合用於識別圖片特定區域的文字嗎?
是的,IronOCR允許開發者在圖片中定義特定區域以執行OCR,從而實現目標文字提取。這項功能在僅一部分圖像包含相關文的情況下十分有用。
一些常見的OCR問題解決技巧是什麼?
常見的OCR問題解決技巧包括確保圖片清晰且高解析度、檢查文字方向、確保語言設置正確,以及升級到最新版本的IronOCR以獲得最佳性能。
如何將OCR結果轉換成.txt或.doc文件?
使用IronOCR,您可以透過從圖片中提取文字並使用C#中的標準文件I/O操作將其儲存來轉換成.txt或.doc文件。這允許您從基於圖像的文字建立可編輯文件。



