如何使用IronOCR在C#中讀取截圖

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

IronOCR的ReadScreenshot方法有效地從截圖中提取文字,處理各種尺寸和噪聲挑戰,同時支持包括PNG、JPG和BMP在內的常見文件格式。

截圖提供了一種快速分享資訊和抓取重要資料的方式。 然而,由於各種尺寸和噪聲,從截圖中提取文字被證明是困難的。這使得截圖成為OCR的一個具挑戰性的媒介。

IronOCR通過提供像ReadScreenshot這樣的專用方法解決了這一問題。 此方法已針對閱讀截圖並從中提取資訊進行了優化,同時接受常見文件格式。 與標準OCR方法不同,此方法應用了特定預處理優化,專為截圖內容量身定制,包括自動降噪和對比增強。

要使用此功能,請安裝[IronOcr.Extension.AdvancedScan]套件。 此擴展提供了增強截圖文字識別準確性的高級計算機視覺功能,特別是對於UI元素、系統字體和現代應用程式中的反鋸齒文字。

快速入門:從截圖中讀取文字

使用IronOCR的OcrPhotoResult立即存取提取的文字、置信度評分和文字區域。 這是將圖像轉換為可用文字的最快方法,設置最少。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronOcr

    PM > Install-Package IronOcr
  2. 複製並運行這段程式碼片段。

    OcrPhotoResult result = new IronTesseract().ReadScreenShot(new OcrInput().LoadImage("screenshot.png"));
  3. 部署以在您的實時環境中測試

    今天就開始在您的專案中使用IronOCR,透過免費試用

    arrow pointer

本指南演示了如何使用IronOCR執行截圖文字識別,並包含範例和結果物件的屬性。 我們將探索進階場景,比如處理特定區域,處理多語言內容,以及對批量處理進行性能優化。

如何使用ReadScreenshot從截圖中提取文字?

要在IronOCR中讀取截圖,請使用OcrInput作為參數。 此方法比庫的標準Read對應方法對截圖進行了更多的優化。 優化包括自動檢測UI元素、改進處理反鋸齒字體,以及跨不同操作系統對系統字體的改進識別。

請注意

  • 此方法當前適用於包括英語、中文、日語、韓語和基於拉丁字母的語言。
  • 在.NET Framework上使用高級掃描需要項目在x64架構上運行。

)}]

哪些截圖型別效果最佳?

以下是我們的程式碼範例輸入; 我們展示了此方法的多功能性,通過混合不同的文字字體和大小。 ReadScreenshot方法在識別以下方面表現卓越:

  • 系統UI字體(Windows, macOS, Linux)
  • 現代應用程式中的反鋸齒文字
  • 混合字體大小和樣式
  • 覆蓋在複雜背景上的文字
  • 主控台輸出和終端截圖
  • 帶有各種網頁字體的瀏覽器內容

為獲得最佳效果,請在未經壓縮的原始解析度下截取截圖。 該方法處理多種圖像格式,但由於其無損壓縮,PNG格式最好保留文字清晰。

IronOCR C# OCR庫主頁顯示平台相容性和文字識別的關鍵功能

如何實現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)
$vbLabelText   $csharpLabel

對於複雜場景,通過額外預處理增強截圖讀取過程:

:path=/static-assets/ocr/content-code-examples/how-to/read-screenshot-3.cs
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");

// 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")

    ' 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
$vbLabelText   $csharpLabel

OcrPhotoResult返回什麼屬性?

Visual Studio除錯器顯示IronOCR庫細節,版本2024.9,準確度評分0.937

控制台輸出顯示從截圖中提取的所有文字實例。 讓我們探討OcrPhotoResult的屬性以及如何有效利用它們:

  • Text:從OCR輸入提取的文字。 此屬性將所有識別的文字作為單個字串保存,保留原始佈局包括換行和間距。
  • Confidence:一個雙精度屬性,表示從0到1的統計準確性信心,其中1代表最高信心。 使用此方法在您的應用中實現質量控制。
  • TextRegion物件的陣列,包含返回截圖中找到文字區域的屬性。 預設情況下,所有Rectangle類。 包括width

使用TextRegions可以:

  • 從特定截圖區域提取文字
  • 識別UI元素位置
  • 基於文字位置建立可點擊的覆蓋
  • 實現區域特定OCR處理

這裡是一個處理單個文字區域的範例:

:path=/static-assets/ocr/content-code-examples/how-to/read-screenshot-4.cs
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.TextInRegion}");
    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.RegionConf:P2}");
    Console.WriteLine("---");
}

// Find specific UI elements by text content
var buttonRegion = result.TextRegions
    .FirstOrDefault(r => r.TextInRegion.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.TextInRegion}")
        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.RegionConf:P2}")
        Console.WriteLine("---")
    Next

    ' Find specific UI elements by text content
    Dim buttonRegion = result.TextRegions _
        .FirstOrDefault(Function(r) r.TextInRegion.Contains("Submit", StringComparison.OrdinalIgnoreCase))

    If buttonRegion IsNot Nothing Then
        Console.WriteLine($"Found button at: {buttonRegion.Region.X}, {buttonRegion.Region.Y}")
    End If
End Using
$vbLabelText   $csharpLabel

進階截圖處理技術

處理多語言截圖

當使用含有多種語言的截圖時,IronOCR提供了強大的多語言支持。 這對於國際應用或多語使用者介面的截圖非常有用:

:path=/static-assets/ocr/content-code-examples/how-to/read-screenshot-5.cs
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
$vbLabelText   $csharpLabel

批量處理性能優化

在處理多個截圖時,請實施這些優化策略:

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
$vbLabelText   $csharpLabel

截圖OCR的最佳實踐

  1. 捕獲質量:在不縮放的原始解析度下截取截圖
  2. 格式選擇:使用PNG格式以保留無損質量
  3. 預處理:根據截圖內容應用適當的濾鏡
  4. 置信度門檻值:對於關鍵應用,實施基於信心的驗證
  5. 進度跟蹤:對於長時間操作,實施進度跟蹤

常見用例

ReadScreenshot方法非常適合:

  • 自動化UI測試和驗證
  • 數位資產管理系統
  • 用於捕捉錯誤訊息的客戶支援工具
  • 記錄自動化
  • 用於螢幕閱讀器的無障礙工具
  • 遊戲和串流應用

與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方法可以處理截圖中的多語言內容,使其適用於國際應用和多語言使用者介面。

IronOCR如何提高資料精確性?

IronOCR通過其先進的識別算法和影像校正功能提高資料精確性,確保文字提取過程既可靠又精確。

IronOCR有免費試用版嗎?

有的,Iron Software提供IronOCR的免費試用版,允許使用者在做出購買決定前測試其功能和能力。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備開始了嗎?
Nuget 下載 6,136,090 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在滾動?

想要快速證明? PM > Install-Package IronOcr
執行範例 觀看您的圖像轉變為可搜尋文字。