跳過到頁腳內容
使用 IRONOCR

如何在 C# 中從螢幕截圖中提取文本

許多人可能會好奇「什麼是OCR截圖?」其他人可能會想知道如何將任何文本的截圖轉換為可編輯的數字文本格式或.txt或.doc格式。 如果您是這些人之一,那麼不用擔心,因為我們有完美的解決方案為您提供。

在本文中,我們將討論一些可以讓您在截圖上執行OCR(光學字符識別)的工具。

市面上有很多OCR工具,但今天我們將使用IronOCR來從截圖中提取文本。

1. IronOCR

IronOCR是一個用於C#和VB.NET編程語言的軟件庫,旨在使開發人員能夠為其應用程序添加OCR(光學字符識別)功能。 該庫可以用於識別圖像中的文字並將其轉換為機器可讀文本。 該庫是基於Tesseract OCR引擎構建的,這被認為是現可用的最精確的OCR引擎之一。

IronOCR可以用來從多種文件格式的圖像中讀取文本,包括PNG,JPG,TIFF,和PDF。 它還提供了一系列先進的文本識別功能,例如識別多種語言的能力,以及從旋轉或傾斜的圖像中識別文本的能力。 此外,開發者可以使用IronOCR快速將OCR功能集成到他們的應用程式中,因為它提供了一個簡單易用的API,可以從C#或VB.NET代碼中調用。 使用IronOCR,您可以選擇您的OCR語言,並對圖片、數字PDF文件和掃描PDF文件執行OCR。

IronOCR被認為是開發人員想要為他們的應用程序添加OCR功能的一個不錯選擇。 它是開源的,易於使用和集成,快速,準確,並且跟隨最新的OCR技術。

2. IronOCR 功能

IronOCR提供了廣泛的功能來幫助開發者將OCR功能集成到其應用中。 IronOCR的一些主要功能包括:

  1. 多語言支持:IronOCR可以識別超過60種語言的文本,包括英語、西班牙語、德語、法語、意大利語和中文。
  2. 自動檢測文本方向:IronOCR可以自動檢測圖像中文字的方向,即便圖像已經旋轉或傾斜。
  3. Support for a wide range of image formats: IronOCR can read text from images in many different file formats, including PNG, JPG, TIFF, and PDF.
  4. 可定制的識別設置:開發者可以定制識別設置以提高特定類型圖像或使用情境的識別準確性。
  5. 能夠從多頁的掃描文檔和PDF中識別文本。
  6. 快速識別和高精度:IronOCR使用Tesseract OCR引擎,這是目前最精確且廣泛使用的OCR引擎之一。
  7. 易用的API:IronOCR提供了一個簡單、易用的API,可以從C#或VB.NET代碼中調用,這使得將OCR功能集成到任何應用程序中變得簡單。

總體而言,IronOCR是一個強大的工具,提供了廣泛的功能來幫助開發者為其應用程序添加OCR功能。

3. 在Visual Studio中創建新項目

打開 Visual Studio 並轉到文件菜單。 選擇“新建項目”,然後選擇控制台應用程序。

輸入專案名稱並在適當的文本框中選擇路徑。 然後,點擊創建按鈕。 選擇所需的.NET Framework,如下圖所示:

如何在C#中執行OCR從截圖中獲取文本,圖1:在Visual Studio中創建一個新項目 在Visual Studio中創建一個新項目

Visual Studio項目現在將生成控制台應用程序的結構。 完成後,它將打開program.cs文件,您可以在其中編寫和執行源代碼。

如何在C#中執行OCR從截圖中獲取文本,圖2:由Visual Studio的新建項目向導生成的program.cs文件 由Visual Studio的新建項目向導生成的program.cs文件

現在我們可以添加IronOCR庫並測試程序。

4. 安裝IronOCR

在Visual Studio中,您可以輕鬆地將IronOCR與您的C#項目集成。

IronOCR提供了多種方式來與C# .NET項目集成。 在這裡,我們將討論其中一種:使用NuGet包管理器安裝IronOCR。

在Visual Studio中,前往工具 > NuGet包管理器 > 包管理器控制台

如何在C#中執行OCR從截圖中獲取文本,圖3:NuGet包管理器UI NuGet包管理器UI

點擊後,一個新的控制台將出現在Visual Studio窗口的底部。 在控制台中鍵入以下命令,然後按下回車鍵。

Install-Package IronOcr

IronOCR將在幾秒鐘內安裝完成。

5. 使用IronOCR在截圖上執行OCR

IronOCR是一個強大的OCR庫,可用於從截圖中識別文本。 使用IronOCR,您可以截取文本的截圖,然後使用庫的OCR功能將截圖中的文本轉換為數字、可編輯的格式。 這裡是一個使用IronOCR在C#中對截圖進行OCR的示例。 要執行截圖OCR,只需截取截圖並運行以下代碼以將文本提取到您想要的任何輸出格式。

using IronOcr;
using System;

class Program
{
    static void Main()
    {
        // Create an instance of IronTesseract, the core OCR engine
        var ocr = new IronTesseract();

        // Perform OCR on the specified image file
        var result = ocr.Read("ocr.png");

        // Output the recognized text to the console
        Console.WriteLine(result.Text);
    }
}
using IronOcr;
using System;

class Program
{
    static void Main()
    {
        // Create an instance of IronTesseract, the core OCR engine
        var ocr = new IronTesseract();

        // Perform OCR on the specified image file
        var result = ocr.Read("ocr.png");

        // Output the recognized text to the console
        Console.WriteLine(result.Text);
    }
}
Imports IronOcr
Imports System

Friend Class Program
	Shared Sub Main()
		' Create an instance of IronTesseract, the core OCR engine
		Dim ocr = New IronTesseract()

		' Perform OCR on the specified image file
		Dim result = ocr.Read("ocr.png")

		' Output the recognized text to the console
		Console.WriteLine(result.Text)
	End Sub
End Class
$vbLabelText   $csharpLabel

輸入圖像文件

如何在C#中執行OCR從截圖中獲取文本,圖4:用於輸入的示例截圖 用於輸入的示例截圖

文本輸出

- IRONOCR for NET
- The C# OCR Library
- OCR for C# to scan and read images & PDFs
- NET OCR library with 125+ global language packs
- Output as text, structured data, or searchable PDFs
- Supports NET 6, 5, Core, Standard, Framework

6. 使用IronOCR對特定區域執行OCR

IronOCR允許您對圖像中的特定區域執行OCR。 這在圖像包含多個文本區域但您只想識別特定區域的文本時非常有用。 下面顯示了一個示例代碼。

using IronOcr;
using IronSoftware.Drawing;
using System;

class Program
{
    static void Main()
    {
        var ocrTesseract = new IronTesseract();

        using (var ocrInput = new OcrInput())
        {
            // Define the rectangle to crop the image for OCR
            var contentArea = new CropRectangle(x: 0, y: 0, width: 350, height: 150);

            // Add the image with the specified cropping area
            ocrInput.AddImage("ocr.png", contentArea);

            // Perform the OCR operation on the defined area
            var ocrResult = ocrTesseract.Read(ocrInput);

            // Output the recognized text
            Console.WriteLine(ocrResult.Text);
        }
    }
}
using IronOcr;
using IronSoftware.Drawing;
using System;

class Program
{
    static void Main()
    {
        var ocrTesseract = new IronTesseract();

        using (var ocrInput = new OcrInput())
        {
            // Define the rectangle to crop the image for OCR
            var contentArea = new CropRectangle(x: 0, y: 0, width: 350, height: 150);

            // Add the image with the specified cropping area
            ocrInput.AddImage("ocr.png", contentArea);

            // Perform the OCR operation on the defined area
            var ocrResult = ocrTesseract.Read(ocrInput);

            // Output the recognized text
            Console.WriteLine(ocrResult.Text);
        }
    }
}
Imports IronOcr
Imports IronSoftware.Drawing
Imports System

Friend Class Program
	Shared Sub Main()
		Dim ocrTesseract = New IronTesseract()

		Using ocrInput As New OcrInput()
			' Define the rectangle to crop the image for OCR
			Dim contentArea = New CropRectangle(x:= 0, y:= 0, width:= 350, height:= 150)

			' Add the image with the specified cropping area
			ocrInput.AddImage("ocr.png", contentArea)

			' Perform the OCR operation on the defined area
			Dim ocrResult = ocrTesseract.Read(ocrInput)

			' Output the recognized text
			Console.WriteLine(ocrResult.Text)
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

輸出

- IRONOCR for NET
- The C# OCR Library
- OCR for C# to scan and read images & PDFs
- NET OCR library with 125+ global language packs

7. 使用IronOCR對圖像執行OCR

要對圖像執行OCR並將識別的文本保存到.txt文件中,您可以使用以下代碼。

using IronOcr;
using System;

class Program
{
    static void Main()
    {
        var ocr = new IronTesseract();
        using (var input = new OcrInput("ocr.png"))
        {
            // Perform OCR on the image
            var result = ocr.Read(input);

            // Save the recognized text to a .txt file
            result.SaveAsTextFile("output.txt");
        }
    }
}
using IronOcr;
using System;

class Program
{
    static void Main()
    {
        var ocr = new IronTesseract();
        using (var input = new OcrInput("ocr.png"))
        {
            // Perform OCR on the image
            var result = ocr.Read(input);

            // Save the recognized text to a .txt file
            result.SaveAsTextFile("output.txt");
        }
    }
}
Imports IronOcr
Imports System

Friend Class Program
	Shared Sub Main()
		Dim ocr = New IronTesseract()
		Using input = New OcrInput("ocr.png")
			' Perform OCR on the image
			Dim result = ocr.Read(input)

			' Save the recognized text to a .txt file
			result.SaveAsTextFile("output.txt")
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

輸出文件的內容如下所示:

如何在C#中執行OCR從截圖中獲取文本,圖5:生成的output.txt文件的內容 生成的output.txt文件的內容

8. 了解更多

閱讀圖像文本提取教程以獲取有關如何對圖像執行OCR的更多信息。

IronOCR是套件中五個.NET庫的一部分,這些庫設計用於處理不同類型的文檔。 您可以用兩個許可證的價格購買所有五個庫。

常見問題解答

如何使用 C# 中的 OCR 從螢幕截圖中提取文字?

您可以在 C# 中使用 IronOCR 從螢幕截圖中提取文字,通過其簡單的 API 將螢幕截圖轉換為可編輯的數字文字格式。首先通過 NuGet 在 Visual Studio 中安裝 IronOCR,然後使用 IronOCR 提供的代碼示例對您的螢幕截圖圖片進行 OCR。

什麼是光學字符識別 (OCR)?

光學字符識別 (OCR) 是一項將各類文檔(如掃描紙質文檔、PDF 文件或數碼相機拍攝的圖片)轉換為可編輯和可搜索數據的技術。IronOCR 是一個 C# 庫,可以在應用程序中輔助實現 OCR。

IronOCR 可以處理多種語言的 OCR 嗎?

是的,IronOCR 支援超過 60 種語言的文字識別,使其適用於國際化應用程序。它提供設置語言偏好的選項以確保準確的文字提取。

IronOCR 支持哪些圖片格式進行 OCR?

IronOCR 支援多種圖像格式進行 OCR,包括 PNG、JPG、TIFF 和 PDF。這種靈活性允許開發人員在使用各種圖像來源時不必手動轉換格式。

文字方向會如何影響 OCR 的準確度?

文字方向會大大影響 OCR 的準確性。IronOCR 自動檢測和校正圖像中的文字方向,確保旋轉或傾斜的文字能夠準確識別並轉換為數字格式。

如何在 C# 項目中安裝 IronOCR?

要在 C# 項目中安裝 IronOCR,使用 Visual Studio 中的 NuGet 封裝管理器。搜索 IronOCR 並將其安裝到您的項目中,以開始使用其 OCR 功能從圖像中提取文字。

使用 IronOCR 進行文字識別的優勢是什麼?

IronOCR 提供了幾個優勢,包括強大的多語言支持、自動文本方向校正、支持多種圖像格式以及可自定義設置以提高識別準確性。其簡單的 API 使其易於集成到 C# 應用程序中。

IronOCR 適合在圖像的特定區域識別文字嗎?

是的,IronOCR 允許開發人員在圖像中定義特定區域進行 OCR,使目標文字提取成為可能。此功能在圖像只有部分包含相關文字的場景中特別有用。

有關 OCR 問題的一些常見故障排除技巧是什麼?

常見的 OCR 問題故障排除技巧包括確保圖像清晰且分辨率高、檢查文字方向、確保設置了正確的語言,以及更新到最新版本的 IronOCR 以獲得最佳表現。

如何將 OCR 結果轉換為 .txt 或 .doc 文件?

使用 IronOCR,您可以將 OCR 結果轉換為 .txt 或 .doc 文件 ,通過從圖像中提取文字並使用 C# 中的標準文件 I/O 操作將其保存。這使您可以從基於圖像的文字創建可編輯文檔。

Kannaopat Udonpant
軟體工程師
在成為软件工程師之前,Kannapat 從日本北海道大學完成了環境資源博士學位。在追逐學位期间,Kannapat 還成為了生產工程系一部份——汽車机器人实验室的成員。2022 年,他利用他的 C# 技能加入 Iron Software 的工程團隊, 專注於 IronPDF。Kannapat 珍惜他的工作,因为他直接向编写大部分 IronPDF 使用的代码的开发者学习。除了同行学习,Kannapat 还喜欢在 Iron Software 工作的社交十环。当他不编写代码或文档时,Kannapat 通常在他的 PS5 上打游戏或重看《The Last of Us》。