How to Define a Specific OCR Region of an Image in C#

To extract text from a specific region of an image in C#, use IronOCR's Rectangle object to define the exact area by specifying x/y coordinates, width, and height, then pass it to the LoadImage method for targeted OCR processing.

Quickstart: Extract Text from Specific Image Region

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronOCR with NuGet Package Manager

    PM > Install-Package IronOcr

  2. Copy and run this code snippet.

    using IronOcr;
    using IronSoftware.Drawing;
    
    // 1. Install IronOCR via NuGet: Install-Package IronOcr
    var ocr = new IronTesseract();
    using var input = new OcrInput();
    
    // 2. Create a Rectangle with coordinates
    var region = new Rectangle(x: 215, y: 1250, width: 1335, height: 280);
    
    // 3. Load image with region
    input.LoadImage("image.png", region);
    
    // 4. Extract text
    var result = ocr.Read(input);
    Console.WriteLine(result.Text);
  3. Deploy to test on your live environment

    Start using IronOCR in your project today with a free trial
    arrow pointer

Often, you only need to extract text from a small part of an image, such as a total amount on an invoice or a specific field from a form. Scanning the full document is inefficient and can introduce errors by capturing irrelevant text.

IronOCR allows you to improve precision, performance, and accuracy by specifying the exact rectangular region to scan. This guide provides a step-by-step walkthrough on how to define a specific OCR region, extract text from it, and visually verify that your coordinates are correct for your OCR tasks.

Get Started with IronOCR


How Do I Perform OCR on a Specific Region?

To define a specific OCR region, you create a Rectangle object from the IronSoftware.Drawing namespace. This object requires four values: the x-coordinate, the y-coordinate, the width, and the height, all in pixels. The (x, y) coordinates represent the top-left corner of your desired area.

When you load your image using LoadImage, you pass this Rectangle as the second parameter. IronOCR will then restrict its OCR process to only the pixels within that bounding box.

Regional OCR is particularly useful when working with structured documents like invoices, scanned forms, or identity documents where specific information always appears in predictable locations. By limiting the OCR to just the relevant areas, you can significantly improve processing speed and reduce false positives from unrelated text.

To find the coordinates for your Rectangle, you can use a simple image editor like MS Paint. Open your input image, hover your mouse over the top-left and bottom-right corners of the specified region, and note the (x, y) pixel coordinates. You can then calculate the rectangle's properties: (x1, y1, width, height), where width = x2-x1 and height = y2-y1.

What Image Should I Use for Testing?

We'll use a sample image with three paragraphs. Our goal is to extract only the second paragraph and ignore the rest of the text. This demonstrates a common scenario where you need to extract specific fields or sections from a larger document.

Terminal window displaying OCR results with 'Hello World' header and extracted text about a bookstore

How Do I Implement Regional OCR in Code?

The implementation involves creating an OcrInput object and loading your image with the specified rectangular region. This approach works with various image formats including JPG, PNG, GIF, TIFF, and BMP.

:path=/static-assets/ocr/content-code-examples/how-to/ocr-region-of-an-image.cs
using IronOcr;
using IronSoftware.Drawing;
using System;

var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();

// Define the specific region as a Rectangle
// (x, y) is the top-left corner.
var ContentArea = new Rectangle(x: 215, y: 1250, width: 1335, height: 280);

ocrInput.LoadImage("region-input.png", ContentArea);

var ocrResult = ocrTesseract.Read(ocrInput);

// Print the extracted text
Console.WriteLine(ocrResult.Text);
$vbLabelText   $csharpLabel

For more complex scenarios, you can define multiple regions within the same image. This is particularly useful when processing forms with multiple fields or tables in documents:

using IronOcr;
using IronSoftware.Drawing;

var ocr = new IronTesseract();
using var input = new OcrInput();

// Define multiple regions for different form fields
var nameField = new Rectangle(x: 100, y: 200, width: 300, height: 50);
var dateField = new Rectangle(x: 100, y: 300, width: 200, height: 50);
var amountField = new Rectangle(x: 400, y: 500, width: 150, height: 50);

// Load the same image multiple times with different regions
input.LoadImage("form.png", nameField);
var nameResult = ocr.Read(input);

input.Clear();
input.LoadImage("form.png", dateField);
var dateResult = ocr.Read(input);

input.Clear();
input.LoadImage("form.png", amountField);
var amountResult = ocr.Read(input);

// Process each field separately
Console.WriteLine($"Name: {nameResult.Text}");
Console.WriteLine($"Date: {dateResult.Text}");
Console.WriteLine($"Amount: {amountResult.Text}");
using IronOcr;
using IronSoftware.Drawing;

var ocr = new IronTesseract();
using var input = new OcrInput();

// Define multiple regions for different form fields
var nameField = new Rectangle(x: 100, y: 200, width: 300, height: 50);
var dateField = new Rectangle(x: 100, y: 300, width: 200, height: 50);
var amountField = new Rectangle(x: 400, y: 500, width: 150, height: 50);

// Load the same image multiple times with different regions
input.LoadImage("form.png", nameField);
var nameResult = ocr.Read(input);

input.Clear();
input.LoadImage("form.png", dateField);
var dateResult = ocr.Read(input);

input.Clear();
input.LoadImage("form.png", amountField);
var amountResult = ocr.Read(input);

// Process each field separately
Console.WriteLine($"Name: {nameResult.Text}");
Console.WriteLine($"Date: {dateResult.Text}");
Console.WriteLine($"Amount: {amountResult.Text}");
$vbLabelText   $csharpLabel

What Results Can I Expect?

As you can see from the console output, only the second paragraph is processed by the OCR. This targeted approach ensures that irrelevant text from other parts of the image doesn't interfere with your results.

OCR Output

The accuracy of regional OCR depends on several factors:

How Can I Verify My Coordinates Are Correct?

To ensure you've selected the correct coordinates for the input image, you can visualize the ContentArea you defined. A simple way to do this is to draw the rectangle on the input image and save it as a new file with StampCropRectangleAndSaveAs. This helps you debug and fine-tune the coordinates for optimal performance.

This visualization technique is especially helpful when working with complex layouts or when you need to highlight specific text areas for quality assurance purposes.

Here is the output image after drawing the specified bounding box on our example input image from above.

How Do I Visualize the Selected Region?

:path=/static-assets/ocr/content-code-examples/how-to/ocr-region-of-an-image-highlighted.cs
using IronOcr;
using IronSoftware.Drawing;

var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();

// Define the specific rectangular area to scan within the image.
// The coordinates are in pixels: (x, y) is the top-left corner of the rectangle.
var ContentArea = new Rectangle(x: 4, y: 59, width: 365, height: 26);

ocrInput.LoadImage("region-input.png", ContentArea);

var ocrResult = ocrTesseract.Read(ocrInput);

// Draws the rectangle from above in a blue bounding box on the image for visualization.
ocrInput.StampCropRectangleAndSaveAs(ContentArea, Color.Aqua, "region-input.png");
$vbLabelText   $csharpLabel

What Does the Visualization Look Like?

OCR Highlighted Output

The light blue rectangle confirms that we have correctly isolated the second paragraph for processing.

When Should I Use Regional OCR?

Regional OCR is ideal for several common scenarios:

  1. Form Processing: When extracting specific fields from standardized forms where data appears in consistent locations.
  2. Invoice Processing: To extract specific values like totals, dates, or invoice numbers without processing the entire document.
  3. License Plates: When using license plate recognition, focus only on the plate area.
  4. Identity Documents: Extract specific fields from passports or ID cards.
  5. Screenshots: When capturing text from specific UI elements in screenshots.

Best Practices for Regional OCR

To achieve optimal results with regional OCR:

  1. Add padding: Include a small buffer around your text to ensure no characters are cut off at the edges.
  2. Test with sample images: Always verify your coordinates with representative samples before processing large batches.
  3. Handle variations: Account for slight positioning variations in scanned documents by making your regions slightly larger than necessary.
  4. Optimize performance: For multithreaded processing, process different regions in parallel.
  5. Monitor confidence: Check the result confidence scores to ensure accuracy.

By focusing OCR processing on specific regions, you can significantly improve both the speed and accuracy of your text extraction tasks. This targeted approach is essential for building efficient document processing workflows in your .NET applications.

Frequently Asked Questions

How do I extract text from only a specific part of an image in C#?

With IronOCR, you can extract text from a specific region by creating a Rectangle object with x/y coordinates, width, and height values. Pass this Rectangle as the second parameter to the LoadImage method, and IronOCR will restrict its OCR processing to only that defined area.

What are the benefits of defining an OCR region instead of scanning the entire image?

By defining a specific OCR region with IronOCR, you can improve processing speed, increase accuracy, and reduce errors from capturing irrelevant text. This is especially useful for structured documents where information appears in predictable locations.

What parameters do I need to create a Rectangle for regional OCR?

To create a Rectangle for IronOCR's regional OCR, you need four values in pixels: x-coordinate, y-coordinate, width, and height. The (x, y) coordinates represent the top-left corner of your desired scanning area.

Which namespace contains the Rectangle object for OCR regions?

The Rectangle object used for defining OCR regions in IronOCR is located in the IronSoftware.Drawing namespace.

What types of documents benefit most from regional OCR processing?

IronOCR's regional OCR is particularly effective for structured documents like invoices, scanned forms, and identity documents where specific information consistently appears in the same locations.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More
Ready to Get Started?
Nuget Downloads 5,269,558 | Version: 2025.12 just released