How to Scan a Barcode in C#
Tags: webdev, barcode, csharp, dotnet
Published: 2024-07-05 06:52:29

###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](https://en.wikipedia.org/wiki/Barcode) 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](https://ironsoftware.com/csharp/barcode/) 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](https://ironsoftware.com/csharp/barcode/) is a comprehensive C# library designed for [barcode generation](https://ironsoftware.com/csharp/barcode/tutorials/csharp-barcode-image-generator/) and [reading](https://ironsoftware.com/csharp/barcode/tutorials/reading-barcodes/), offering a robust set of features and capabilities that make it a versatile tool for developers. It supports a wide range of [barcode formats](https://ironsoftware.com/csharp/barcode/troubleshooting/support-barcode-formats/), including QR codes, Code 128, Code 39, and EAN, and can handle [multiple barcodes](https://ironsoftware.com/csharp/barcode/how-to/read-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](https://ironsoftware.com/csharp/barcode/how-to/customize-barcode-style/), 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
```
Alternatively, you can install it via the NuGet Package Manager GUI by searching for "IronBarcode".

![Install IronBarcode - Nuget Package Manager Console - Microsoft Visual Studio](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/35xg0d2obh2exjk25212.png)
##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);
}
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8peiotcs0drsbynganl3.png)
The Output generated from the above code is as:

![Output - Reading Barcode in C#](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uvh54gunv4p20lx2v5il.png)
###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);
 }
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/inffclavtf1gwryciufj.png)
The output generated is as:

![Read Barcode from PDF File in C#](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0z0gy55w0i4ux3c1jnfk.png)
##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);
 }
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r326vw7m37712nb390b4.png)
The Output of the above code is:

![Barcode Scanners](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ou942v7v4j574ww3fhmv.png)
###Handling Multiple Barcodes in a Single Image

In cases where you have [multiple barcodes](https://ironsoftware.com/csharp/barcode/how-to/read-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("----------------------------------------");
}
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7qjwoykrjenxkrabld5e.png)
The Output of the code is as:

![Scan Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q10fbnw2dofas3cyr1rs.png)
##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");
```

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#](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3f9mlovuvagxmya1ybk7.png)
##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](https://ironsoftware.com/csharp/barcode/#trial-license) to test its functionalities before opting for a [commercial license](https://ironsoftware.com/csharp/barcode/licensing/). 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.