如何使用 IronOCR 在 C# 中读取屏幕截图
IronOCR 的 ReadScreenshot 方法可高效地从屏幕截图中提取文本,能够处理各种尺寸和噪点问题,同时支持 PNG、JPG 和 BMP 等常见文件格式。
截图是共享信息和捕捉重要数据的快捷方式。 然而,由于尺寸和噪音的不同,从屏幕截图中提取文本已被证明是困难的。这使得截图成为一种具有挑战性的 OCR 媒体。
IronOCR 通过提供 ReadScreenshot 等专用方法解决了这一问题。 该方法针对阅读截图和从中提取信息进行了优化,同时接受常见的文件格式。 与标准 OCR 方法不同的是,该方法应用了专为截图内容定制的特定预处理优化,包括自动降噪和对比度增强。
要使用此功能,请安装 [IronOcr.Extension.AdvancedScan] 包。 该扩展提供了先进的计算机视觉功能,可提高屏幕截图文本识别的准确性,特别是对于现代应用程序中的用户界面元素、系统字体和反锯齿文本。
快速入门:从屏幕截图中读取文本
使用 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坐标,以及height和width。
使用 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 的最佳实践
- 捕获质量:以本机分辨率捕获屏幕截图,不进行缩放
- 格式选择:使用PNG格式进行无损质量保留
- 预处理:根据屏幕截图内容应用适当的过滤器
- 置信阈值:为关键应用实施基于置信度的验证 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 的免费试用,使用户在做出购买决定之前可以测试其功能和能力。

