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.
-
1Install IronOCR with NuGet Package Manager
-
2Copy 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);C# -
3Deploy to test on your live environment
Start using IronOCR in your project today with a free trial
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 to Define a Specific OCR Region of an Image
- Download the C# library for defining the OCR region
- Instantiate the OCR engine
- Specify the OCR region with a rectangle
- Load the image with
LoadImagealong with the defined rectangle - Access the OcrResult property to view and manipulate the extracted data
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.

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.
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);Imports IronOcr
Imports IronSoftware.Drawing
Imports System
Dim ocrTesseract As New IronTesseract()
Using ocrInput As New OcrInput()
' Define the specific region as a Rectangle
' (x, y) is the top-left corner.
Dim ContentArea As New Rectangle(x:=215, y:=1250, width:=1335, height:=280)
ocrInput.LoadImage("region-input.png", ContentArea)
Dim ocrResult = ocrTesseract.Read(ocrInput)
' Print the extracted text
Console.WriteLine(ocrResult.Text)
End UsingFor 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();
// 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 with a separate OcrInput per region
OcrResult nameResult;
using (var input = new OcrInput())
{
input.LoadImage("form.png", nameField);
nameResult = ocr.Read(input);
}
OcrResult dateResult;
using (var input = new OcrInput())
{
input.LoadImage("form.png", dateField);
dateResult = ocr.Read(input);
}
OcrResult amountResult;
using (var input = new OcrInput())
{
input.LoadImage("form.png", amountField);
amountResult = ocr.Read(input);
}
// Process each field separately
Console.WriteLine($"Name: {nameResult.Text}");
Console.WriteLine($"Date: {dateResult.Text}");
Console.WriteLine($"Amount: {amountResult.Text}");
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.

The accuracy of regional OCR depends on several factors:
- Image quality: Higher resolution images generally yield better results. Consider using DPI settings to optimize your images.
- Text orientation: Ensure text is properly aligned. Use page rotation detection if needed.
- Contrast and clarity: Apply image correction filters to enhance text readability.
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?
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");Imports IronOcr
Imports IronSoftware.Drawing
Dim ocrTesseract = New IronTesseract()
Using ocrInput As 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.
Dim ContentArea As New Rectangle(x:=4, y:=59, width:=365, height:=26)
ocrInput.LoadImage("region-input.png", ContentArea)
Dim 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")
End UsingWhat Does the Visualization Look Like?

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:
- Form Processing: When extracting specific fields from standardized forms where data appears in consistent locations.
- Invoice Processing: To extract specific values like totals, dates, or invoice numbers without processing the entire document.
- License Plates: When using license plate recognition, focus only on the plate area.
- Identity Documents: Extract specific fields from passports or ID cards.
- Screenshots: When capturing text from specific UI elements in screenshots.
Best Practices for Regional OCR
To achieve optimal results with regional OCR:
- Add padding: Include a small buffer around your text to ensure no characters are cut off at the edges.
- Test with sample images: Always verify your coordinates with representative samples before processing large batches.
- Handle variations: Account for slight positioning variations in scanned documents by making your regions slightly larger than necessary.
- Optimize performance: For multithreaded processing, process different regions in parallel.
- 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 can I define a specific OCR region in an image using C#?
To define a specific OCR region in C#, use IronOCR's `Rectangle` object by specifying x/y coordinates, width, and height. Then pass this rectangle to the `LoadImage` method for targeted OCR processing.
Why is defining a specific OCR region beneficial?
Specifying a particular OCR region helps to improve precision and performance by focusing on the relevant part of an image, preventing irrelevant text extraction and reducing errors.
What libraries do I need to perform OCR on a specific image region in C#?
You will need IronOCR and IronSoftware.Drawing libraries. IronOCR is installed via NuGet and allows you to perform OCR on predefined image regions.
How do I specify the rectangle coordinates for OCR in a C# application?
To specify rectangle coordinates, use an image editor to determine the x/y coordinates of the top-left corner and set the width and height of the desired region in pixels.
Can I extract text from multiple regions within an image using IronOCR?
Yes, IronOCR allows you to define and process multiple rectangular regions within the same image, making it ideal for handling forms with multiple fields.
Is it possible to visualize the OCR region on the input image?
Yes, you can visualize the OCR region by using the `StampCropRectangleAndSaveAs` method to draw the specified rectangle on the input image, aiding in coordinate verification.
For what applications is regional OCR particularly useful?
Regional OCR is useful for form processing, invoice scanning, license plate recognition, identity document analysis, and extracting text from UI elements in screenshots.
What are some best practices for using regional OCR in document processing?
Best practices include adding padding around text, testing with sample images, handling slight document variations, optimizing performance with multithreading, and monitoring result confidence scores.
How can I improve the accuracy of IronOCR's regional scanning?
Improve accuracy by using high-resolution images, ensuring proper text orientation, applying contrast and clarity filters, and verifying the targeted region visually.
Can IronOCR be used with different image formats?
Yes, IronOCR supports various image formats including JPG, PNG, GIF, TIFF, and BMP, making it versatile for different scanning and processing needs.

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.