如何使用 IronOCR 在 C# 中实现屏幕截图图片转文字 OCR识别
IronOCR 的 ReadScreenshot 方法通过高效的图片转文字技术从屏幕截图中提取文本,进行精准的 OCR识别处理,处理各种尺寸和噪声挑战,同时支持包括 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 坐标以及矩形的高度和宽度。
使用 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 方法可以处理屏幕截图中的多语言内容,因此适用于国际应用程序和多语言用户界面。

