How to Read from System.Drawing Objects in C#
IronOCR enables reading text from System.Drawing objects like Bitmap and Image by wrapping them in OcrImageInput, providing seamless OCR functionality for .NET applications across Windows, macOS, and Linux platforms.
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.
Quickstart: Read Text from a System.Drawing.Bitmap
With a single statement, create an IronTesseract and feed it a System.Drawing.Bitmap wrapped by OcrImageInput to extract all text. This quickstart example demonstrates how IronOCR converts images into readable text with minimal setup.
```cs:title=Extract Text in One Line var result = new IronOcr.IronTesseract().Read(new IronOcr.OcrImageInput(new System.Drawing.Bitmap("image.png")));
<div class="hsg-featured-snippet">
<h3>Minimal Workflow (5 steps)</h3>
<ol>
<li><a class="js-modal-open" data-modal-id="trial-license-after-download" href="https://nuget.org/packages/IronOcr/">Download a C# library for reading System.Drawing Objects</a></li>
<li>Obtain System.Drawing objects such as `Bitmap` and `Image`</li>
<li>Construct the `OcrImageInput` class using the acquired data</li>
<li>Utilize `AnyBitmap` from Iron Software for Linux and macOS</li>
<li>Define the reading area by specifying the crop region</li>
</ol>
</div>
<br class="clear">
## How Do I Read from System.Drawing.Bitmap?
<!-- TODO: Add image here -->
<!--  -->
<!-- Description: Screenshot showing the step-by-step process -->
First, instantiate the **`IronTesseract`** class to perform OCR. Create a `System.Drawing.Bitmap` from one of the various methods. In the code example, a file path is used.
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.
```csharp
:path=/static-assets/ocr/content-code-examples/how-to/input-system-drawing-read-bitmap.csWhy does the using statement matter for OcrImageInput?
The using statement is crucial when working with OcrImageInput because it ensures proper resource management and memory cleanup. OcrImageInput implements IDisposable, which means it holds unmanaged resources that need to be released when you're done with the object. Without the using statement, these resources might not be released promptly, potentially leading to memory leaks or file locks. This is particularly important when processing multiple images in batch operations. For more details on proper resource management in IronOCR, see our API Reference documentation.
What are common Bitmap loading methods?
System.Drawing.Bitmap provides several loading methods beyond the file path constructor used in our example. You can create Bitmaps from streams (new Bitmap(stream)), from existing Images (new Bitmap(image)), or even create blank bitmaps with specific dimensions (new Bitmap(width, height)). When working with web applications, loading from streams is particularly useful for processing uploaded files. For embedded resources, you can use Assembly.GetManifestResourceStream(). IronOCR handles all these Bitmap sources seamlessly through the OcrImageInput constructor. Learn more about different input methods in our Images (jpg, png, gif, tiff, bmp) guide.
When should I dispose of the Bitmap object?
Bitmap disposal timing depends on your application's workflow. If you only need the Bitmap for OCR, dispose of it immediately after creating the OcrImageInput. However, if you need to perform multiple operations or display the image, keep it alive until all operations complete. Always use using statements or try-finally blocks to ensure disposal. Remember that OcrImageInput creates its own internal copy, so the original Bitmap can be disposed after OcrImageInput creation. For complex scenarios involving multiple image operations, consider our OCR Image Optimization Filters examples.
How Do I Read from System.Drawing.Image?
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.csusing 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)What’s the difference between Image and Bitmap for OCR?
While System.Drawing.Bitmap is a specific implementation for bitmap images, System.Drawing.Image is an abstract base class that can represent various image formats including JPEG, PNG, GIF, and TIFF. For OCR purposes, IronOCR treats both identically through OcrImageInput, but Image provides more flexibility when working with different formats. Bitmap offers pixel-level manipulation capabilities, while Image is better for general image handling. Both work equally well with IronOCR’s advanced Tesseract 5 engine. The choice depends on your broader application needs rather than OCR performance.
Why use Image.FromFile over other loading methods?
Image.FromFile is the simplest and most direct method for loading images from disk. It automatically detects the image format and handles the file reading process. Alternative methods like Image.FromStream are better for web applications or when working with memory streams. Image.FromFile locks the file until the Image is disposed, which can be a consideration in multi-threaded applications. For production scenarios requiring high performance or concurrent access, consider loading images into memory streams first. Our Multithreaded Tesseract OCR example demonstrates best practices for concurrent image processing.
How Do I Read from IronSoftware.Drawing.AnyBitmap?
Similarly, after creating or obtaining an AnyBitmap object, you can construct the OcrImageInput class. The constructor will handle 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.csusing 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)Why choose AnyBitmap over System.Drawing classes?
AnyBitmap offers superior cross-platform compatibility compared to System.Drawing classes. While System.Drawing.Common has limited support on non-Windows platforms in .NET 6+, AnyBitmap works seamlessly across Windows, Linux, and macOS. It provides a consistent API without platform-specific dependencies, making it ideal for cloud deployments and containerized applications. AnyBitmap also offers better memory management and performance optimizations specifically designed for image processing tasks. For detailed compatibility information, see our Compatibility documentation.
What platforms does AnyBitmap support?
AnyBitmap supports all major platforms where .NET runs: Windows (x86, x64, ARM), Linux (including Alpine Linux for Docker), and macOS (both Intel and Apple Silicon). This broad platform support makes it the recommended choice for modern .NET applications that need to run in diverse environments. It's particularly valuable for cloud deployments on AWS Lambda or Azure Functions. Learn more about platform-specific setup in our guides for Linux, macOS, and Docker environments.
How does AnyBitmap handle memory management?
AnyBitmap implements efficient memory management through automatic garbage collection integration and explicit disposal patterns. It uses memory pooling for frequently allocated buffers and implements copy-on-write semantics for better performance. Unlike System.Drawing.Bitmap which can hold file locks, AnyBitmap loads images fully into memory, preventing file access issues. It also provides better control over memory usage in high-throughput scenarios. For applications processing large volumes of images, AnyBitmap’s memory efficiency can significantly reduce overall memory footprint. See our System.Drawing.Common Alternatives guide for migration tips.
How Can I Specify a 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 scan region can significantly enhance performance. In the provided code example, only the chapter number and title are extracted.
:path=/static-assets/ocr/content-code-examples/how-to/input-images-read-specific-region.csusing 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)When should I use region scanning for better performance?
Region scanning dramatically improves performance when you only need text from specific areas of consistent document layouts. Common use cases include extracting headers, form fields, invoice totals, or ID card information. Performance gains are most significant with large images where the text occupies a small portion. For a 3000x4000 pixel invoice, scanning just the total amount region can be 10-20 times faster than full-page OCR. Region scanning also improves accuracy by eliminating potential noise from other areas. For more region-based examples, see our Content Areas & Crop Regions with PDFs guide.
How do I determine the correct coordinates for my region?
Determining coordinates requires understanding that Rectangle uses (X, Y, Width, Height) format, where (0,0) is the top-left corner. Start by opening your image in an image editor that displays cursor coordinates. Alternatively, use IronOCR’s debugging features to visualize detected text regions. For dynamic layouts, consider using IronOCR to perform a full scan first, then analyze the OcrResult to find text positions programmatically. Our Highlight Texts for Debugging example shows how to visualize OCR regions for accurate coordinate determination.
What happens if the region exceeds image boundaries?
When a specified region exceeds image boundaries, IronOCR automatically clips it to the valid image area. For example, if your image is 1000x1000 pixels and you specify a rectangle at (900, 900, 200, 200), IronOCR will only process the area from (900, 900) to (1000, 1000). This automatic clipping prevents errors but may result in incomplete text extraction if your coordinates are incorrect. Always validate your regions against actual image dimensions. For dynamic image sizes, calculate regions as percentages rather than fixed pixels. The OCR Region of an Image guide provides more examples of safe region handling.
OCR Result

Frequently Asked Questions
How do I extract text from a System.Drawing.Bitmap using OCR in C#?
IronOCR makes it simple to extract text from System.Drawing.Bitmap objects. First, instantiate the IronTesseract class, then wrap your Bitmap in an OcrImageInput object using a using statement, and finally call the Read method. The minimal code is: var result = new IronOcr.IronTesseract().Read(new IronOcr.OcrImageInput(bitmap));
Why is the using statement important when working with OcrImageInput?
The using statement is crucial because OcrImageInput implements IDisposable and holds unmanaged resources that need proper cleanup. Without it, you risk memory leaks or file locks, especially when processing multiple images. IronOCR's OcrImageInput requires proper disposal to ensure efficient resource management in your .NET applications.
Can I perform OCR on System.Drawing.Image objects?
Yes, IronOCR supports OCR on System.Drawing.Image objects since Image is the base class for Bitmap. Simply wrap your Image object in OcrImageInput the same way you would with a Bitmap, and IronOCR will extract the text seamlessly across Windows, macOS, and Linux platforms.
What is IronSoftware.Drawing.AnyBitmap and how does it relate to OCR?
IronSoftware.Drawing.AnyBitmap is a bitmap class from IronDrawing, an open-source library that helps replace System.Drawing.Common in .NET projects. It provides cross-platform compatibility for Windows, macOS, and Linux, making it ideal for use with IronOCR when you need consistent image handling across different operating systems.
Can I specify a specific area of an image for text extraction?
Yes, IronOCR allows you to define specific reading areas by specifying crop regions. This feature enables you to focus OCR processing on particular sections of your System.Drawing objects, improving performance and accuracy when you only need text from specific parts of an image.







