IronOCR 教程 如何在 .NET 中从图像读取文本 C# OCR Image to Text Tutorial: Convert Images to Text Without Tesseract Jacob Mellor 已更新:七月 23, 2025 Download IronOCR NuGet 下载 DLL 下载 Windows 安装程序 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English 想在 C# 中将图像转换为文本,而无需进行复杂的 Tesseract 配置? 本综合 IronOCR for .NET C# 教程向您展示如何在您的 .NET 应用程序中实现强大的光学字符识别功能,只需几行代码。 as-heading:2(快速入门:从图像中提取文本,只需一行)</em 这个例子说明了掌握 IronOCR 是多么容易--只需一行 C# 就能将您的图像转化为文本。 它演示了如何初始化 OCR 引擎并立即读取和检索文本,而无需进行复杂的设置。 Get started making PDFs with NuGet now: Install IronOCR with NuGet Package Manager PM > Install-Package IronOcr Copy and run this code snippet. string text = new IronTesseract().Read("image.png").Text; Deploy to test on your live environment Start using IronOCR in your project today with a free trial Free 30 day Trial 最小工作流程(5 个步骤)下载 IronOCR - 用于图像到文本转换的 C# OCR 库。 使用 IronTesseract 类立即读取图像中的文本。 应用图像过滤器以提高低质量扫描的 OCR 精确度使用可下载的语言包处理多种语言将结果导出为可搜索的 PDF 或提取文本字符串 如何在 .NET 应用程序中从图像中读取文本? 要在您的 .NET 应用程序中实现 C# OCR 图像转文本功能,您需要一个可靠的 OCR 库。 IronOCR 使用 `IronOcr.IronTesseract` 类提供了一个托管解决方案,无需外部依赖即可最大限度地提高准确性和速度。 首先,将 IronOCR 安装到 Visual Studio 项目中。 You can download the [IronOCR DLL directly](https://www.nuget.org/packages/IronOcr/) or use [NuGet Package Manager](https://www.nuget.org/packages/IronOcr/). ```shell :ProductInstall ``` 为什么在不使用 Tesseract 的情况下选择 IronOCR 进行 C# OCR? 当您需要用 C# 将图像转换为文本时,IronOCR 与传统的 Tesseract 实现相比具有显著优势: - 可在纯 .NET 环境中立即使用 - 无需安装或配置 Tesseract - 运行最新的引擎:**Tesseract 5**(加上 Tesseract 4 和 3) - 与 .NET Framework 4.5+、.NET Standard 2+ 和 .NET Core 2、3、5、6、7、8、9 和 10 兼容 - 与原版 Tesseract 相比,提高了准确性和速度 - 支持 Xamarin、Mono、Azure 和 Docker 部署 - 通过 NuGet 包管理复杂的 Tesseract 词典 - 自动处理 PDF、多帧 TIFF 和所有主要图像格式 - 纠正低质量和倾斜扫描,以获得最佳效果 今天在您的项目中使用 IronOCR,免费试用。 第一步: 免费开始 如何使用 IronOCR C# 教程进行基础 OCR? 这个 Iron Tesseract C# 示例演示了使用 IronOCR 从图像读取文本的最简单方法。 `IronOcr.IronTesseract` 类提取文本并以字符串形式返回。 ```csharp // Basic C# OCR image to text conversion using IronOCR // This example shows how to extract text from images without complex setup using IronOcr; using System; try { // Initialize IronTesseract for OCR operations var ocrEngine = new IronTesseract(); // Path to your image file - supports PNG, JPG, TIFF, BMP, and more var imagePath = @"img\Screenshot.png"; // Create input and perform OCR to convert image to text using (var input = new OcrInput(imagePath)) { // Read text from image and get results OcrResult result = ocrEngine.Read(input); // Display extracted text Console.WriteLine(result.Text); } } catch (OcrException ex) { // Handle OCR-specific errors Console.WriteLine($"OCR Error: {ex.Message}"); } catch (Exception ex) { // Handle general errors Console.WriteLine($"Error: {ex.Message}"); } ``` 该代码在清晰的图像上实现了 100% 的准确率,提取的文本与图像完全一致: ```txt IronOCR Simple Example In this simple example we test the accuracy of our C# OCR library to read text from a PNG Image. This is a very basic test, but things will get more complicated as the tutorial continues. The quick brown fox jumps over the lazy dog ``` `IronTesseract` 类在内部处理复杂的 OCR 操作。 它能自动扫描对齐、优化分辨率,并使用 IronOCR 从图像中读取文本,准确度达到人类水平。 尽管在幕后进行了复杂的处理,包括图像分析、引擎优化和智能文本识别,但 OCR 流程在保持出色准确性的同时,还能与人类的阅读速度相匹配。 。 截图展示 IronOCR 从 PNG 图像中精确提取文本的能力如何在不配置 Tesseract 的情况下实现高级 C# OCR? 对于需要在 C# 中将图像转换为文本时获得最佳性能的生产应用程序,请同时使用 `OcrInput` 和 `IronTesseract` 类。 这种方法提供了对 OCR 过程的细粒度控制。 OcrInput 类的功能IronTesseract 类功能如何开始使用 OcrInput 和 IronTesseract? 下面是本 IronOCR C# 教程的推荐配置,可以很好地适用于大多数文档类型: ```csharp using IronOcr; // Initialize IronTesseract for advanced OCR operations IronTesseract ocr = new IronTesseract(); // Create input container for processing multiple images using (OcrInput input = new OcrInput()) { // Process specific pages from multi-page TIFF files int[] pageIndices = new int[] { 1, 2 }; // Load TIFF frames - perfect for scanned documents input.LoadImageFrames(@"img\Potter.tiff", pageIndices); // Execute OCR to read text from image using IronOCR OcrResult result = ocr.Read(input); // Output the extracted text Console.WriteLine(result.Text); } ``` 该配置在中等质量的扫描件上始终保持近乎完美的准确性。 `LoadImageFrames` 方法可高效处理多页文档,因此非常适合批量处理场景。 。 IronOCR 如何处理低质量扫描? *Low-resolution document with noise that IronOCR can process accurately using image filters* 在处理包含失真和数字噪声的不完美扫描时,**IronOCR 的性能优于其他 C# OCR 库**。 它是专门为现实世界的场景而设计的,而不是原始的测试图像。 ```csharp // Advanced Iron Tesseract C# example for low-quality images using IronOcr; using System; var ocr = new IronTesseract(); try { using (var input = new OcrInput()) { // Load specific pages from poor-quality TIFF var pageIndices = new int[] { 0, 1 }; input.LoadImageFrames(@"img\Potter.LowQuality.tiff", pageIndices); // Apply deskew filter to correct rotation and perspective input.Deskew(); // Critical for improving accuracy on skewed scans // Perform OCR with enhanced preprocessing OcrResult result = ocr.Read(input); // Display results Console.WriteLine("Recognized Text:"); Console.WriteLine(result.Text); } } catch (Exception ex) { Console.WriteLine($"Error during OCR: {ex.Message}"); } ``` 使用`Input.Deskew()`,低质量扫描的准确性提高到**99.8%**,几乎与高质量结果相匹配。 这就说明了为什么 IronOCR 是 C# OCR 的首选,而不会带来 Tesseract 的复杂性。 图像过滤器可能会略微增加处理时间,但会大大缩短整个 OCR 的持续时间。 找到正确的平衡点取决于您的文档质量。 在大多数情况下,`Input.Deskew()` 和 `Input.DeNoise()` 都能可靠地提高 OCR 性能。 了解[图像预处理技术](/csharp/ocr/tutorials/c-sharp-ocr-image-filters/)的更多信息。 如何优化 OCR 性能和速度? 在 C# 中将图像转换为文本时,影响 OCR 速度的最主要因素是输入质量。 较高的 DPI(约 200 dpi)和最小的噪点可以产生最快、最准确的结果。 虽然 IronOCR 擅长纠正不完善的文档,但这种增强功能需要额外的处理时间。 选择压缩痕迹最小的图像格式。 由于数字噪声较低,TIFF 和 PNG 通常比 JPEG 能产生更快的效果。 哪些图像过滤器可提高 OCR 速度? 以下过滤器可显著提高 C# OCR 图像到文本工作流程的性能: - **`OcrInput.Rotate(双度)`:**顺时针旋转图像(逆时针旋转为负值) - **`OcrInput.Binarize()`:**转换为黑/白,提高低对比度场景中的性能 - **`OcrInput.ToGrayScale()`:**转换为灰度,以提高速度。 - **`OcrInput.Contrast()`:**自动调整对比度以提高准确性 - **`OcrInput.DeNoise()`:**在预计会出现噪声时移除数字伪影 - **`OcrInput.Invert()`:**反转黑底白字的颜色 - **`OcrInput.Dilate()`:**扩展文本边界 - **`OcrInput.Erode()`:**减少文本边界 - **`OcrInput.Deskew()`:**校正对齐方式--对于倾斜文档至关重要 - **`OcrInput.DeepCleanBackgroundNoise()`:**强力去除噪声 - **`OcrInput.EnhanceResolution`:**改善低分辨率图像质量 如何配置 IronOCR 以获得最高速度? 在处理高质量扫描时,使用这些设置可优化速度: ```csharp using IronOcr; // Configure for speed - ideal for clean documents IronTesseract ocr = new IronTesseract(); // Exclude problematic characters to speed up recognition ocr.Configuration.BlackListCharacters = "~`$#^*_{[]}|\\"; // Use automatic page segmentation ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto; // Select fast English language pack ocr.Language = OcrLanguage.EnglishFast; using (OcrInput input = new OcrInput()) { // Load specific pages from document int[] pageIndices = new int[] { 1, 2 }; input.LoadImageFrames(@"img\Potter.tiff", pageIndices); // Read with optimized settings OcrResult result = ocr.Read(input); Console.WriteLine(result.Text); } ``` 与默认设置相比,该优化设置在保持**99.8%**准确性的同时,实现了**35%的速度提升**。 如何使用 C# OCR 阅读图像的特定区域? 下面的 Iron Tesseract C# 示例展示了如何使用 `System.Drawing.Rectangle` 锁定特定区域。 这种技术对于处理文本出现在可预测位置的标准化表单非常有用。 IronOCR 能否处理裁剪区域以获得更快的结果? 使用基于像素的坐标,您可以将 OCR 限制在特定区域,从而大幅提高速度并防止提取不必要的文本: ```csharp using IronOcr; using IronSoftware.Drawing; // Initialize OCR engine for targeted region processing var ocr = new IronTesseract(); using (var input = new OcrInput()) { // Define exact region for OCR - coordinates in pixels var contentArea = new System.Drawing.Rectangle( x: 215, y: 1250, width: 1335, height: 280 ); // Load image with specific area - perfect for forms and invoices input.AddImage("img/ComSci.png", contentArea); // Process only the defined region OcrResult result = ocr.Read(input); Console.WriteLine(result.Text); } ``` 这种有针对性的方法在只提取相关文本的同时将速度提高了 **41%**。 它非常适合结构化文档,如[发票](/csharp/ocr/blog/using-ironocr/invoice-ocr-csharp-tutorial/)、支票和表单。 同样的裁剪技术可与[PDF OCR 操作](/csharp/ocr/how-to/input-pdfs/)无缝配合。 。 演示使用 IronOCR 的矩形选择功能进行基于区域的精确文本提取的文档IronOCR 支持多少种语言? IronOCR 通过便捷的语言包提供 **125 种国际语言**。 从我们的网站或通过 [NuGet 软件包管理器](https://www.nuget.org/packages?q=IronOcr.Languages)以 DLL 形式下载。 Install language packs through the NuGet interface ([search "IronOcr.Languages"](https://www.nuget.org/packages?q=IronOcr.Languages)) or visit the [complete language pack listing](/csharp/ocr/languages/). 支持的语言包括阿拉伯语、中文(简体/繁体)、日语、韩语、印地语、俄语、德语、法语、西班牙语和其他 115 多种语言,每种语言都经过优化,可实现准确的文本识别。 如何在多种语言中实现 OCR? 此 IronOCR C# 教程示例演示了阿拉伯语文本识别: ```shell :InstallCmd Install-Package IronOcr.Languages.Arabic ``` IronOCR 能否处理包含多种语言的文档? 当文档包含混合语言时,配置 IronOCR 以支持多语言: ```shell :InstallCmd Install-Package IronOcr.Languages.ChineseSimplified ``` ```csharp // Multi-language OCR configuration using IronOcr; var ocr = new IronTesseract(); // Set primary language ocr.Language = OcrLanguage.ChineseSimplified; // Add secondary languages as needed ocr.AddSecondaryLanguage(OcrLanguage.English); // Custom .traineddata files can be added for specialized recognition // ocr.AddSecondaryLanguage("path/to/custom.traineddata"); using (var input = new OcrInput()) { // Process multi-language document input.AddImage("img/MultiLanguage.jpeg"); var result = ocr.Read(input); result.SaveAsTextFile("MultiLanguage.txt"); } ``` 如何使用 C# OCR 处理多页文档? IronOCR 可将多个页面或图像无缝合并到一个 `OcrResult` 中。 该功能可实现强大的功能,如[创建可搜索的 PDF](/csharp/ocr/how-to/searchable-pdf/) 和从整个文档集中提取文本。 在单个 OCR 操作中混合和匹配各种来源(图像、TIFF 框架和 PDF 页面): ```csharp // Multi-source document processing using IronOcr; IronTesseract ocr = new IronTesseract(); using (OcrInput input = new OcrInput()) { // Add various image formats input.AddImage("image1.jpeg"); input.AddImage("image2.png"); // Process specific frames from multi-frame images int[] frameNumbers = { 1, 2 }; input.AddImageFrames("image3.gif", frameNumbers); // Process all sources together OcrResult result = ocr.Read(input); // Verify page count Console.WriteLine($"{result.Pages.Count} Pages processed."); } ``` 高效处理 TIFF 文件的所有页面: ```csharp using IronOcr; IronTesseract ocr = new IronTesseract(); using (OcrInput input = new OcrInput()) { // Define pages to process (0-based indexing) int[] pageIndices = new int[] { 0, 1 }; // Load specific TIFF frames input.LoadImageFrames("MultiFrame.Tiff", pageIndices); // Extract text from all frames OcrResult result = ocr.Read(input); Console.WriteLine(result.Text); Console.WriteLine($"{result.Pages.Count} Pages processed"); } ``` 将 TIFF 或 PDF 转换为可搜索格式: ```csharp using System; using IronOcr; IronTesseract ocr = new IronTesseract(); using (OcrInput input = new OcrInput()) { try { // Load password-protected PDF if needed input.LoadPdf("example.pdf", "password"); // Process entire document OcrResult result = ocr.Read(input); Console.WriteLine(result.Text); Console.WriteLine($"{result.Pages.Count} Pages recognized"); } catch (Exception ex) { Console.WriteLine($"Error processing PDF: {ex.Message}"); } } ``` 如何从图像创建可搜索的 PDF? IronOCR 擅长创建可搜索的 PDF - 这是数据库系统、搜索引擎优化和文档可访问性的关键功能。 ```csharp using IronOcr; IronTesseract ocr = new IronTesseract(); using (OcrInput input = new OcrInput()) { // Set document metadata input.Title = "Quarterly Report"; // Combine multiple sources input.AddImage("image1.jpeg"); input.AddImage("image2.png"); // Add specific frames from animated images int[] gifFrames = new int[] { 1, 2 }; input.AddImageFrames("image3.gif", gifFrames); // Create searchable PDF OcrResult result = ocr.Read(input); result.SaveAsSearchablePdf("searchable.pdf"); } ``` 将现有 PDF 转换为可搜索版本: ```csharp using IronOcr; var ocr = new IronTesseract(); using (var input = new OcrInput()) { // Set PDF metadata input.Title = "Annual Report 2024"; // Process existing PDF input.LoadPdf("example.pdf", "password"); // Generate searchable version var result = ocr.Read(input); result.SaveAsSearchablePdf("searchable.pdf"); } ``` 对 TIFF 转换应用同样的技巧: ```csharp using IronOcr; var ocr = new IronTesseract(); using (var input = new OcrInput()) { // Configure document properties input.Title = "Scanned Archive Document"; // Select pages to process var pageIndices = new int[] { 1, 2 }; input.LoadImageFrames("example.tiff", pageIndices); // Create searchable PDF from TIFF OcrResult result = ocr.Read(input); result.SaveAsSearchablePdf("searchable.pdf"); } ``` 如何将 OCR 结果导出为 HOCR HTML? IronOCR 支持 HOCR HTML 导出,可实现结构化的 **PDF 至 HTML** 和 **TIFF 至 HTML** 转换,同时保留布局信息: ```csharp using IronOcr; var ocr = new IronTesseract(); using (var input = new OcrInput()) { // Set HTML title input.Title = "Document Archive"; // Process multiple document types input.AddImage("image2.jpeg"); input.AddPdf("example.pdf", "password"); // Add TIFF pages var pageIndices = new int[] { 1, 2 }; input.AddTiff("example.tiff", pageIndices); // Export as HOCR with position data OcrResult result = ocr.Read(input); result.SaveAsHocrFile("hocr.html"); } ``` IronOCR 能否在读取文本的同时读取条形码? IronOCR 将文本识别与[条形码阅读功能](/csharp/ocr/how-to/barcodes/)独特地结合在一起,无需单独的库: ```csharp // Enable combined text and barcode recognition using IronOcr; var ocr = new IronTesseract(); // Enable barcode detection ocr.Configuration.ReadBarCodes = true; using (var input = new OcrInput()) { // Load image containing both text and barcodes input.AddImage("img/Barcode.png"); // Process both text and barcodes var result = ocr.Read(input); // Extract barcode data foreach (var barcode in result.Barcodes) { Console.WriteLine($"Barcode Value: {barcode.Value}"); Console.WriteLine($"Type: {barcode.Type}, Location: {barcode.Location}"); } } ``` 如何访问详细的 OCR 结果和元数据? IronOCR 结果对象提供了全面的数据,高级开发人员可以利用这些数据开发复杂的应用程序。 每个 `OcrResult` 都包含分层集合:页面、段落、行、单词和字符。 所有元素都包括详细的元数据,如位置、字体信息和置信度分数。 单个元素(段落、单词、Barcode)可以导出为图像或位图,以便进一步处理: ```csharp using System; using IronOcr; using IronSoftware.Drawing; // Configure with barcode support IronTesseract ocr = new IronTesseract { Configuration = { ReadBarCodes = true } }; using OcrInput input = new OcrInput(); // Process multi-page document int[] pageIndices = { 1, 2 }; input.LoadImageFrames(@"img\Potter.tiff", pageIndices); OcrResult result = ocr.Read(input); // Navigate the complete results hierarchy foreach (var page in result.Pages) { // Page-level data int pageNumber = page.PageNumber; string pageText = page.Text; int pageWordCount = page.WordCount; // Extract page elements OcrResult.Barcode[] barcodes = page.Barcodes; AnyBitmap pageImage = page.ToBitmap(); double pageWidth = page.Width; double pageHeight = page.Height; foreach (var paragraph in page.Paragraphs) { // Paragraph properties int paragraphNumber = paragraph.ParagraphNumber; string paragraphText = paragraph.Text; double paragraphConfidence = paragraph.Confidence; var textDirection = paragraph.TextDirection; foreach (var line in paragraph.Lines) { // Line details including baseline information string lineText = line.Text; double lineConfidence = line.Confidence; double baselineAngle = line.BaselineAngle; double baselineOffset = line.BaselineOffset; foreach (var word in line.Words) { // Word-level data string wordText = word.Text; double wordConfidence = word.Confidence; // Font information (when available) if (word.Font != null) { string fontName = word.Font.FontName; double fontSize = word.Font.FontSize; bool isBold = word.Font.IsBold; bool isItalic = word.Font.IsItalic; } foreach (var character in word.Characters) { // Character-level analysis string charText = character.Text; double charConfidence = character.Confidence; // Alternative character choices for spell-checking OcrResult.Choice[] alternatives = character.Choices; } } } } } ``` 摘要向前迈进源代码下载 常见问题解答 如何在不使用 Tesseract 的情况下,将图像转换为 C# 中的文本? 您可以使用 IronOCR 在 C# 中将图像转换为文本,而无需 Tesseract。IronOCR 通过内置方法直接处理图像到文本的转换,简化了该过程。 如何提高低质量图像的 OCR 准确性? IronOCR 提供如 Input.Deskew() 和 Input.DeNoise() 的图像滤镜,可以通过校正偏斜和减少噪声来增强低质量图像,从而显著提高 OCR 准确性。 在 C# 中使用 OCR 从多页文档中提取文本的步骤是什么? 要从多页文档中提取文本,IronOCR 允许您使用 LoadPdf() 方法来加载和处理每一页的 PDF 文件或处理 TIFF 文件,有效地将每一页转换为文本。 是否可以同时从图像中读取条码和文本? 是的,IronOCR 可以从一个图像中同时读取文本和条码。您可以启用条码读取 ocr.Configuration.ReadBarCodes = true,这使得能够提取文本和条码数据。 如何设置 OCR 以处理多语言文档? IronOCR 支持超过 125 种语言,您可以使用 ocr.Language 设置主要语言,并通过 ocr.AddSecondaryLanguage() 添加其他语言,以实现多语言文档处理。 有哪些方法可以以不同格式导出 OCR 结果? IronOCR 提供几种导出 OCR 结果的方法,例如用于 PDF 的 SaveAsSearchablePdf(),用于纯文本的 SaveAsTextFile(),以及用于 HOCR HTML 格式的 SaveAsHocrFile()。 如何优化大图像文件的 OCR 处理速度? 要优化 OCR 处理速度,请使用 IronOCR 的 OcrLanguage.EnglishFast 以加快语言识别,并定义特定的 OCR 区域 System.Drawing.Rectangle 以减少处理时间。 如何处理受保护 PDF 文件的 OCR 处理程序? 在处理受保护的 PDF 时,使用正确密码与 LoadPdf() 方法配合使用。IronOCR 通过自动将页面转换为图像来处理基于图像的 PDF,以进行 OCR 处理。 如果 OCR 结果不准确我该怎么办? 如果 OCR 结果不准确,请考虑使用 IronOCR 的图像增强功能,如 Input.Deskew() 和 Input.DeNoise(),并确保安装了正确的语言包。 我可以自定义 OCR 过程以排除某些字符吗? 可以,IronOCR 允许通过使用 BlackListCharacters 属性自定义 OCR 过程,以排除特定字符,从而通过专注于相关文本提高准确率和处理速度。 Jacob Mellor 立即与工程团队聊天 首席技术官 Jacob Mellor 是 Iron Software 的首席技术官,是 C# PDF 技术的先锋工程师。作为 Iron Software 核心代码库的原始开发者,自公司成立以来,他就塑造了公司的产品架构,并与首席执行官 Cameron Rimington 一起将其转变成一家公司,拥有50多人,服务于 NASA、特斯拉和全球政府机构。Jacob 拥有曼彻斯特大学 (1998-2001) 的一级荣誉土木工程学士学位。1999 年在伦敦创办了自己的第一家软件公司,并于 2005 年创建了他的第一个 .NET 组件后,他专注于解决微软生态系统中的复杂问题。他的旗舰 IronPDF 和 IronSuite .NET 库在全球已获得超过 3000 万次的 NuGet 安装,其基础代码继续为全球使用的开发者工具提供支持。拥有 25 年商业经验和 41 年编程经验的 Jacob 仍专注于推动企业级 C#、Java 和 Python PDF 技术的创新,同时指导下一代技术领导者。 审核者 Jeffrey T. Fritz 首席项目经理 - .NET 社区团队 Jeff 也是 .NET 和 Visual Studio 团队的首席项目经理。他是 .NET Conf 虚拟会议系列的执行制片人,并主持“Fritz and Friends”直播节目,每周两次与观众一起谈论技术并编写代码。Jeff 撰写研讨会、演示文稿并计划包括 Microsoft Build、Microsoft Ignite、.NET Conf 和 Microsoft MVP 峰会在内的最大型微软开发者活动的内容。 准备开始了吗? Nuget 下载 5,044,537 | 版本: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:5,044,537 查看许可证