How to Set a Barcode Crop Region in C#

How to Define Barcode Crop Region for Faster Read

This article was translated from English: Does it need improvement?
Translated
View the article in English

One of the most important features in IronBarcode is the ability for users to specify Crop Regions. The aim of this feature is to enable IronBarcode to only read specific barcodes or areas specified by the Crop Region in the image by using the IronSoftware.Drawing.Rectangle object. Using this feature will not only reduce errors in reading but also improve the reading performance.

Quickstart: Define and Apply Crop Region to Read Barcodes Faster

Create a crop rectangle and feed it into IronBarcode in seconds—no extra setup, no friction. See how easy it is to limit scanning to a specific image area using BarcodeReaderOptions.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. Copy and run this code snippet.

    var results = IronBarCode.BarcodeReader.Read("image.png", new IronBarCode.BarcodeReaderOptions { CropArea = new IronSoftware.Drawing.Rectangle(x: 50, y: 100, width: 300, height: 150) });
  3. Deploy to test on your live environment

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

Find Crop Region coordinates and size in an image

There are many ways available for users to find the coordinates of a point in an image. One way is to load the image using the 'Paint' application on the computer. To get the first coordinate of the Crop Region, move the cursor to the preferred first spot, which will be the top-left corner of the Rectangle, and take the x, y coordinate given by the app at the bottom left of the screen. Then, locate the second point, which will be the bottom-right corner of the Rectangle. Refer to the image below for a clearer understanding.

CropRegion reference

The coordinate values can then be used as properties for the Rectangle object. The width of the object can be defined as x2 - x1, while height can be defined as y2 - y1.

:path=/static-assets/barcode/content-code-examples/how-to/set-crop-region-instantiate-CropRegion.cs
using IronBarCode;

int x1 = 62;
int y1 = 29;
int x2 = 345;
int y2 = 522;

IronSoftware.Drawing.Rectangle crop1 = new IronSoftware.Drawing.Rectangle(x: x1, y: y1, width: x2-x1, height: y2-y1);
Imports IronBarCode

Private x1 As Integer = 62
Private y1 As Integer = 29
Private x2 As Integer = 345
Private y2 As Integer = 522

Private crop1 As New IronSoftware.Drawing.Rectangle(x:= x1, y:= y1, width:= x2-x1, height:= y2-y1)
$vbLabelText   $csharpLabel

Apply CropRegion and Read Barcode

Once we have defined the CropRegions where we want IronBarcode to read, we can apply the object into BarcodeReaderOptions as one of the properties, besides other settings. This can then be used as a parameter in the BarcodeReader.Read() method. The code snippet below shows this process:

:path=/static-assets/barcode/content-code-examples/how-to/set-crop-region-apply-CropRegion.cs
using IronBarCode;
using System;

int x1 = 62;
int y1 = 29;
int x2 = 345;
int y2 = 522;

IronSoftware.Drawing.Rectangle crop1 = new IronSoftware.Drawing.Rectangle(x: x1, y: y1, width: x2 - x1, height: y2 - y1);

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    CropArea = crop1
};

var result = BarcodeReader.Read("sample.png", options);
foreach (var item in result)
{
    Console.WriteLine(item.Value);
}
Imports IronBarCode
Imports System

Private x1 As Integer = 62
Private y1 As Integer = 29
Private x2 As Integer = 345
Private y2 As Integer = 522

Private crop1 As New IronSoftware.Drawing.Rectangle(x:= x1, y:= y1, width:= x2 - x1, height:= y2 - y1)

Private options As New BarcodeReaderOptions() With {.CropArea = crop1}

Private result = BarcodeReader.Read("sample.png", options)
For Each item In result
	Console.WriteLine(item.Value)
Next item
$vbLabelText   $csharpLabel

From the code snippet above, we used the instantiated Rectangle in the BarcodeReaderOptions object as the CropArea property. We then use this BarcodeReaderOptions object as a parameter in the BarcodeReader.Read() method to apply the CropArea to the image and read the barcodes inside.

常见问题解答

定义裁剪区域如何提高 C# 中的条码读取效果?

使用 IronBarcode 库在 C# 中定义裁剪区域可以专注于图像的特定区域,通过减少不必要的数据处理提高条码读取的准确性和速度。

设置条码读取裁剪区域涉及哪些步骤?

要设置条码读取的裁剪区域,您需要使用图像编辑器确定区域的坐标,使用 IronSoftware.Drawing.Rectangle 对象定义区域,并通过 BarcodeReaderOptions 应用于 BarcodeReader.Read() 方法。

如何在图像中确定裁剪区域的坐标?

您可以使用图像编辑工具如 'Paint' 选择所需矩形的左上角和右下角,并记下 x, y 坐标来确定裁剪区域的坐标。

Rectangle 对象在定义裁剪区域中的作用是什么?

IronSoftware.Drawing.Rectangle 对象用于指定裁剪区域的坐标和尺寸,这有助于将条码读取过程集中在图像的特定区域。

在 C# 中设置裁剪区域后可以进行修改吗?

是的,您可以通过调整 Rectangle 对象的坐标和尺寸来修改裁剪区域,然后再将其应用于 BarcodeReaderOptions

使用 IronBarcode 进行条码读取时必须使用裁剪区域吗?

使用条码读取时不必使用裁剪区域,但这样做可以显著提高过程的效率和准确性,将读取集中在特定图像区域上。

BarcodeReaderOptions 对象如何利用裁剪区域?

BarcodeReaderOptions 对象通过将裁剪区域设置为 CropArea 属性来利用其功能,然后作为参数传递给 BarcodeReader.Read() 方法,以便将条码读取集中在定义的区域。

在条码检测中使用裁剪区域有什么优势?

在条码检测中使用裁剪区域的优势包括减少读取错误、提高速度以及专注于图像的特定区域,从而提升整体条码读取性能。

Hairil Hasyimi Bin Omar
软件工程师
如所有伟大的工程师一般,Hairil 是个热心的学习者。他正在提高对 C#、Python 和 Java 的知识,并利用这些知识为 Iron Software 团队成员增值。Hairil 从马来西亚的玛拉工业大学加入 Iron Software 团队,获得化学与工艺工程学士学位。
准备开始了吗?
Nuget 下载 1,935,276 | 版本: 2025.11 刚刚发布