How to Read from System.Drawing Objects

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.

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

IronSoftware.Drawing.AnyBitmap is a bitmap class in IronDrawing, an open-source library originally developed by Iron Software. It helps C# software engineers replace System.Drawing.Common in .NET projects on Windows, macOS, and Linux platforms.

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;

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

// Read image file to Bitmap
Bitmap bitmap = new Bitmap("Potter.tiff");

// Import System.Drawing.Bitmap
using var imageInput = new OcrImageInput(bitmap);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
Imports IronOcr
Imports System.Drawing

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Read image file to Bitmap
Private bitmap As New Bitmap("Potter.tiff")

' Import System.Drawing.Bitmap
Private imageInput = New OcrImageInput(bitmap)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
$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 Image = System.Drawing.Image;

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

// Open image file as Image
Image image = Image.FromFile("Potter.tiff");

// Import System.Drawing.Image
using var imageInput = new OcrImageInput(image);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
Imports IronOcr
Imports Image = System.Drawing.Image

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Open image file as Image
Private image As Image = Image.FromFile("Potter.tiff")

' Import System.Drawing.Image
Private imageInput = New OcrImageInput(image)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
$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;

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

// Open image file as AnyBitmap
AnyBitmap anyBitmap = AnyBitmap.FromFile("Potter.tiff");

// Import IronSoftware.Drawing.AnyBitmap
using var imageInput = new OcrImageInput(anyBitmap);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
Imports IronOcr
Imports IronSoftware.Drawing

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Open image file as AnyBitmap
Private anyBitmap As AnyBitmap = AnyBitmap.FromFile("Potter.tiff")

' Import IronSoftware.Drawing.AnyBitmap
Private imageInput = New OcrImageInput(anyBitmap)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
$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 IronOcr;
using IronSoftware.Drawing;
using System;

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

// Specify crop region
Rectangle scanRegion = new Rectangle(800, 200, 900, 400);

// Add image
using var imageInput = new OcrImageInput("Potter.tiff", ContentArea: scanRegion);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Output the result to console
Console.WriteLine(ocrResult.Text);
Imports IronOcr
Imports IronSoftware.Drawing
Imports System

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Specify crop region
Private scanRegion As New Rectangle(800, 200, 900, 400)

' Add image
Private imageInput = New OcrImageInput("Potter.tiff", ContentArea:= scanRegion)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)

' Output the result to console
Console.WriteLine(ocrResult.Text)
$vbLabelText   $csharpLabel

OCR Result

Read specific region

Frequently Asked Questions

How can I extract text from a bitmap image in C#?

You can use IronOCR by first creating an instance of the IronTesseract class. Then, create a System.Drawing.Bitmap and pass it to an OcrImageInput object. Finally, use the Read method to extract text.

What steps are involved in reading from System.Drawing objects for OCR tasks?

To read from System.Drawing objects with IronOCR, download the library, obtain System.Drawing objects, construct the OcrImageInput class, and define a crop region if needed. Use AnyBitmap for cross-platform compatibility on Linux and macOS.

How do you perform OCR on a System.Drawing.Image?

To perform OCR on a System.Drawing.Image, create an OcrImageInput object with the image and execute the Read method in IronOCR.

What is the benefit of using AnyBitmap in cross-platform projects?

IronSoftware.Drawing.AnyBitmap allows developers to replace System.Drawing.Common with a cross-platform solution, enabling OCR functionality on Windows, macOS, and Linux.

How can scan regions improve OCR performance?

By defining specific scan regions in the OcrImageInput class, you can focus OCR efforts on relevant areas, which can significantly enhance performance and accuracy.

Is IronOCR compatible with Linux and macOS?

Yes, IronOCR is compatible with Linux and macOS when using IronSoftware.Drawing.AnyBitmap, offering a cross-platform solution for OCR tasks.

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

In IronOCR, specify a scan region by setting coordinates and dimensions in the OcrImageInput class, which enhances OCR performance by focusing on relevant image sections.

What classes are essential for handling images in .NET OCR projects?

In .NET OCR projects, classes like System.Drawing.Bitmap, System.Drawing.Image, and IronSoftware.Drawing.AnyBitmap are essential for handling images.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.
Reviewed by
Jeff Fritz
Jeffrey T. Fritz
Principal Program Manager - .NET Community Team
Jeff is also a Principal Program Manager for the .NET and Visual Studio teams. He is the executive producer of the .NET Conf virtual conference series and hosts 'Fritz and Friends' a live stream for developers that airs twice weekly where he talks tech and writes code together with viewers. Jeff writes workshops, presentations, and plans content for the largest Microsoft developer events including Microsoft Build, Microsoft Ignite, .NET Conf, and the Microsoft MVP Summit