DEV Community

Cover image for How to Scan a Barcode in C#
Mehr Muhammad Hamza
Mehr Muhammad Hamza

Posted on

How to Scan a Barcode in C#

How to Scan a Barcode in C

  1. Create or Open Visual Studio Project
  2. Install IronBarcode Library
  3. Pass Input Image File Path to BarcodeReader.Read() Method
  4. Loop Through Extracted Results to Get Barcode Value

In today’s digital age, barcodes are ubiquitous, used across various industries for tracking products, managing inventory, and facilitating transactions. Developing a barcode scanner application in C# is a valuable skill, particularly with the robust capabilities provided by the IronBarcode library. This article will guide you through building a barcode scanner in C# using IronBarcode, covering installation, basic usage, advanced features, and best practices.

What is IronBarcode:

IronBarcode is a comprehensive C# library designed for barcode generation and reading, offering a robust set of features and capabilities that make it a versatile tool for developers. It supports a wide range of barcode formats, including QR codes, Code 128, Code 39, and EAN, and can handle multiple barcodes in a single image. IronBarcode allows for barcode reading from images, PDFs, and live video feeds, making it suitable for various applications such as inventory management, point-of-sale systems, and document processing. It also enables barcode customization, including color, size, and text annotations. With its ease of integration and extensive documentation, IronBarcode is ideal for both simple and complex barcode-related tasks in diverse industries.

Getting Started with IronBarcode

Installation

To begin, you need to install the IronBarcode library. This can be easily done via NuGet Package Manager in Visual Studio. Open the NuGet Package Manager Console and run the following command:

Install-Package Barcode
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can install it via the NuGet Package Manager GUI by searching for "IronBarcode".

Install IronBarcode - Nuget Package Manager Console - Microsoft Visual Studio

Basic Barcode Scanning

Reading Barcodes from Images

IronBarcode makes it straightforward to read barcodes from images. Here’s a simple example of how to read a barcode from an image file:

var barcodeResult = BarcodeReader.Read(@"barcode.png");

foreach (var barcode in barcodeResult)
{
    Console.WriteLine("Barcode Value: " + barcode.Text);
    Console.WriteLine("Barcode Format: " + barcode.BarcodeType);
}
Enter fullscreen mode Exit fullscreen mode

The above code uses IronBarcode to read barcodes from an image file named "barcode.png". The BarcodeReader.Read method reads the barcodes from the specified image. It then iterates through each detected barcode in the barcodeResult collection, printing its value and format to the console for each barcode found.

The Barcode Image used in this example is as:
Barcode Image
The Output generated from the above code is as:

Output - Reading Barcode in C#

Reading Barcode from PDF Files:

IronBarcode makes it straightforward to read barcodes from PDF file. Here’s a simple example of how to read a barcode from a pdf file:

 var barcodeResult = BarcodeReader.ReadPdf(@"barcode.pdf");

 foreach (var barcode in barcodeResult)
 {
     Console.WriteLine("Barcode Value: " + barcode.Text);
     Console.WriteLine("Barcode Format: " + barcode.BarcodeType);
 }
Enter fullscreen mode Exit fullscreen mode

This code snippet uses IronBarcode to read barcodes from a PDF file named "barcode.pdf" using the BarcodeReader.ReadPdf method. It iterates through each detected barcode in the barcodeResult collection, printing the barcode's value and format to the console for each barcode found.

The PDF file used in this example is as:

Barcode Scanner
The output generated is as:

Read Barcode from PDF File in C#

Advanced Features

Multiple Barcode Formats

IronBarcode supports a wide range of barcode formats, including QR code, Code 128, Code 39, EAN, and more. You can specify the type of barcode you want to read from barcode images:

BarcodeReaderOptions options = new BarcodeReaderOptions();
 options.ExpectBarcodeTypes = BarcodeEncoding.EAN13;
 options.Speed = ReadingSpeed.Balanced;


 var barcodeResult = BarcodeReader.Read(@"barcode_EAN13.png", options);

 foreach (var barcode in barcodeResult)
 {
     Console.WriteLine("Barcode Value: " + barcode.Text);
     Console.WriteLine("Barcode Format: " + barcode.BarcodeType);
 }
Enter fullscreen mode Exit fullscreen mode

This code snippet configures IronBarcode to read EAN-13 barcodes from an image file named "barcode_EAN13.png" using specific options. It first creates a BarcodeReaderOptions object, setting the expected barcode type to EAN-13 and the reading speed to balanced. You may set the reading speed to ReadingSpeed.Faster for faster barcode scanning. The BarcodeReader.Read method then reads the barcodes from the specified image using these options. It automatically managed the speed. Finally, it iterates through each detected barcode in the barcodeResult collection, printing the barcode's value and format to the console for each barcode found.

Barcode Image used in this example is as:

Barcode EAN-13
The Output of the above code is:

Barcode Scanners

Handling Multiple Barcodes in a Single Image

In cases where you have multiple barcodes in a single image, IronBarcode can read multiple barcodes:

BarcodeReaderOptions options = new BarcodeReaderOptions();
options.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional;
options.Speed = ReadingSpeed.Balanced;
options.ExpectMultipleBarcodes = true;


var barcodeResult = BarcodeReader.Read(@"multiple_barcode.jpg", options);

foreach (var barcode in barcodeResult)
{
    Console.WriteLine("----------------------------------------");
    Console.WriteLine("Barcode Value: " + barcode.Text);
    Console.WriteLine("Barcode Format: " + barcode.BarcodeType);
    Console.WriteLine("----------------------------------------");
}
Enter fullscreen mode Exit fullscreen mode

The above code snippet configures IronBarcode to read multiple one-dimensional barcodes from an image file named "multiple_barcode.png" using specific options. First, it creates a BarcodeReaderOptions object and sets the ExpectBarcodeTypes property to all one-dimensional barcode types. It also sets the reading speed to balanced and enables the ExpectMultipleBarcodes property to true, allowing the reading of multiple barcodes from a single image. The BarcodeReader.Read method then reads the barcodes from the specified image using these options. The code iterates through each detected barcode in the barcodeResult collection and prints each barcode's value and format to the console, surrounded by separators for clarity.

The Input Image is as:

Multiple Barcode
The Output of the code is as:

Scan Code

Generating Barcodes

IronBarcode is not only limited to reading barcodes; it also provides functionalities to generate them. Here’s an example of how to create a simple barcode:

 var barcode = BarcodeWriter.CreateBarcode("My_Barcode_Code128", BarcodeEncoding.Code128);
 barcode.SaveAsPng("barcode_128.png");
Enter fullscreen mode Exit fullscreen mode

This code snippet uses the IronBarcode library to generate a barcode. It creates a Code 128 barcode with the text "My_Barcode_Code128" using the BarcodeWriter.CreateBarcode method. After generating the barcode, it saves the barcode as a PNG image file named "barcode_128.png" using the SaveAsPng method.

The output Image is as:

Generate Barcode in C#

Conclusion:

In conclusion, building a barcode scanner application in C# using IronBarcode offers developers a robust solution with extensive capabilities for barcode generation and reading. IronBarcode, as a versatile .NET library, supports a wide array of barcode formats including QR codes, Code 128, and EAN, making it suitable for diverse applications across industries such as inventory management and retail. Its ability to handle multiple barcodes from various sources, including images, PDFs, and live video feeds, underscores its adaptability in different scenarios. Moreover, IronBarcode's intuitive integration into Windows Application and its automatic management of barcode generation and reading processes simplify development tasks, enhancing efficiency and productivity.

For developers interested in exploring IronBarcode, it offers a free trial to test its functionalities before opting for a commercial license. This trial period allows developers to evaluate its suitability for their projects, ensuring compatibility and performance across multiple images and multiple documents. Whether you are building a simple barcode scanner or integrating complex barcode functionalities into enterprise applications, IronBarcode provides the tools necessary to streamline development and deliver robust barcode solutions.

Top comments (0)