Passer au contenu du pied de page
UTILISATION D'IRONOCR

Comment obtenir du texte à partir d'une capture d'écran en C#

Many people out there may be wondering "What is an OCR Screenshot?" Others might wonder how to convert a screenshot of any text into a digital text-editable format or to .txt, or .doc format. If you are one of these people, then worry no more because we have the perfect solutions for you.

In this article, we will discuss different tools that will allow you to perform OCR, Optical Character Recognition, on screenshots.

There are many OCR tools out there but today we will be using IronOCR to extract text from screenshots.

1. IronOCR

IronOCR is a software library for the C# and VB.NET programming languages, designed to enable developers to add OCR (Optical Character Recognition) capabilities to their applications. The library can be used to recognize text in images and convert it into machine-readable text. The library is built on the Tesseract OCR engine, which is considered one of the most accurate OCR engines available.

IronOCR can be used to read text from images in many different file formats, including PNG, JPG, TIFF, and PDF. It also provides a range of advanced features for working with text recognition, such as the ability to recognize multiple languages, as well as the ability to recognize text from images that have been rotated or skewed. Additionally, developers can use IronOCR to quickly integrate OCR functionality into their applications, as it provides a simple, easy-to-use API that can be called from C# or VB.NET code. Using IronOCR, you can choose your OCR language, and perform OCR on images, digital PDF files, and scanned PDF files.

IronOCR is considered a good option for developers who want to add OCR functionality to their applications. It's open-source, easy to use and integrate, fast, accurate, and up-to-date with the latest OCR technologies.

2. IronOCR Features

IronOCR provides a wide range of features to help developers integrate OCR functionality into their applications. Some of the key features of IronOCR include:

  1. Multi-language support: IronOCR can recognize text in over 60 languages, including English, Spanish, German, French, Italian, and Chinese.
  2. Automatic detection of text orientation: IronOCR can automatically detect the orientation of text in an image, even if the image has been rotated or skewed.
  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. Customizable recognition settings: Developers can customize the recognition settings to improve recognition accuracy for specific types of images or use cases.
  5. Ability to recognize text from scanned documents and PDFs with multiple pages.
  6. Fast recognition and high accuracy: IronOCR uses the Tesseract OCR engine, which is one of the most accurate and widely used OCR engines available.
  7. Easy-to-use API: IronOCR provides a simple, easy-to-use API that can be called from C# or VB.NET code, which makes it easy to integrate OCR functionality into any application.

Overall, IronOCR is a powerful tool that provides a wide range of features to help developers add OCR functionality to their applications.

3. Creating a New Project in Visual Studio

Open Visual Studio and go to the File menu. Select "New Project" and then select Console Application.

Enter the project name and select the path in the appropriate text box. Then, click the Create button. Select the required .NET Framework, as in the screenshot below:

How to OCR Get Text From Screenshot in C#, Figure 1: Creating a New Project in Visual Studio Creating a New Project in Visual Studio

The Visual Studio project will now generate the structure for the console application. Once finished, it will open the program.cs file, in which you can write and execute source code.

How to OCR Get Text From Screenshot in C#, Figure 2: The program.cs file, generated from Visual Studio's New Project Wizard The program.cs file, generated from Visual Studio's New Project Wizard

Now we can add the IronOCR library and test the program.

4. Install IronOCR

In Visual Studio, you can easily integrate IronOCR with your C# project.

IronOCR offers multiple processes to integrate with a C# .NET project. Here, we'll discuss one of them: installing IronOCR using the NuGet Package Manager.

In Visual Studio go to Tools > NuGet Package Manager > Package Manager Console

How to OCR Get Text From Screenshot in C#, Figure 3: The NuGet Package Manager UI The NuGet Package Manager UI

After clicking, a new console will appear at the bottom of Visual Studio's window. Type the below command in the console and press enter.

Install-Package IronOcr

IronOCR will be installed in just a few seconds.

5. Using IronOCR to Perform OCR on a Screenshot

IronOCR is a powerful OCR library that can be used to recognize text from screenshots. With IronOCR, you can take a screenshot of text, and then use the library's OCR capabilities to convert the text in the screenshot into a digital, editable format. Here's an example of how you might use IronOCR to perform OCR on a screenshot in C#. To perform screenshot OCR, just capture a screenshot and run the below code to extract the text to any output format you want.

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

Input Image file

How to OCR Get Text From Screenshot in C#, Figure 4: Sample Screenshot used for input Sample Screenshot used for input

Text Output

- 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. Using IronOCR to Perform OCR on a Specific Zone

IronOCR allows you to perform OCR on specific zones within an image. This can be useful when the image contains multiple regions of text, and you only want to recognize the text within a specific region. An example code for this is shown below.

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

Output

- 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. Using IronOCR to Perform OCR on an Image

To perform OCR on an image and save the recognized text in a .txt file, you can use the following code.

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

The contents of the output file are shown below:

How to OCR Get Text From Screenshot in C#, Figure 5: Contents of the generated output.txt file Contents of the generated output.txt file

8. Learn More

Read the Image Text Extraction tutorial for more information about how to perform OCR on images.

IronOCR is part of a suite of five .NET libraries designed to work with different types of documents. You can purchase all five libraries for the price of just two licenses.

Questions Fréquemment Posées

Comment puis-je extraire du texte d'une capture d'écran en utilisant OCR en C# ?

Vous pouvez utiliser IronOCR en C# pour extraire du texte d'une capture d'écran en exploitant son API simple pour convertir la capture d'écran en un format texte numérique éditable. Installez d'abord IronOCR via NuGet dans Visual Studio, puis utilisez les exemples de code fournis par IronOCR pour effectuer un OCR sur votre image de capture d'écran.

Qu'est-ce que la reconnaissance optique de caractères (OCR) ?

La reconnaissance optique de caractères (OCR) est une technologie qui convertit différents types de documents, tels que documents papier numérisés, fichiers PDF ou images capturées par un appareil photo numérique, en données éditables et consultables. IronOCR est une bibliothèque C# qui facilite l'OCR dans les applications.

IronOCR peut-il gérer plusieurs langues pour l'OCR ?

Oui, IronOCR prend en charge la reconnaissance de texte dans plus de 60 langues, ce qui le rend polyvalent pour les applications internationales. Il offre des options pour définir les préférences linguistiques afin d'assurer une extraction de texte précise.

Quel format d'image IronOCR prend-il en charge pour l'OCR ?

IronOCR prend en charge divers formats d'image pour l'OCR, y compris PNG, JPG, TIFF et PDF. Cette flexibilité permet aux développeurs de travailler avec une large gamme de sources d'image sans avoir besoin de convertir les formats manuellement.

Comment l'orientation du texte peut-elle affecter la précision de l'OCR ?

L'orientation du texte peut considérablement impacter la précision de l'OCR. IronOCR détecte et corrige automatiquement l'orientation du texte dans les images, garantissant que le texte tourné ou incliné est reconnu et converti avec précision en format numérique.

Comment installer IronOCR dans un projet C# ?

Pour installer IronOCR dans un projet C#, utilisez le Gestionnaire de paquet NuGet dans Visual Studio. Recherchez IronOCR et installez-le dans votre projet pour commencer à utiliser ses capacités d'OCR pour l'extraction de texte à partir d'images.

Quels sont les avantages d'utiliser IronOCR pour la reconnaissance de texte ?

IronOCR offre plusieurs avantages, notamment un support multilingue robuste, une correction automatique de l'orientation du texte, la prise en charge de multiples formats d'image et des réglages personnalisables pour améliorer la précision de la reconnaissance. Son API simple facilite une intégration facile dans les applications C#.

IronOCR est-il adapté pour reconnaître du texte dans des zones spécifiques d'une image ?

Oui, IronOCR permet aux développeurs de définir des zones spécifiques au sein d'une image pour effectuer un OCR, permettant ainsi une extraction de texte ciblée. Cette fonction est utile pour les scénarios où seule une partie de l'image contient le texte pertinent.

Quelles sont quelques astuces courantes de dépannage pour les problèmes d'OCR ?

Les astuces courantes de dépannage pour les problèmes d'OCR incluent s'assurer que l'image est claire et de haute résolution, vérifier l'orientation du texte, s'assurer que la langue correcte est définie, et mettre à jour vers la dernière version de IronOCR pour une performance optimale.

Comment puis-je convertir les résultats OCR en fichier .txt ou .doc ?

Avec IronOCR, vous pouvez convertir les résultats OCR en fichier .txt ou .doc en extrayant le texte de l'image et en l'enregistrant à l'aide d'opérations standard de lecture/écriture de fichiers en C#. Cela vous permet de créer des documents modifiables à partir de texte basé sur des images.

Kannaopat Udonpant
Ingénieur logiciel
Avant de devenir ingénieur logiciel, Kannapat a obtenu un doctorat en ressources environnementales à l'université d'Hokkaido au Japon. Pendant qu'il poursuivait son diplôme, Kannapat est également devenu membre du laboratoire de robotique de véhicules, qui fait partie du département de bioproduction. En 2022, il a utilisé ses compé...
Lire la suite