Get started with IronOCR

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

First Step:
green arrow pointer



Read System.Drawing.Bitmap Example

First, instantiate the IronTesseract class to perform OCR. Create a System.Drawing.Bitmap from one of the various methods. In the code example, I've decided to use a file path.

Next, use the using statement to create the OcrImageInput object, passing the image from the System.Drawing.Bitmap object to it. Finally, use the Read method to perform OCR.

:path=/static-assets/ocr/content-code-examples/how-to/input-system-drawing-read-bitmap.cs
using IronOcr;
using System.Drawing;

// This code snippet demonstrates how to perform Optical Character Recognition (OCR)
// using the IronTesseract library on a given image file. The image file is read into
// a Bitmap object, which is then processed to extract text using OCR and print the result
// to the console.

// Make sure that the IronOcr library is properly referenced in your project before trying to use it.

// Instantiate IronTesseract object
var ocrTesseract = new IronTesseract();

// Read the image file into a Bitmap object
// Ensure "Potter.tiff" exists in the executing directory, or specify the full path to the image
using (Bitmap bitmap = new Bitmap("Potter.tiff"))
{
    // Use the OcrInput class to prepare the Bitmap for OCR processing
    using (var imageInput = new OcrInput(bitmap))
    {
        // Perform OCR on the input image and store the text result
        OcrResult ocrResult = ocrTesseract.Read(imageInput);

        // Print the OCR result text to the console
        Console.WriteLine(ocrResult.Text);
    }
}

// Note: Ensure you handle exceptions and edge cases (like file not found) in real-world applications.
Imports IronOcr

Imports System.Drawing



' This code snippet demonstrates how to perform Optical Character Recognition (OCR)

' using the IronTesseract library on a given image file. The image file is read into

' a Bitmap object, which is then processed to extract text using OCR and print the result

' to the console.



' Make sure that the IronOcr library is properly referenced in your project before trying to use it.



' Instantiate IronTesseract object

Private ocrTesseract = New IronTesseract()



' Read the image file into a Bitmap object

' Ensure "Potter.tiff" exists in the executing directory, or specify the full path to the image

Using bitmap As New Bitmap("Potter.tiff")

	' Use the OcrInput class to prepare the Bitmap for OCR processing

	Using imageInput = New OcrInput(bitmap)

		' Perform OCR on the input image and store the text result

		Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)



		' Print the OCR result text to the console

		Console.WriteLine(ocrResult.Text)

	End Using

End Using



' Note: Ensure you handle exceptions and edge cases (like file not found) in real-world applications.
$vbLabelText   $csharpLabel

Read System.Drawing.Image Example

Reading from a System.Drawing.Image is as simple as creating the OcrImageInput object with the Image and then performing the standard OCR process using the Read method.

:path=/static-assets/ocr/content-code-examples/how-to/input-system-drawing-read-image.cs
using IronOcr;
using System;
using System.Drawing;

// This implementation utilizes IronTesseract to perform OCR (Optical Character Recognition)
// on an image file. The image is loaded, and text is extracted from it using IronTesseract.

try
{
    // Instantiate the OCR engine.
    IronTesseract ocrTesseract = new IronTesseract();

    // Open the image file as a System.Drawing.Image.
    // Ensure the file path is correct.
    using Image image = Image.FromFile("Potter.tiff");
    
    // Convert the image into an OCR-friendly format using OcrInput.
    using var imageInput = new OcrInput();
    imageInput.AddImage(image);
    
    // Perform OCR on the input image.
    OcrResult ocrResult = ocrTesseract.Read(imageInput);

    // Output the detected text to the console (or handle it as needed).
    Console.WriteLine("Extracted Text: ");
    Console.WriteLine(ocrResult.Text);
}
catch (Exception ex)
{
    // Handle any exceptions that occur during the OCR process.
    Console.WriteLine("An error occurred: " + ex.Message);
}
Imports IronOcr

Imports System

Imports System.Drawing



' This implementation utilizes IronTesseract to perform OCR (Optical Character Recognition)

' on an image file. The image is loaded, and text is extracted from it using IronTesseract.



Try

	' Instantiate the OCR engine.

	Dim ocrTesseract As New IronTesseract()



	' Open the image file as a System.Drawing.Image.

	' Ensure the file path is correct.

	Using image As Image = Me.Image.FromFile("Potter.tiff")

	

		' Convert the image into an OCR-friendly format using OcrInput.

		Dim imageInput = New OcrInput()

		imageInput.AddImage(image)

	

		' Perform OCR on the input image.

		Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)

	

		' Output the detected text to the console (or handle it as needed).

		Console.WriteLine("Extracted Text: ")

		Console.WriteLine(ocrResult.Text)

	End Using

Catch ex As Exception

	' Handle any exceptions that occur during the OCR process.

	Console.WriteLine("An error occurred: " & ex.Message)

End Try
$vbLabelText   $csharpLabel

Read IronSoftware.Drawing.AnyBitmap Example

Similarly, after creating or obtaining an AnyBitmap object, you can construct the OcrImageInput class. The constructor will take care of all the necessary steps to import the data. The code example below demonstrates this.

:path=/static-assets/ocr/content-code-examples/how-to/input-system-drawing-read-anybitmap.cs
using IronOcr;
using IronSoftware.Drawing;

public class OcrExample
{
    // Instantiate IronTesseract for Optical Character Recognition (OCR)
    private static readonly IronTesseract ocrTesseract = new IronTesseract();

    public static void Main()
    {
        // Load the image file as AnyBitmap. Ensure that the file path is correct and accessible.
        AnyBitmap anyBitmap = null;
        try
        {
            anyBitmap = AnyBitmap.FromFile("Potter.tiff");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error loading image: {ex.Message}");
            return;
        }

        // Use 'using' statement to ensure proper disposal of OcrImageInput resource.
        using (var imageInput = new OcrImageInput(anyBitmap))
        {
            try
            {
                // Perform OCR on the image to extract text content.
                OcrResult ocrResult = ocrTesseract.Read(imageInput);

                // Output the recognized text to the console or handle it as needed in your application.
                Console.WriteLine("Recognized Text:");
                Console.WriteLine(ocrResult.Text);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error during OCR process: {ex.Message}");
            }
        }
    }
}
Imports IronOcr

Imports IronSoftware.Drawing



Public Class OcrExample

	' Instantiate IronTesseract for Optical Character Recognition (OCR)

	Private Shared ReadOnly ocrTesseract As New IronTesseract()



	Public Shared Sub Main()

		' Load the image file as AnyBitmap. Ensure that the file path is correct and accessible.

		Dim anyBitmap As AnyBitmap = Nothing

		Try

			anyBitmap = AnyBitmap.FromFile("Potter.tiff")

		Catch ex As Exception

			Console.WriteLine($"Error loading image: {ex.Message}")

			Return

		End Try



		' Use 'using' statement to ensure proper disposal of OcrImageInput resource.

		Using imageInput = New OcrImageInput(anyBitmap)

			Try

				' Perform OCR on the image to extract text content.

				Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)



				' Output the recognized text to the console or handle it as needed in your application.

				Console.WriteLine("Recognized Text:")

				Console.WriteLine(ocrResult.Text)

			Catch ex As Exception

				Console.WriteLine($"Error during OCR process: {ex.Message}")

			End Try

		End Using

	End Sub

End Class
$vbLabelText   $csharpLabel

Specify Scan Region

In the construction of the OcrImageInput class, you can specify the area to scan. This allows you to define the specific region of the image document for OCR. Depending on the image document, specifying the scanning region can significantly enhance performance. In the provided code example, I specify that only the chapter number and title should be extracted.

: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 System.Drawing.Bitmap?

System.Drawing.Bitmap is a class in the .NET Framework used for working with bitmap images. It provides methods and properties to create, manipulate, and display bitmap images.

What is the role of System.Drawing.Image in .NET?

System.Drawing.Image is a base class for all GDI+ image objects in the .NET Framework, serving as the parent class for various image types, including System.Drawing.Bitmap.

How can I perform OCR on a bitmap image?

You can perform OCR on a System.Drawing.Bitmap by using a library like IronOCR, where you create an OcrImageInput object with the bitmap and then use the Read method to extract text.

How does a custom bitmap class help in .NET projects?

IronSoftware.Drawing.AnyBitmap, a bitmap class in the IronDrawing library, helps C# software engineers replace System.Drawing.Common in .NET projects on Windows, macOS, and Linux platforms.

How do you specify a scan region for OCR in an image processing library?

In IronOCR, you can specify a scan region by defining the area to scan using coordinates and dimensions in the OcrImageInput class, which can enhance performance by focusing on specific areas of the image.

How do you read a System.Drawing.Image using an OCR library?

To read a System.Drawing.Image using IronOCR, create an OcrImageInput object with the image and perform the standard OCR process using the Read method.

What is the use of an OCR engine in image text extraction?

IronTesseract is used to initialize the Tesseract OCR engine in IronOCR, allowing developers to perform OCR operations on images.

Can an OCR library be used on Linux and macOS?

Yes, IronOCR can be used on Linux and macOS by utilizing AnyBitmap from Iron Software, which supports these platforms.

What is the advantage of defining a specific scan region in an image?

Defining a specific scan region allows you to focus on a particular area of an image for OCR, which can improve performance and accuracy by narrowing the scan to relevant sections.

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.
Talk to an Expert Five Star Trust Score Rating

Ready to Get Started?

Nuget Passed