Get started with IronOCR

Start using IronOCR in your project today with a free trial.

First Step:
green arrow pointer



Read Images Example

Begin by instantiating the IronTesseract class to enable OCR. Utilize the 'using' statement to create an OcrImageInput object, specifying the image file path. This ensures the proper disposal of resources when they are no longer needed. IronOCR supports input images in various formats, including jpg, png, gif, tiff, and bmp. Finally, use the Read method to perform OCR.

:path=/static-assets/ocr/content-code-examples/how-to/input-images-read.cs
using IronOcr;

// This code utilizes the IronOCR library to perform OCR (Optical Character Recognition)
// on an image file, extracting text from the image.
// Ensure you have installed the IronOCR NuGet package to run this code.

// Instantiate an instance of IronTesseract, the main OCR engine in the IronOCR library.
var ocrTesseract = new IronTesseract();

// Open an image file as an input for OCR processing.
// Here, "Potter.png" is used as the sample image.
// The 'using' statement ensures that the resource is disposed of correctly after use.
using (var imageInput = new OcrInput("Potter.png"))
{
    // Perform OCR on the input image.
    // The 'Read' method processes the image and returns the extracted text in an OcrResult object.
    OcrResult ocrResult = ocrTesseract.Read(imageInput);

    // Display the extracted text or use it as needed.
    // 'System.Console.WriteLine' is used to output the text to the console.
    System.Console.WriteLine(ocrResult.Text);
}
Imports IronOcr



' This code utilizes the IronOCR library to perform OCR (Optical Character Recognition)

' on an image file, extracting text from the image.

' Ensure you have installed the IronOCR NuGet package to run this code.



' Instantiate an instance of IronTesseract, the main OCR engine in the IronOCR library.

Private ocrTesseract = New IronTesseract()



' Open an image file as an input for OCR processing.

' Here, "Potter.png" is used as the sample image.

' The 'using' statement ensures that the resource is disposed of correctly after use.

Using imageInput = New OcrInput("Potter.png")

	' Perform OCR on the input image.

	' The 'Read' method processes the image and returns the extracted text in an OcrResult object.

	Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)



	' Display the extracted text or use it as needed.

	' 'System.Console.WriteLine' is used to output the text to the console.

	System.Console.WriteLine(ocrResult.Text)

End Using
$vbLabelText   $csharpLabel
Read PNG image

Visit the How to Read Multi-Frame/Page GIFs and TIFFs article to learn more about reading TIFF and GIF images.

Import Images as Bytes

Apart from the plain old filepath, the OcrImageInput class also accepts image information in the form of bytes, AnyBitmap, Stream, as well as Image. The AnyBitmap is a bitmap object of IronSoftware.Drawing.AnyBitmap.

:path=/static-assets/ocr/content-code-examples/how-to/input-images-import-byte.cs
using IronOcr;
using System.IO;

// Create an instance of IronTesseract for performing OCR operations.
IronTesseract ocrTesseract = new IronTesseract();

// Read all bytes from the specified image file. This file should exist in the same directory as the executable or provide the full path.
byte[] data = File.ReadAllBytes("Potter.tiff");

// Using the byte array, create an instance of OcrInput. This object represents the input image for OCR processing.
using var imageInput = new OcrInput(data);

// Perform OCR on the provided image input. The OCR engine extracts the text content from the image.
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Output the recognized text to the console. The recognized text is obtained from the OcrResult object.
System.Console.WriteLine(ocrResult.Text);
Imports IronOcr

Imports System.IO



' Create an instance of IronTesseract for performing OCR operations.

Private ocrTesseract As New IronTesseract()



' Read all bytes from the specified image file. This file should exist in the same directory as the executable or provide the full path.

Private data() As Byte = File.ReadAllBytes("Potter.tiff")



' Using the byte array, create an instance of OcrInput. This object represents the input image for OCR processing.

Private imageInput = New OcrInput(data)



' Perform OCR on the provided image input. The OCR engine extracts the text content from the image.

Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)



' Output the recognized text to the console. The recognized text is obtained from the OcrResult object.

System.Console.WriteLine(ocrResult.Text)
$vbLabelText   $csharpLabel

Specify Scan Region

A CropRectangle is also accepted when instantiating the OcrImageInput class. This allows you to specify which region of the image document should be OCR'ed. Depending on the image document, specifying the region to scan can significantly improve performance. In the code example below, you specify that only the chapter number and title should be read.

:path=/static-assets/ocr/content-code-examples/how-to/input-images-read-specific-region.cs
// Using directives for necessary namespaces
using IronOcr;
using IronSoftware.Drawing;
using System;

// Instantiate IronTesseract, a library for OCR (Optical Character Recognition)
IronTesseract ocrTesseract = new IronTesseract();

// Specify the crop region - the rectangular area of the image to be analyzed
// Parameters are (x: start position, y: start position, width: width of the rectangle, height: height of the rectangle)
var scanRegion = new System.Drawing.Rectangle(800, 200, 900, 400);

try
{
    // Load the image from given file and specify the region for OCR by setting ContentArea property
    using var imageInput = new OcrInput("Potter.tiff");

    // Ensure the region is set only if it's within the bounds of the image
    imageInput.ContentArea = scanRegion;
    
    // Perform OCR on the specified region of the image
    OcrResult ocrResult = ocrTesseract.Read(imageInput);

    // Output the recognized text to the console
    Console.WriteLine(ocrResult.Text);
}
catch (Exception ex)
{
    // Catch and display any exceptions during image processing or OCR
    Console.WriteLine($"An error occurred: {ex.Message}");
}
' Using directives for necessary namespaces

Imports IronOcr

Imports IronSoftware.Drawing

Imports System



' Instantiate IronTesseract, a library for OCR (Optical Character Recognition)

Private ocrTesseract As New IronTesseract()



' Specify the crop region - the rectangular area of the image to be analyzed

' Parameters are (x: start position, y: start position, width: width of the rectangle, height: height of the rectangle)

Private scanRegion = New System.Drawing.Rectangle(800, 200, 900, 400)



Try

	' Load the image from given file and specify the region for OCR by setting ContentArea property

	Dim imageInput = New OcrInput("Potter.tiff")



	' Ensure the region is set only if it's within the bounds of the image

	imageInput.ContentArea = scanRegion



	' Perform OCR on the specified region of the image

	Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)



	' Output the recognized text to the console

	Console.WriteLine(ocrResult.Text)

Catch ex As Exception

	' Catch and display any exceptions during image processing or OCR

	Console.WriteLine($"An error occurred: {ex.Message}")

End Try
$vbLabelText   $csharpLabel

OCR Result

Read specific region

Frequently Asked Questions

What is Optical Character Recognition (OCR)?

OCR is a technology used to recognize and extract text from images. It is especially useful for digitizing printed documents, allowing you to work with textual content from scanned pages, photographs, or other image files.

Which image formats are supported?

Using IronOCR, you can work with various image formats, including jpg, png, gif, tiff, and bmp.

How can I get started with image reading in C#?

To get started, download the IronOCR library from NuGet, instantiate the IronTesseract class, and use the OcrImageInput class to input an image and perform OCR using the Read method.

How do I read an image in .NET?

Instantiate the IronTesseract class, use a 'using' statement to create an OcrInput object with the image file path, and call the Read method to perform OCR using IronOCR.

Can I read images from a byte array in C#?

Yes, by using IronOCR, you can input images in the form of bytes, AnyBitmap, Stream, and Image. Use the OcrInput class with a byte array to perform OCR on image data.

How can I specify a scan region for OCR in C# applications?

With IronOCR, you can specify a scan region by using a CropRectangle when instantiating the OcrImageInput class. This allows you to define which region of the image should be OCR'ed, potentially improving performance.

What is the purpose of using a 'using' statement in image processing?

The 'using' statement in IronOCR ensures proper disposal of resources when they are no longer needed, which is crucial for efficient memory management during OCR operations.

Can multi-frame or multi-page images be handled in C#?

Yes, with IronOCR, you can read multi-frame or multi-page images such as GIFs and TIFFs. Refer to the specific documentation for detailed instructions.

Chaknith related to OCR Result
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.