Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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
Key Points in the Code:
IronTesseract
object.Image.FromFile
method which facilitates obtaining a Bitmap
object from an image file.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.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