如何在 C# 中使用IronOCR讀取螢幕截圖進行 OCR辨識與圖片轉文字
IronOCR 的 ReadScreenshot 方法能夠有效率地從螢幕截圖中擷取圖片文字,處理各種尺寸和噪音挑戰,同時支援包括 PNG、JPG 和 BMP 在內的常見文件格式,輕鬆實現 OCR辨識功能。
螢幕截圖是快速分享資訊和擷取重要資料的一種方式。 然而,由於螢幕截圖尺寸不一且存在噪聲,從中提取文字一直很困難。這使得螢幕截圖成為OCR識別的挑戰性媒介。
IronOCR透過提供諸如 ReadScreenshot 之類的專門方法來解決這個問題。 該方法針對讀取螢幕截圖和從中提取資訊進行了最佳化,同時支援常見的檔案格式。 與標準 OCR 方法不同,該方法應用了針對螢幕截圖內容量身定制的特定預處理優化,包括自動降噪和對比度增強。
若要使用此功能,請安裝IronOCR軟體套件。 此擴充功能提供高級電腦視覺功能,可提高螢幕截圖文字辨識的準確性,尤其適用於現代應用程式中的 UI 元素、系統字體和抗鋸齒文字。
快速入門:從螢幕截圖讀取文字
使用 IronOCR 的 ReadScreenshot,只需幾秒鐘即可開始使用——將螢幕截圖加載到 OcrInput 中,呼叫 ReadScreenShot,然後立即透過 OcrPhotoResult 存取提取的文字、置信度分數和文字區域。 這是將圖像轉換為可用文字的最快方法,而且設定極少。
本指南示範如何使用IronOCR進行螢幕截圖文字識別,並逐步介紹範例和結果物件的屬性。 我們將探索進階場景,例如處理特定區域、處理多語言內容以及優化批次處理的效能。
最簡工作流程(5個步驟)
- 下載用於讀取螢幕截圖的 C# 庫
- 導入螢幕截圖進行處理
- 使用`ReadScreenshot`方法從圖像中提取文本
- 使用**OcrPhotoResult**屬性檢索提取的資料以進行進一步處理。
- 根據需要儲存或匯出提取的文字。
如何使用 ReadScreenshot 從螢幕截圖中提取文字?
若要在IronOCR中讀取螢幕截圖,請使用 ReadScreenshot 方法,該方法接受 OcrInput 作為參數。 這種方法比函式庫的標準 Read 對應方法更適合截圖。 優化內容包括自動檢測使用者介面元素、更好地處理抗鋸齒字體以及改進對不同作業系統中系統字體的識別。
- 該方法目前適用於英語、中文、日語、韓語以及拉丁字母等語言。
- 在.NET Framework上使用進階掃描功能需要專案在 x64 架構上運作。
哪些類型的螢幕截圖效果最佳?
以下是我們為程式碼範例提供的輸入; 我們透過混合使用不同的字體和字號來展示這種方法的通用性。 ReadScreenshot 方法擅長辨識:
- 系統介面字型(Windows、macOS、Linux)
- 來自現代應用程式的抗鋸齒文本
- 混合字體大小和样式
- 文字疊加在複雜的背景上
- 控制台輸出和終端截圖
- 瀏覽器內容包含各種網頁字體
為獲得最佳效果,請以原始解析度截取螢幕截圖,不要進行壓縮。 此方法可以處理各種影像格式,但由於 PNG 格式具有無損壓縮特性,因此能最好地保留文字清晰度。

如何實作 ReadScreenshot 方法?
:path=/static-assets/ocr/content-code-examples/how-to/read-screenshot-read-screenshot.cs
using IronOcr;
using System;
using System.Linq;
// Instantiate OCR engine
var ocr = new IronTesseract();
using var inputScreenshot = new OcrInput();
inputScreenshot.LoadImage("screenshotOCR.png");
// Perform OCR
OcrPhotoResult result = ocr.ReadScreenShot(inputScreenshot);
// Output screenshot information
Console.WriteLine(result.Text);
Console.WriteLine(result.TextRegions.First().Region.X);
Console.WriteLine(result.TextRegions.Last().Region.Width);
Console.WriteLine(result.Confidence);
Imports IronOcr
Imports System
Imports System.Linq
' Instantiate OCR engine
Private ocr = New IronTesseract()
Private inputScreenshot = New OcrInput()
inputScreenshot.LoadImage("screenshotOCR.png")
' Perform OCR
Dim result As OcrPhotoResult = ocr.ReadScreenShot(inputScreenshot)
' Output screenshot information
Console.WriteLine(result.Text)
Console.WriteLine(result.TextRegions.First().Region.X)
Console.WriteLine(result.TextRegions.Last().Region.Width)
Console.WriteLine(result.Confidence)
對於複雜場景,可透過額外的預處理來增強螢幕截圖讀取過程:
using IronOcr;
using System;
// Configure OCR engine with specific settings for screenshots
var ocr = new IronTesseract()
{
// Set language for better accuracy with non-English content
Language = OcrLanguage.English,
// Configure for screen-resolution images
Configuration = new TesseractConfiguration()
{
PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd,
// Enable whitelist for specific characters if needed
WhiteListCharacters = null
}
};
using var inputScreenshot = new OcrInput();
// Load screenshot with specific DPI setting for consistency
inputScreenshot.LoadImage("screenshotOCR.png", 96); // Standard screen DPI
// Apply preprocessing for better accuracy
inputScreenshot.DeNoise(); // Remove screenshot artifacts
inputScreenshot.Sharpen(); // Enhance text edges
// Perform OCR with error handling
try
{
OcrPhotoResult result = ocr.ReadScreenShot(inputScreenshot);
// Process results with confidence threshold
if (result.Confidence > 0.8)
{
Console.WriteLine($"High confidence text extraction: {result.Text}");
}
else
{
Console.WriteLine("Low confidence - consider image preprocessing");
}
}
catch (Exception ex)
{
Console.WriteLine($"OCR Error: {ex.Message}");
}
using IronOcr;
using System;
// Configure OCR engine with specific settings for screenshots
var ocr = new IronTesseract()
{
// Set language for better accuracy with non-English content
Language = OcrLanguage.English,
// Configure for screen-resolution images
Configuration = new TesseractConfiguration()
{
PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd,
// Enable whitelist for specific characters if needed
WhiteListCharacters = null
}
};
using var inputScreenshot = new OcrInput();
// Load screenshot with specific DPI setting for consistency
inputScreenshot.LoadImage("screenshotOCR.png", 96); // Standard screen DPI
// Apply preprocessing for better accuracy
inputScreenshot.DeNoise(); // Remove screenshot artifacts
inputScreenshot.Sharpen(); // Enhance text edges
// Perform OCR with error handling
try
{
OcrPhotoResult result = ocr.ReadScreenShot(inputScreenshot);
// Process results with confidence threshold
if (result.Confidence > 0.8)
{
Console.WriteLine($"High confidence text extraction: {result.Text}");
}
else
{
Console.WriteLine("Low confidence - consider image preprocessing");
}
}
catch (Exception ex)
{
Console.WriteLine($"OCR Error: {ex.Message}");
}
Imports IronOcr
Imports System
' Configure OCR engine with specific settings for screenshots
Dim ocr As New IronTesseract() With {
' Set language for better accuracy with non-English content
.Language = OcrLanguage.English,
' Configure for screen-resolution images
.Configuration = New TesseractConfiguration() With {
.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd,
' Enable whitelist for specific characters if needed
.WhiteListCharacters = Nothing
}
}
Using inputScreenshot As New OcrInput()
' Load screenshot with specific DPI setting for consistency
inputScreenshot.LoadImage("screenshotOCR.png", 96) ' Standard screen DPI
' Apply preprocessing for better accuracy
inputScreenshot.DeNoise() ' Remove screenshot artifacts
inputScreenshot.Sharpen() ' Enhance text edges
' Perform OCR with error handling
Try
Dim result As OcrPhotoResult = ocr.ReadScreenShot(inputScreenshot)
' Process results with confidence threshold
If result.Confidence > 0.8 Then
Console.WriteLine($"High confidence text extraction: {result.Text}")
Else
Console.WriteLine("Low confidence - consider image preprocessing")
End If
Catch ex As Exception
Console.WriteLine($"OCR Error: {ex.Message}")
End Try
End Using
OcrPhotoResult 傳回哪些屬性?

控制台輸出顯示已從螢幕截圖中提取所有文字實例。 讓我們來探討一下 OcrPhotoResult 的特性以及如何有效地利用它們:
Text: 從 OCR 輸入中提取的文字。 此屬性包含所有已識別的文本,並將其作為單一字串保存,保留原始佈局,包括換行符和間距。Confidence: 一個雙精度屬性,表示統計準確度的置信度,範圍從 0 到 1,其中 1 表示最高的置信度。 使用此功能可在您的應用程式中實現品質控制。TextRegion: 一個TextRegion物件數組,其中包含傳回螢幕截圖中文字所在區域的屬性。 預設情況下,所有TextRegion都是IronOCR模型派生的Rectangle類別。 它包括矩形的 x 和 y 座標以及高度和寬度。
使用 TextRegions 可以讓你:
- 從特定螢幕截圖區域提取文本
- 確定使用者介面元素位置
- 根據文字位置建立可點擊的疊加層
- 實現特定區域的OCR處理
以下是處理單一文字區域的範例:
using IronOcr;
using System;
using System.Linq;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("screenshot.png");
OcrPhotoResult result = ocr.ReadScreenShot(input);
// Process each text region individually
foreach (var region in result.TextRegions)
{
Console.WriteLine($"Text: {region.Text}");
Console.WriteLine($"Location: X={region.Region.X}, Y={region.Region.Y}");
Console.WriteLine($"Size: {region.Region.Width}x{region.Region.Height}");
Console.WriteLine($"Confidence: {region.Confidence:P2}");
Console.WriteLine("---");
}
// Find specific UI elements by text content
var buttonRegion = result.TextRegions
.FirstOrDefault(r => r.Text.Contains("Submit", StringComparison.OrdinalIgnoreCase));
if (buttonRegion != null)
{
Console.WriteLine($"Found button at: {buttonRegion.Region.X}, {buttonRegion.Region.Y}");
}
using IronOcr;
using System;
using System.Linq;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("screenshot.png");
OcrPhotoResult result = ocr.ReadScreenShot(input);
// Process each text region individually
foreach (var region in result.TextRegions)
{
Console.WriteLine($"Text: {region.Text}");
Console.WriteLine($"Location: X={region.Region.X}, Y={region.Region.Y}");
Console.WriteLine($"Size: {region.Region.Width}x{region.Region.Height}");
Console.WriteLine($"Confidence: {region.Confidence:P2}");
Console.WriteLine("---");
}
// Find specific UI elements by text content
var buttonRegion = result.TextRegions
.FirstOrDefault(r => r.Text.Contains("Submit", StringComparison.OrdinalIgnoreCase));
if (buttonRegion != null)
{
Console.WriteLine($"Found button at: {buttonRegion.Region.X}, {buttonRegion.Region.Y}");
}
Imports IronOcr
Imports System
Imports System.Linq
Dim ocr As New IronTesseract()
Using input As New OcrInput()
input.LoadImage("screenshot.png")
Dim result As OcrPhotoResult = ocr.ReadScreenShot(input)
' Process each text region individually
For Each region In result.TextRegions
Console.WriteLine($"Text: {region.Text}")
Console.WriteLine($"Location: X={region.Region.X}, Y={region.Region.Y}")
Console.WriteLine($"Size: {region.Region.Width}x{region.Region.Height}")
Console.WriteLine($"Confidence: {region.Confidence:P2}")
Console.WriteLine("---")
Next
' Find specific UI elements by text content
Dim buttonRegion = result.TextRegions _
.FirstOrDefault(Function(r) r.Text.Contains("Submit", StringComparison.OrdinalIgnoreCase))
If buttonRegion IsNot Nothing Then
Console.WriteLine($"Found button at: {buttonRegion.Region.X}, {buttonRegion.Region.Y}")
End If
End Using
進階螢幕截圖處理技巧
處理多語言螢幕截圖
在處理包含多種語言的螢幕截圖時, IronOCR提供強大的多語言支援。 這對於國際應用程式或多語言使用者介面的螢幕截圖非常有用:
using IronOcr;
// Configure for multiple languages
var ocr = new IronTesseract();
ocr.AddSecondaryLanguage(OcrLanguage.ChineseSimplified);
ocr.AddSecondaryLanguage(OcrLanguage.Japanese);
using var input = new OcrInput();
input.LoadImage("multilingual-screenshot.png");
// Process with language detection
OcrPhotoResult result = ocr.ReadScreenShot(input);
Console.WriteLine($"Extracted multilingual text: {result.Text}");
using IronOcr;
// Configure for multiple languages
var ocr = new IronTesseract();
ocr.AddSecondaryLanguage(OcrLanguage.ChineseSimplified);
ocr.AddSecondaryLanguage(OcrLanguage.Japanese);
using var input = new OcrInput();
input.LoadImage("multilingual-screenshot.png");
// Process with language detection
OcrPhotoResult result = ocr.ReadScreenShot(input);
Console.WriteLine($"Extracted multilingual text: {result.Text}");
Imports IronOcr
' Configure for multiple languages
Dim ocr As New IronTesseract()
ocr.AddSecondaryLanguage(OcrLanguage.ChineseSimplified)
ocr.AddSecondaryLanguage(OcrLanguage.Japanese)
Using input As New OcrInput()
input.LoadImage("multilingual-screenshot.png")
' Process with language detection
Dim result As OcrPhotoResult = ocr.ReadScreenShot(input)
Console.WriteLine($"Extracted multilingual text: {result.Text}")
End Using
批量處理的效能優化
處理多個螢幕截圖時,請實施以下最佳化策略:
using IronOcr;
using System.Collections.Generic;
using System.Threading.Tasks;
public async Task ProcessScreenshotBatchAsync(List<string> screenshotPaths)
{
var ocr = new IronTesseract();
// Process screenshots in parallel for better performance
var tasks = screenshotPaths.Select(async path =>
{
using var input = new OcrInput();
input.LoadImage(path);
// Apply consistent preprocessing
input.DeNoise();
var result = await Task.Run(() => ocr.ReadScreenShot(input));
return new { Path = path, Result = result };
});
var results = await Task.WhenAll(tasks);
// Process results
foreach (var item in results)
{
Console.WriteLine($"File: {item.Path}");
Console.WriteLine($"Text: {item.Result.Text}");
Console.WriteLine($"Confidence: {item.Result.Confidence:P2}");
}
}
using IronOcr;
using System.Collections.Generic;
using System.Threading.Tasks;
public async Task ProcessScreenshotBatchAsync(List<string> screenshotPaths)
{
var ocr = new IronTesseract();
// Process screenshots in parallel for better performance
var tasks = screenshotPaths.Select(async path =>
{
using var input = new OcrInput();
input.LoadImage(path);
// Apply consistent preprocessing
input.DeNoise();
var result = await Task.Run(() => ocr.ReadScreenShot(input));
return new { Path = path, Result = result };
});
var results = await Task.WhenAll(tasks);
// Process results
foreach (var item in results)
{
Console.WriteLine($"File: {item.Path}");
Console.WriteLine($"Text: {item.Result.Text}");
Console.WriteLine($"Confidence: {item.Result.Confidence:P2}");
}
}
Imports IronOcr
Imports System.Collections.Generic
Imports System.Threading.Tasks
Public Async Function ProcessScreenshotBatchAsync(screenshotPaths As List(Of String)) As Task
Dim ocr As New IronTesseract()
' Process screenshots in parallel for better performance
Dim tasks = screenshotPaths.Select(Async Function(path)
Using input As New OcrInput()
input.LoadImage(path)
' Apply consistent preprocessing
input.DeNoise()
Dim result = Await Task.Run(Function() ocr.ReadScreenShot(input))
Return New With {Key .Path = path, Key .Result = result}
End Using
End Function)
Dim results = Await Task.WhenAll(tasks)
' Process results
For Each item In results
Console.WriteLine($"File: {item.Path}")
Console.WriteLine($"Text: {item.Result.Text}")
Console.WriteLine($"Confidence: {item.Result.Confidence:P2}")
Next
End Function
螢幕截圖OCR最佳實踐
1.截圖品質:以原始解析度截取螢幕截圖,不進行縮放。
2.格式選擇:使用 PNG 格式以無損保留影像品質。
3.預處理:根據螢幕截圖內容套用適當的濾鏡
4.置信閾值:對關鍵應用程式實施基於置信度的驗證
5.進度追蹤:對於長期作業,實施進度跟踪
常見用例
ReadScreenshot 方法非常適合:
- 自動化使用者介面測試和驗證
數位資產管理系統 - 用於捕獲錯誤訊息的客戶支援工具
- 文件自動化
- 螢幕閱讀器的輔助工具
- 遊戲和串流應用
與IronOCR功能的集成
螢幕截圖讀取功能與其他IronOCR功能無縫整合。 探索全面的OCR 結果處理功能,以各種格式匯出數據,或深入研究高階Tesseract 配置,以微調辨識精度。
概括
IronOCR 的 ReadScreenshot 方法為從螢幕截圖中提取文字提供了強大且最佳化的解決方案。 憑藉專門的預處理、高精度和全面的結果數據,它使開發人員能夠建立能夠可靠地處理螢幕截圖內容的強大應用程式。 無論是樓宇自動化工具、無障礙解決方案或資料擷取系統,ReadScreenshot 方法都能提供生產環境所需的效能和準確性。
常見問題解答
從螢幕截圖中進行 OCR 擷取有何挑戰性?
由於尺寸和雜訊程度各異,螢幕截圖對 OCR 來說是獨特的挑戰。IronOcr 使用其專門的 ReadScreenshot 方法來解決這些問題,該方法可應用專門針對螢幕快照內容最佳化的自動降噪和對比強化功能。
螢幕截圖 OCR 支援哪些檔案格式?
IronOCR 的 ReadScreenshot 方法支援常見的影像檔案格式,包括 PNG、JPG 和 BMP,因此與大多數的螢幕截圖工具和應用程式相容。
ReadScreenshot 方法與標準 OCR 方法有何不同?
與 IronOCR 中的標準 OCR 方法不同,ReadScreenshot 方法應用了專為螢幕截圖內容量身打造的特定預處理最佳化,包括自動降噪、對比強化,以及更好地處理反鋸齒字型和 UI 元素。
螢幕截圖 OCR 功能需要哪些額外套件?
要使用 IronOCR 中的 ReadScreenshot 功能,您需要安裝 IronOcr.Extension.AdvancedScan 套件,該套件提供先進的電腦視覺功能,可提高螢幕截圖文字辨識的準確度。
如何快速開始從螢幕截圖中擷取文字?
使用 IronOCR,您可以在幾秒鐘內從螢幕截圖中擷取文字,只要將螢幕截圖載入 OcrInput、呼叫 ReadScreenShot,並立即透過 OcrPhotoResult 存取擷取的文字、置信分數和文字區域即可。
螢幕截圖 OCR 可針對哪些類型的內容進行最佳化?
IronOCR 的螢幕截圖最佳化功能包括自動偵測 UI 元件、改善不同作業系統的系統字型辨識,以及更好地處理現代應用程式中常見的反鋸齒文字。
我可以處理截圖的特定區域嗎?
是的,IronOCR 支援處理螢幕截圖的特定區域,讓您可以針對特定區域進行處理,而不是處理整張圖片,這樣可以提高效能和精確度。
螢幕截圖 OCR 是否支援多語言內容?
IronOCR 的 ReadScreenshot 方法可以處理螢幕截圖中的多國語言內容,因此適用於國際應用程式和多國語言使用者介面。

