DEV Community

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

Posted on • Updated on

How to Scan a Barcode in C#

Last Updated: Aug 26,2024

In today's fast-paced digital world, barcodes are everywhere, from retail to healthcare, streamlining processes, and improving accuracy. Whether you’re managing inventory, processing transactions, or tracking products, building a barcode scanner in C# using IronBarcode can be a game-changer. This article will guide you through the process of creating a barcode scanner application with IronBarcode, from installation to advanced features and best practices, ensuring you harness the full potential of this powerful library.

Why Choose IronBarcode?

IronBarcode is more than just a barcode reader; it’s a comprehensive toolkit for barcode generation, scanning, and customization in C#. Whether you’re developing a simple inventory management system or a complex point-of-sale application, IronBarcode simplifies the process with robust features, cross-platform compatibility, and seamless integration into .NET projects.

Key Benefits:

  • Versatility: Supports a wide range of barcode formats, including QR codes, Code 128, Code 39, EAN, and more.
  • Multi-Source Input: Read barcodes from images, PDFs, and live video feeds.
  • Customization: Allows customization of barcodes in terms of color, size, and text annotations.
  • Speed and Efficiency: Optimized for quick barcode reading, even from low-quality images.

Getting Started with IronBarcode

Installation: The First Step

Installing IronBarcode is quick and straightforward. You can add it to your Visual Studio project via the NuGet Package Manager, which is the easiest and most efficient method.

To install IronBarcode using the NuGet Package Manager Console:



Install-Package Barcode


Enter fullscreen mode Exit fullscreen mode

Alternatively, you can install it via the NuGet Package Manager GUI:

  1. Right-click on your project in Visual Studio.
  2. Select "Manage NuGet Packages."
  3. Search for "IronBarcode."
  4. Click "Install."

Install IronBarcode - Nuget Package Manager Console - Microsoft Visual Studio

IronBarcode is compatible with various .NET frameworks, including .NET 5, .NET Core, and .NET Framework, making it suitable for both Windows and cross-platform applications on Linux and macOS.

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

In this example:

  • BarcodeReader.Read(@"barcode.png"): Reads the barcode(s) from the specified image file.
  • foreach (var barcode in barcodeResult): Iterates through the detected barcodes, allowing you to extract and print each barcode's value and format. This simple snippet demonstrates how IronBarcode can be used to quickly and accurately read barcodes from images, making it ideal for basic applications.

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

Here, the BarcodeReader.ReadPdf method is used to read barcodes from a PDF file. The code then iterates through the barcodes found in the PDF, displaying their values and formats.

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

Handling multiple barcodes in a Single Image
Often, a single image or document may contain multiple barcodes. IronBarcode excels in these scenarios by allowing you to extract all barcodes with ease.



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

In this example, we configure BarcodeReaderOptions to expect multiple one-dimensional barcodes in the image. The ExpectMultipleBarcodes property is set to true, ensuring that all barcodes are read. This is particularly useful in scenarios like warehouse management, where multiple barcodes might be present on a single label.

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#

Error Handling and Troubleshooting

When working with barcode scanning, you may encounter a few common issues that can be addressed with proper error handling and troubleshooting techniques:

  • Null Results: Sometimes, no barcode may be detected in the image. Always check for null results and handle them gracefully by notifying the user or attempting a different approach.
  • Low-Quality Images: Scanning barcodes from low-resolution or poorly lit images can result in errors. Preprocessing techniques like increasing contrast or resizing the image can improve scan accuracy.
  • Unexpected Barcode Types: If the barcode type scanned isn’t the one expected, consider logging this information for analysis or alerting the user to take corrective action.

By anticipating these issues and incorporating robust error handling in your application, you can create a more reliable and user-friendly barcode scanning solution.

Advanced Features of IronBarcode

IronBarcode offers a range of advanced features that can significantly enhance the functionality and performance of your barcode scanning application:

  • Live Video Feed Scanning: Integrate barcode scanning directly into live video feeds, ideal for real-time applications in retail or logistics.
  • Mobile Application Integration: Seamlessly add barcode scanning capabilities to mobile apps for on-the-go data capture.
  • Multi-Threaded Barcode Processing: Leverage multi-threading to improve performance, especially when dealing with large volumes of barcodes or high-resolution images.
  • Customizable Barcode Detection: Tailor the detection algorithms to prioritize certain barcode types or optimize for speed versus accuracy.
  • Machine Learning Enhancements: Utilize advanced machine learning techniques to improve barcode recognition in challenging conditions, such as damaged or partially obscured barcodes.

Practical Use Cases: Where to Apply IronBarcode

IronBarcode is versatile and can be applied in various industries and scenarios. Here are some common use cases:

  • Retail and Point of Sale: Automate product checkout and inventory management.
  • Healthcare: Track patient records and medication with barcoded labels.
  • Logistics: Manage shipments and track packages efficiently.
  • Document Management: Embed and read barcodes in scanned documents for easy indexing and retrieval. ##Conclusion:

IronBarcode is a powerful and versatile tool that enables developers to create efficient and reliable barcode scanning applications in C#. With its extensive support for various barcode formats, optimized performance for quick and accurate reading, and advanced features like multi-threaded processing and live video feed scanning, IronBarcode is well-suited for a wide range of industries and use cases. To fully explore its capabilities, download IronBarcode today and take advantage of the free trial provided by Iron Software. For further guidance, check out the documentation and connect with the developer community in our forum.

Top comments (0)