Cómo Configurar una Región de Recorte de Código de Barras en 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.

Preguntas Frecuentes

¿Cómo mejora definir una región de recorte la lectura de códigos de barras en C#?

Definir una región de recorte en C# utilizando la biblioteca IronBarcode te permite enfocarte en áreas específicas de una imagen, mejorando la precisión y la velocidad de la lectura de códigos de barras al reducir el procesamiento de datos innecesarios.

¿Cuáles son los pasos para configurar una región de recorte para leer códigos de barras?

Para configurar una región de recorte para la lectura de códigos de barras, necesitas determinar las coordenadas de la región usando un editor de imágenes, definir la región usando el objeto IronSoftware.Drawing.Rectangle y aplicarlo a través del BarcodeReaderOptions en el método BarcodeReader.Read().

¿Cómo puedes determinar las coordenadas para una región de recorte en una imagen?

Puedes determinar las coordenadas para una región de recorte utilizando una herramienta de edición de imágenes como 'Paint' para seleccionar las esquinas superiores izquierdas e inferiores derechas del rectángulo deseado y anotar las coordenadas x, y.

¿Cuál es el papel del objeto Rectangle en la definición de una región de recorte?

El objeto IronSoftware.Drawing.Rectangle se utiliza para especificar las coordenadas y dimensiones de la región de recorte, lo que ayuda a enfocar el proceso de lectura de códigos de barras en áreas específicas de una imagen.

¿Puedes modificar una región de recorte después de haberla configurado en C#?

Sí, puedes modificar una región de recorte ajustando las coordenadas y dimensiones del objeto Rectangle antes de aplicarlo a BarcodeReaderOptions.

¿Es obligatorio usar una región de recorte para la lectura de códigos de barras con IronBarcode?

No es obligatorio usar una región de recorte para la lectura de códigos de barras, pero hacerlo puede mejorar significativamente la eficiencia y precisión del proceso al concentrar la lectura en áreas específicas de la imagen.

¿Cómo utiliza el objeto BarcodeReaderOptions la región de recorte?

El objeto BarcodeReaderOptions utiliza la región de recorte estableciéndola como propiedad CropArea, que luego se pasa como parámetro al método BarcodeReader.Read() para enfocar la lectura de códigos de barras en la región definida.

¿Cuáles son las ventajas de usar una región de recorte en la detección de códigos de barras?

El uso de una región de recorte en la detección de códigos de barras ofrece ventajas como la reducción de errores de lectura, la mejora de la velocidad y el enfoque en áreas específicas de una imagen, lo que mejora el rendimiento general de la lectura de códigos de barras.

Hairil Hasyimi Bin Omar
Ingeniero de Software
Como todos los grandes ingenieros, Hairil es un ávido aprendiz. Está refinando su conocimiento de C#, Python y Java, usando ese conocimiento para agregar valor a los miembros del equipo en Iron Software. Hairil se unió al equipo de Iron Software desde la Universiti Teknologi MARA en Malasia, donde se ...
Leer más
¿Listo para empezar?
Nuget Descargas 1,935,276 | Versión: 2025.11 recién lanzado