Read Screenshot

This code example demonstrates how to use the IronTesseract OCR engine to extract text from a screenshot image.

// Import necessary namespaces
using System;
using System.Linq; // For using LINQ operations such as First() and Last()
using IronOcr; // For the IronTesseract library

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

        // Initialize an OcrInput object and load the image containing the screenshot
        var input = new OcrInput();
        input.AddImage("screenshotOCR.png");

        // Process the screenshot using the OCR engine
        // The Read method returns an OcrResult object containing the recognized text and related regions
        OcrResult result = Ocr.Read(input);

        // Print the extracted text from the screenshot
        Console.WriteLine("Extracted Text:");
        Console.WriteLine(result.Text);

        // Print the X coordinate of the first recognized text region
        Console.WriteLine("\nX Coordinate of First Recognized Text Region:");
        Console.WriteLine(result.TextRegions.First().Location.X);

        // Print the width of the last recognized text region
        Console.WriteLine("\nWidth of Last Recognized Text Region:");
        Console.WriteLine(result.TextRegions.Last().Location.Width);

        // Print the OCR confidence score
        Console.WriteLine("\nOCR Confidence Score:");
        Console.WriteLine(result.Confidence);
    }
}
// Import necessary namespaces
using System;
using System.Linq; // For using LINQ operations such as First() and Last()
using IronOcr; // For the IronTesseract library

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

        // Initialize an OcrInput object and load the image containing the screenshot
        var input = new OcrInput();
        input.AddImage("screenshotOCR.png");

        // Process the screenshot using the OCR engine
        // The Read method returns an OcrResult object containing the recognized text and related regions
        OcrResult result = Ocr.Read(input);

        // Print the extracted text from the screenshot
        Console.WriteLine("Extracted Text:");
        Console.WriteLine(result.Text);

        // Print the X coordinate of the first recognized text region
        Console.WriteLine("\nX Coordinate of First Recognized Text Region:");
        Console.WriteLine(result.TextRegions.First().Location.X);

        // Print the width of the last recognized text region
        Console.WriteLine("\nWidth of Last Recognized Text Region:");
        Console.WriteLine(result.TextRegions.Last().Location.Width);

        // Print the OCR confidence score
        Console.WriteLine("\nOCR Confidence Score:");
        Console.WriteLine(result.Confidence);
    }
}
' Import necessary namespaces
Imports Microsoft.VisualBasic
Imports System
Imports System.Linq ' For using LINQ operations such as First() and Last()
Imports IronOcr ' For the IronTesseract library

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

		' Initialize an OcrInput object and load the image containing the screenshot
		Dim input = New OcrInput()
		input.AddImage("screenshotOCR.png")

		' Process the screenshot using the OCR engine
		' The Read method returns an OcrResult object containing the recognized text and related regions
		Dim result As OcrResult = Ocr.Read(input)

		' Print the extracted text from the screenshot
		Console.WriteLine("Extracted Text:")
		Console.WriteLine(result.Text)

		' Print the X coordinate of the first recognized text region
		Console.WriteLine(vbLf & "X Coordinate of First Recognized Text Region:")
		Console.WriteLine(result.TextRegions.First().Location.X)

		' Print the width of the last recognized text region
		Console.WriteLine(vbLf & "Width of Last Recognized Text Region:")
		Console.WriteLine(result.TextRegions.Last().Location.Width)

		' Print the OCR confidence score
		Console.WriteLine(vbLf & "OCR Confidence Score:")
		Console.WriteLine(result.Confidence)
	End Sub
End Class
$vbLabelText   $csharpLabel

Explanation:

  1. Namespace Imports: The code imports necessary namespaces, including System.Linq for LINQ operations and IronOcr for OCR functionalities.

  2. Creating OCR Engine: An instance of the IronTesseract class is created to use the OCR engine's capabilities for text recognition.

  3. Loading the Image: An OcrInput object is initialized to load the image (screenshotOCR.png) from which text needs to be extracted.

  4. Reading the Image:

    • The Read method of the IronTesseract instance processes the image and returns an OcrResult containing the recognized text.
    • It also contains details about the regions where text was identified.
  5. Outputs:
    • Extracted text is printed using result.Text.
    • The X coordinate of the region of the first recognized text is printed.
    • The width of the region of the last recognized text is printed.
    • The OCR confidence score, indicating the accuracy of the text recognition, is printed.

This method allows for the extraction of text and region-specific details from screenshots, which is particularly useful for capturing text in graphical content or user interfaces.