USING IRONBARCODE

C# Barcode Scanner Step-by-Step Tutorial

Updated February 7, 2024
Share:

This tutorial will demonstrate how to create a barcode scanner with assistance from a third-party library. Multiple libraries are available for this purpose, but some are paid, some provide less functionality, and some are difficult to implement. It is a challenging task to find a very comprehensive and useful library that is free, efficient, and easy to implement.

For this reason, we will be using IronBarcode, as it is the best fit for developing a .NET barcode scanner. It also has the added benefit of being free for development, highly efficient, and easy to implement. IronBarcode allows developers to read and write barcodes and QR codes within .NET applications and websites. Reading or writing barcodes only requires a single line of code with this library.

The .NET barcode library reads and writes most barcode and QR code standards. The supported barcode types include code 39/93/128, UPC A/E, EAN 8/13, ITF, RSS 14 / Expanded, Databar, Codabar, Aztec, Data Matrix, MaxiCode, PDF417, MSI, Plessey, USPS, and QR. The barcode result data includes type, text, binary data, page, and image files.

The barcode writing API checks and verifies format, length, number, and checksum to automatically avoid encoding errors. The barcode writer allows for styling, resizing, margins, borders, recoloring, and adding text annotations. The barcode writer can write to image files including BMP, PNG, GIF, TIFF, and JPG. It can also write to PDF or HTML files.

Let's create our barcode scanner to understand it better.

Create a Visual Studio Project

First of all, create a Visual Studio project for developing a demonstration app. You can also open an existing one.

Follow the steps below for creating a new Console Application project to focus on core functionalities. The same code can be implemented in web API, MVC, Web Forms, or Windows Forms Applications.

  1. Open Visual Studio
  2. Click on Create New Project
  3. Select Template, click on the Next button
  4. Name the Project, click on the Next button
  5. Select Target Framework, click on the Next button
  6. Click on the Create button, click on the Next button

A new Project will be created as shown below:

C# Barcode Scanner Step-by-Step Tutorial, Figure 1: Console Application Console Application

The next step is to install the IronBarcode NuGet package to use its functionalities.

Install NuGet package of IronBarcode

You can install the library using the Package Manager Console, NuGet Package Manager Solutions, or directly install it from the NuGet BarCode package page.

Follow the steps below:

  1. Click on Tools > NuGet Package Manager > Package Manager Console.

    C# Barcode Scanner Step-by-Step Tutorial, Figure 2: Package Manager Console UI Package Manager Console UI

  2. Write the following command
Install-Package BarCode

The package will be installed.

Now let's write a code for scanning a barcode image.

Scanning a barcode from an Image file

Add the following namespace

using IronBarCode;
using IronBarCode;
Imports IronBarCode
VB   C#

Next write the following code inside the main function to read barcode data from an image.

var myBarcode = BarcodeReader.Read(@"D:\Barcode Images\mybarcode.jpeg");
Console.WriteLine(myBarcode);
var myBarcode = BarcodeReader.Read(@"D:\Barcode Images\mybarcode.jpeg");
Console.WriteLine(myBarcode);
Dim myBarcode = BarcodeReader.Read("D:\Barcode Images\mybarcode.jpeg")
Console.WriteLine(myBarcode)
VB   C#

The BarcodeReader class provides a Read function that takes a file path as an argument. This function reads the image and returns barcode data. This method reads barcodes from a BMP, PNG, GIF, TIFF, or JPG with granular settings for developer control to balance performance against accuracy for their given use case.

The path contains the following barcode image which the application will scan.

Barcode Image

C# Barcode Scanner Step-by-Step Tutorial, Figure 3: The barcode image used in this Console Application The barcode image used in this Console Application

Let's read this barcode image to see if this program produces the correct result.

Output

C# Barcode Scanner Step-by-Step Tutorial, Figure 4: The Console output on running application The Console output on running application

It can be seen that the program has generated an accurate output.

Scanning a barcode from a PDF

There are many circumstances where there is a need to scan a barcode from a PDF invoice. In this example, we will scan the barcode from the following invoice.

PDF document

C# Barcode Scanner Step-by-Step Tutorial, Figure 5: An invoice in PDF format An invoice in PDF format

Consider the following code snippet to scan the barcode from a PDF document:

var myBarcode = BarcodeReader.ReadPdf(@"D:\Barcode Images\invoice.pdf");
foreach(var barcodeData in myBarcode)
{
    Console.WriteLine(barcodeData.Value);
}
var myBarcode = BarcodeReader.ReadPdf(@"D:\Barcode Images\invoice.pdf");
foreach(var barcodeData in myBarcode)
{
    Console.WriteLine(barcodeData.Value);
}
Dim myBarcode = BarcodeReader.ReadPdf("D:\Barcode Images\invoice.pdf")
For Each barcodeData In myBarcode
	Console.WriteLine(barcodeData.Value)
Next barcodeData
VB   C#

The BarcodeReader class provides the ReadPdf function which takes a file path as an argument. This function looks for a barcode image in a PDF file, scans the whole barcode, and returns its data in the form of an array. This function reads barcodes from every image embedded in the document.

a foreach loop used to print the data of the barcode in the console.

Output

The invoice number is printed on the console.

C# Barcode Scanner Step-by-Step Tutorial, Figure 6: The Console output displays the invoice number The Console output displays the invoice number

Scanning barcodes from multiple files

This example will demonstrate how to scan multiple barcodes from image files simultaneously.

Barcode images

C# Barcode Scanner Step-by-Step Tutorial, Figure 7: Barcode images used in the sample below Barcode images used in the sample below

Consider the following code snippets which read multiple barcodes and scan their result using multithreading.

List<string> barcodeList = new List<string>();
barcodeList.Add(@"D:\Barcode Images\barcode1.jpg");
barcodeList.Add(@"D:\Barcode Images\barcode2.jpg");
barcodeList.Add(@"D:\Barcode Images\barcode3.jpg");
var batchResults =  BarcodeReader.ReadAsync(barcodeList);
// Work with the results
foreach (var result in batchResults)
{
    string barcodeValue = result.Text;
}
List<string> barcodeList = new List<string>();
barcodeList.Add(@"D:\Barcode Images\barcode1.jpg");
barcodeList.Add(@"D:\Barcode Images\barcode2.jpg");
barcodeList.Add(@"D:\Barcode Images\barcode3.jpg");
var batchResults =  BarcodeReader.ReadAsync(barcodeList);
// Work with the results
foreach (var result in batchResults)
{
    string barcodeValue = result.Text;
}
Dim barcodeList As New List(Of String)()
barcodeList.Add("D:\Barcode Images\barcode1.jpg")
barcodeList.Add("D:\Barcode Images\barcode2.jpg")
barcodeList.Add("D:\Barcode Images\barcode3.jpg")
Dim batchResults = BarcodeReader.ReadAsync(barcodeList)
' Work with the results
For Each result In batchResults
	Dim barcodeValue As String = result.Text
Next result
VB   C#

Firstly, a list is created to hold the file path of all the barcode images. Next, the ReadAsync function is called, which takes a List<string> type as an argument and returns the data. This method reads barcodes from multiple images in parallel. Multiple threads will start and be automatically managed to improve performance for batch barcode reading tasks.

Scan QR Code

The usage of QR codes is rapidly increasing. Therefore, this section will show how to scan QR codes using C#.

QR code

C# Barcode Scanner Step-by-Step Tutorial, Figure 8: The QR code used in this demonstration The QR code used in this demonstration

Consider the following code sample:

var qrCodeResult = BarcodeReader.Read(@"D:\Barcode Images\QRcode.jpeg");
foreach (var result in qrCodeResult)
{
    Console.WriteLine(result.Text);
}
var qrCodeResult = BarcodeReader.Read(@"D:\Barcode Images\QRcode.jpeg");
foreach (var result in qrCodeResult)
{
    Console.WriteLine(result.Text);
}
Dim qrCodeResult = BarcodeReader.Read("D:\Barcode Images\QRcode.jpeg")
For Each result In qrCodeResult
	Console.WriteLine(result.Text)
Next result
VB   C#

The same Read function as discussed above is used to read the QR Code. This is the simplicity provided by the IronBarcode library. Such that the same function and code can be used for different image sources.

Output

C# Barcode Scanner Step-by-Step Tutorial, Figure 9: The Console out of QR code reader The Console out of QR code reader

Summary

This tutorial demonstrated a very easy way to scan barcodes from a single image, scan barcodes from a PDF document, and scan barcodes from multiple images and multiple documents in parallel. We have used the same function for different functionalities. It provides performance and usability both at the same time. IronBarcode provides the functionality of generating barcodes and QR codes with different configurations. There are so many features that cannot be discussed in the same article. Please click on the documentation page for further exploration of IronBarcode.

IronBarcode is part of the Iron Software Suite. This suite contains other very useful libraries, such as IronPDF for reading and writing PDF files, IronXL for manipulating Excel files, IronOCR for reading text from images, and IronWebScraper for extracting data from different websites. You can purchase the complete Iron Suite for the price of two individual libraries.

< PREVIOUS
How to Make A QR Code For A Link (C# Tutorial)
NEXT >
.NET QR Code Generator (Code Example Tutorial)

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 1,246,703 View Licenses >