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
Get started making PDFs with NuGet now:
Install IronOCR with NuGet Package Manager
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);Deploy to test on your live environment
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
OcrResultproperty 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.
:path=/static-assets/ocr/content-code-examples/how-to/ocr-region-of-an-image.csusing 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);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}");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?
:path=/static-assets/ocr/content-code-examples/how-to/ocr-region-of-an-image-highlighted.csusing 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");What 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 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.






