How to Use System Drawing Images for OCR Processing in C#

In this tutorial, we delve into using Iron OCR to extract text from images with System.Drawing in C#. Starting with setting up Iron OCR and the necessary namespaces, we guide you through initializing the Tesseract object to read a TIFF image file. By converting this file into a bitmap object, we create an OCR image input instance, allowing Iron OCR to process and extract text from the image.

The tutorial showcases different methods to handle images, including using Image.FromFile and AnyBitmap from IronSoftware.Drawing, demonstrating the flexibility of Iron OCR in processing images loaded through various techniques. Additionally, the video explains how to perform OCR on specific areas of an image by defining a scan region using coordinates and dimensions. By loading this region into an OCR image input method, we extract and print the text to the console.

The tutorial concludes with running the project to explore the console output, providing practical insights into extracting text from System.Drawing objects using Iron OCR in C#. Viewers are encouraged to subscribe for more informative videos and to try out the software using the available trial package linked in the description.

Here is a sample code demonstrating the above process:

using System;
using System.Drawing; // Provides access to basic graphics functionality
using IronOcr; // Namespace for Iron OCR functionality

class Program
{
    static void Main()
    {
        // Initialize the OCR engine with default language settings
        var ocr = new IronTesseract();

        // Load a TIFF image file into a System.Drawing.Bitmap object
        using Bitmap bitmap = (Bitmap)Image.FromFile("example.tiff");

        // Create an OCR input using the loaded bitmap
        using var ocrInput = new OcrInput(bitmap);

        // Process the OCR input to extract text
        var result = ocr.Read(ocrInput);

        // Output the recognized text to the console
        Console.WriteLine(result.Text);

        // Specify a region of the image for OCR scanning
        var specificArea = new Rectangle(50, 50, 200, 100);

        // Reload the image with the specified region
        ocrInput.AddImage(bitmap, specificArea);

        // Re-process the specified region for OCR
        var areaResult = ocr.Read(ocrInput);

        // Output the text found in the specific region
        Console.WriteLine(areaResult.Text);
    }
}
using System;
using System.Drawing; // Provides access to basic graphics functionality
using IronOcr; // Namespace for Iron OCR functionality

class Program
{
    static void Main()
    {
        // Initialize the OCR engine with default language settings
        var ocr = new IronTesseract();

        // Load a TIFF image file into a System.Drawing.Bitmap object
        using Bitmap bitmap = (Bitmap)Image.FromFile("example.tiff");

        // Create an OCR input using the loaded bitmap
        using var ocrInput = new OcrInput(bitmap);

        // Process the OCR input to extract text
        var result = ocr.Read(ocrInput);

        // Output the recognized text to the console
        Console.WriteLine(result.Text);

        // Specify a region of the image for OCR scanning
        var specificArea = new Rectangle(50, 50, 200, 100);

        // Reload the image with the specified region
        ocrInput.AddImage(bitmap, specificArea);

        // Re-process the specified region for OCR
        var areaResult = ocr.Read(ocrInput);

        // Output the text found in the specific region
        Console.WriteLine(areaResult.Text);
    }
}
Imports System
Imports System.Drawing ' Provides access to basic graphics functionality
Imports IronOcr ' Namespace for Iron OCR functionality

Friend Class Program
	Shared Sub Main()
		' Initialize the OCR engine with default language settings
		Dim ocr = New IronTesseract()

		' Load a TIFF image file into a System.Drawing.Bitmap object
		Using bitmap As Bitmap = CType(Image.FromFile("example.tiff"), Bitmap)
	
			' Create an OCR input using the loaded bitmap
			Dim ocrInput As New OcrInput(bitmap)
	
			' Process the OCR input to extract text
			Dim result = ocr.Read(ocrInput)
	
			' Output the recognized text to the console
			Console.WriteLine(result.Text)
	
			' Specify a region of the image for OCR scanning
			Dim specificArea = New Rectangle(50, 50, 200, 100)
	
			' Reload the image with the specified region
			ocrInput.AddImage(bitmap, specificArea)
	
			' Re-process the specified region for OCR
			Dim areaResult = ocr.Read(ocrInput)
	
			' Output the text found in the specific region
			Console.WriteLine(areaResult.Text)
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

Key Points in the Code:

  • Library Setup: The tutorial begins by including the necessary namespaces and initializing the IronTesseract object.
  • Loading Images: Images can be loaded using Image.FromFile method which facilitates obtaining a Bitmap object from an image file.
  • OCR Processing: An OcrInput object is created from the Bitmap, enabling the OCR engine to process the image data. The Read method executes the OCR process and extracts text from the bitmap.
  • Region-Based OCR: By defining a Rectangle, users can specify a particular area of the image to perform OCR, which can be useful for scanning sections of documents or images.

Further Reading: How to Read from System.Drawing Objects

Kannaopat Udonpant
Software Engineer
Before becoming a Software Engineer, Kannapat completed a Environmental Resources PhD from Hokkaido University in Japan. While pursuing his degree, Kannapat also became a member of the Vehicle Robotics Laboratory, which is part of the Department of Bioproduction Engineering. In 2022, he leveraged his C# skills to join Iron Software's engineering team, where he focuses on IronPDF. Kannapat values his job because he learns directly from the developer who writes most of the code used in IronPDF. In addition to peer learning, Kannapat enjoys the social aspect of working at Iron Software. When he's not writing code or documentation, Kannapat can usually be found gaming on his PS5 or rewatching The Last of Us.
< PREVIOUS
How To Read PDFS in OCR C#
NEXT >
How to Read PDFs with IronOCR