DEV Community

Cover image for How to Read A Barcode in C#
Mehr Muhammad Hamza
Mehr Muhammad Hamza

Posted on

How to Read A Barcode in C#

How to Read Barcodes in C

  1. Download Barcode Reader for C# Project
  2. Create a Visual Studio Project:
  3. Install Barcode Library
  4. Write a Code to Read a Barcode
  5. Write Code to read multiple barcode images

This tutorial provides a straightforward approach to read barcodes using C# within the .NET Framework. It equips developers with essential skills to efficiently extract barcode information for various applications.

C# Barcode Reader Library:

IronBarcode is a powerful and easy-to-use library for barcode generation and reading in .NET applications. Developed by IronSoftware, it allows developers to integrate barcode functionalities into their C# and VB.NET projects with minimal effort. The library supports a wide range of barcode types and formats, making it a versatile tool for various barcode-related tasks in software development.

Key Features of IronBarcode

Barcode Reading:

  1. The library can read and decode barcodes from images, PDFs, and even directly from web URLs.
  2. It supports reading multiple barcodes from a single image.
  3. Advanced reading options allow for fine-tuning, such as specifying barcode types, adjusting image contrast, and selecting specific regions of an image to scan.

Barcode Generation:

  1. IronBarcode can generate a variety of barcodes. Supported barcode formats are QR codes, Code 128, Code 39, UPC, EAN, and more.
  2. It supports customization of barcode appearance, such as colors, and sizes, and adding logos or images within QR codes. It provides methods to style barcodes.
  3. Barcodes can be saved in various formats like PNG, JPEG, GIF, BMP, and more.

Supported Formats:

IronBarcode supports a wide range of barcode formats including linear (1D) barcodes and matrix (2D) barcodes.

Ease of Use:

  1. The API is designed to be intuitive and straightforward, making it accessible even to developers with limited experience in barcode technologies.
  2. Comprehensive documentation and examples

Step-By-Step Tutorial:

Let's begin the step-by-step tutorial to generate barcodes.

Step # 1: Create a Visual Studio Project:

Here are the steps to create a Visual Studio Console application in .NET 8:

  1. Open Visual Studio.
  2. Click on "Create a new project."
  3. Select "Console App" and click "Next."
  4. Name your project and select the location, then click "Next."
  5. Choose ".NET 8" as the framework and click "Create."
  6. Wait for the project to be set up and initialized. Visual Studio Project

Step # 2: Install Barcode Library:

Before getting started, you need to install the Barcode Library in your project. You have three options for this, and you can choose any one that suits you best.

Package Manager Console:

  1. Go to "Tools" > "NuGet Package Manager" > "Package Manager Console."
  2. In the console, type: Install-Package Barcode
  3. Press Enter and wait for the installation to complete.

Package Manager Console

Nuget Package Manager Solution:

  1. Right-click on your solution in the Solution Explorer.
  2. Select "Manage NuGet Packages for Solution."
  3. In the "Browse" tab, search for "IronBarcode."

Image description

  1. Select "IronBarcode" from the list and click "Install."
  2. Choose the projects within the solution where you want to install the package and click "OK."

Download From the Link:

If you like doing things manually, just download the IronBarCode.Dll from the link provided and then add it to your project as a reference.

Once IronBarcode is installed, include the following namespace in your C# class:

using Ironbarcode;
Enter fullscreen mode Exit fullscreen mode

Step # 3: Write Code to Read Barcode Image:

The following code snippet demonstrates how to read a barcode from an image file and output its text content using IronBarcode in C#.

static void Main(string[] args)
{
    var resultFromFile = BarcodeReader.Read(@"barcode.png"); 
    foreach (var item in resultFromFile)
    {
        Console.WriteLine(item.Text);
    }
}
Enter fullscreen mode Exit fullscreen mode

The above code utilizes IronBarcode to extract data from a barcode image file ("barcode.png"). It iterates through the obtained results and prints out the textual content of each identified barcode to the console.

Read Barcode from Image

Read From Multiple Barcodes:

Efficiently scanning multiple barcodes within a document is made possible by the IronBarcode library in C#. This code showcases how to utilize the library's capabilities, such as concurrent processing and advanced barcode recognition options. By fine-tuning parameters like multithreading and expected barcode types, developers can optimize the scanning process for swift and accurate extraction of multiple barcode values from a single document. The following barcode images will be used as input.

C# Barcode Reader

Check out the code below to see how IronBarcode simplifies and improves barcode reading in C#.

var myOptionsExample = new BarcodeReaderOptions
{
    // Choose a speed from: Faster, Balanced, Detailed, ExtremeDetail
    // There is a tradeoff in performance as more Detail is set
    Speed = ReadingSpeed.Balanced,

    // Reader will stop scanning once a barcode is found, unless set to true
    ExpectMultipleBarcodes = true,

    // By default, all barcode formats are scanned for.
    // Specifying one or more, performance will increase.
    ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,

    // Utilizes multiple threads to reads barcodes from multiple images in parallel.
    Multithreaded = true,

    // Maximum threads for parallel. Default is 4
    MaxParallelThreads = 2,

};
var resultFromPdf = BarcodeReader.Read("barcode.pdf", myOptionsExample);
for (int i = 0; i < resultFromPdf.Count; i++)
{
// Print Barcode Data
    Console.WriteLine($"Value from Barcode # {i} : {resultFromPdf[i]}");
}
Enter fullscreen mode Exit fullscreen mode

The provided code snippet demonstrates how to utilize the IronBarcode library in C# for efficient and flexible barcode reading. By configuring options such as reading speed, expected barcode types, and multithreading, developers can optimize the scanning process to extract multiple barcode values from a PDF document. The code initializes a set of options defining the reading parameters, including the desired reading speed, the expectation of multiple barcodes, and the utilization of multithreading for parallel processing. Subsequently, it reads barcodes from the specified PDF file using the defined options and prints out the extracted barcode values along with their corresponding indices. This implementation showcases how IronBarcode simplifies barcode reading tasks while providing customization options for enhanced performance and versatility.

Utilizing IronBarcode in C# facilitates faster barcode scanning by efficiently processing binary data through the integration of a barcode scanner. This enables seamless extraction of barcode information from various file formats across different operating systems, enhancing overall efficiency in barcode recognition tasks. By harnessing the library's capabilities, developers can streamline their applications' barcode reading functionality, achieving swift and accurate results across diverse environments.

Conclusion:

In conclusion, IronBarcode stands as a robust solution for barcode reading in C#, offering developers a straightforward and efficient way to integrate barcode functionality into their applications. Its extensive feature set, including advanced reading options, barcode generation, and support for multiple barcode formats, streamlines the development process. With a free trial available, developers can explore IronBarcode's capabilities risk-free, and upon satisfaction, seamlessly transition to purchase for continued access to its full functionality and support. IronBarcode empowers developers to enhance their applications with accurate and fast barcode scanning capabilities, making it a valuable asset in the toolkit of any C# developer.

Following the conclusion, it's worth highlighting IronQR, a specialized library dedicated to QR code generation and recognition. IronQR employs advanced machine-learning techniques to enhance accuracy and speed, making it a valuable addition to any developer's toolkit. When combined with IronBarcode, these two libraries offer a comprehensive solution for incorporating both barcode and QR code functionalities seamlessly into .NET applications.

Top comments (0)