USING IRONBARCODE

C# Barcode Scanner Step-by-Step Tutorial

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 code for scanning a barcode image.

Scanning a barcode from an Image file

Add the following namespace

using IronBarCode;
using IronBarCode;
Imports IronBarCode
$vbLabelText   $csharpLabel

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

// Read the barcode from an image file
var myBarcode = BarcodeReader.Read(@"D:\Barcode Images\mybarcode.jpeg");

// Print the barcode data to the console
Console.WriteLine(myBarcode);
// Read the barcode from an image file
var myBarcode = BarcodeReader.Read(@"D:\Barcode Images\mybarcode.jpeg");

// Print the barcode data to the console
Console.WriteLine(myBarcode);
' Read the barcode from an image file
Dim myBarcode = BarcodeReader.Read("D:\Barcode Images\mybarcode.jpeg")

' Print the barcode data to the console
Console.WriteLine(myBarcode)
$vbLabelText   $csharpLabel

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:

// Read barcodes from a PDF file
var myBarcode = BarcodeReader.ReadPdf(@"D:\Barcode Images\invoice.pdf");

// Iterate through each barcode found and print its value
foreach(var barcodeData in myBarcode)
{
    Console.WriteLine(barcodeData.Value);
}
// Read barcodes from a PDF file
var myBarcode = BarcodeReader.ReadPdf(@"D:\Barcode Images\invoice.pdf");

// Iterate through each barcode found and print its value
foreach(var barcodeData in myBarcode)
{
    Console.WriteLine(barcodeData.Value);
}
' Read barcodes from a PDF file
Dim myBarcode = BarcodeReader.ReadPdf("D:\Barcode Images\invoice.pdf")

' Iterate through each barcode found and print its value
For Each barcodeData In myBarcode
	Console.WriteLine(barcodeData.Value)
Next barcodeData
$vbLabelText   $csharpLabel

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 is 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.

// Create a list of file paths containing barcode images
List<string> barcodeList = new List<string>
{
    @"D:\Barcode Images\barcode1.jpg",
    @"D:\Barcode Images\barcode2.jpg",
    @"D:\Barcode Images\barcode3.jpg"
};

// Read barcodes asynchronously from multiple files
var batchResults = BarcodeReader.ReadAsync(barcodeList);

// Work with the results
foreach (var result in batchResults)
{
    string barcodeValue = result.Text;
    Console.WriteLine(barcodeValue);
}
// Create a list of file paths containing barcode images
List<string> barcodeList = new List<string>
{
    @"D:\Barcode Images\barcode1.jpg",
    @"D:\Barcode Images\barcode2.jpg",
    @"D:\Barcode Images\barcode3.jpg"
};

// Read barcodes asynchronously from multiple files
var batchResults = BarcodeReader.ReadAsync(barcodeList);

// Work with the results
foreach (var result in batchResults)
{
    string barcodeValue = result.Text;
    Console.WriteLine(barcodeValue);
}
' Create a list of file paths containing barcode images
Dim barcodeList As New List(Of String) From {"D:\Barcode Images\barcode1.jpg", "D:\Barcode Images\barcode2.jpg", "D:\Barcode Images\barcode3.jpg"}

' Read barcodes asynchronously from multiple files
Dim batchResults = BarcodeReader.ReadAsync(barcodeList)

' Work with the results
For Each result In batchResults
	Dim barcodeValue As String = result.Text
	Console.WriteLine(barcodeValue)
Next result
$vbLabelText   $csharpLabel

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:

// Read the QR code from an image file
var qrCodeResult = BarcodeReader.Read(@"D:\Barcode Images\QRcode.jpeg");

// Iterate through each result and print its text
foreach (var result in qrCodeResult)
{
    Console.WriteLine(result.Text);
}
// Read the QR code from an image file
var qrCodeResult = BarcodeReader.Read(@"D:\Barcode Images\QRcode.jpeg");

// Iterate through each result and print its text
foreach (var result in qrCodeResult)
{
    Console.WriteLine(result.Text);
}
' Read the QR code from an image file
Dim qrCodeResult = BarcodeReader.Read("D:\Barcode Images\QRcode.jpeg")

' Iterate through each result and print its text
For Each result In qrCodeResult
	Console.WriteLine(result.Text)
Next result
$vbLabelText   $csharpLabel

The same Read function as discussed above is used to read the QR Code. This is the simplicity provided by the IronBarcode library, allowing the same function and code to 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, providing 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.

Frequently Asked Questions

What is IronBarcode?

IronBarcode is a .NET library that allows developers to read and write barcodes and QR codes within .NET applications and websites. It is highly efficient, easy to implement, and free for development.

How can I create a barcode scanner in C#?

To create a barcode scanner in C#, you can use the IronBarcode library. Start by creating a Visual Studio project, install the IronBarcode NuGet package, and use the BarcodeReader class to read barcodes from image or PDF files.

What barcode types are supported by IronBarcode?

IronBarcode supports various barcode types including 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 codes.

How do I install the IronBarcode NuGet package?

You can install the IronBarcode library using the Package Manager Console in Visual Studio. Use the command 'Install-Package Barcode' to install it from the NuGet BarCode package page.

Can IronBarcode read barcodes from PDF files?

Yes, IronBarcode can read barcodes from PDF files using the ReadPdf function, which scans the entire PDF document for barcode images and returns their data.

How can I scan multiple barcodes from different files simultaneously?

You can scan multiple barcodes from different files using the ReadAsync function, which allows asynchronous barcode reading from multiple image files, improving performance through multithreading.

Is it possible to scan QR codes using IronBarcode?

Yes, you can scan QR codes using the same Read function used for barcodes, making it simple to use IronBarcode for various image sources.

What are the benefits of using IronBarcode for barcode scanning?

IronBarcode offers ease of use, high efficiency, and adaptability. It supports a wide range of barcode types and provides simple APIs for both reading and writing barcodes and QR codes.

What other libraries are part of the Iron Software Suite?

The Iron Software Suite includes libraries such as IronPDF for PDF manipulation, IronXL for Excel files, IronOCR for text recognition from images, and IronWebScraper for web data extraction.

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he says it’s one of his favorite aspects of working with Iron Software. Jordi grew up in Miami, Florida and studied Computer Science and Statistics at University of Florida.
< PREVIOUS
How to Make A QR Code For A Link (C# Tutorial)
NEXT >
.NET QR Code Generator (Code Example Tutorial)