Read Barcodes in C#

by Frank Walker

IronBarcode provides a versatile, advanced, and efficient library for reading barcodes in .NET.

Installation

Start using IronBarcode in your project today with a free trial.

First Step:
green arrow pointer

IronBarcode provides a versatile, advanced, and efficient library for reading barcodes in .NET.

The first step will be to install Iron Barcode, and this is most easily achieved using our NuGet package, although you may also choose to manually install the DLL to your project or to your global assembly cache. IronBarcode works well to produce a C# Barcode Scanner application.

Install-Package BarCode

Read Your First Barcode

Reading a Barcode or QR Code in .NET is incredibly easy using the IronBarcode class library with .NET Barcode Reader. In our first example, we can see how to read this Barcode with one line of code.

Code128 Barcode Image to be Scanned with C#

We can extract its value, its image, its encoding type, its binary data (if any), and we can then output that to the console.

:path=/static-assets/barcode/content-code-examples/tutorials/reading-barcodes-1.cs
using IronBarCode;
using System;

// Read barcode
BarcodeResults results = BarcodeReader.Read("GetStarted.png");

// Log the result to Console Window
foreach (BarcodeResult result in results)
{
    if (result != null)
    {
        Console.WriteLine("GetStarted was a success. Read Value: " + result.Text);
    }
}
Imports IronBarCode
Imports System

' Read barcode
Private results As BarcodeResults = BarcodeReader.Read("GetStarted.png")

' Log the result to Console Window
For Each result As BarcodeResult In results
	If result IsNot Nothing Then
		Console.WriteLine("GetStarted was a success. Read Value: " & result.Text)
	End If
Next result
VB   C#

Try Harder and Be Specific

In this next example, we will add a barcode scanning option to read a challenging image. The ExtremeDetail value of the Speed enum allows deeper scanning for a barcode that might be obscured, corrupted, or at a skewed angle.

:path=/static-assets/barcode/content-code-examples/tutorials/reading-barcodes-2.cs
using IronBarCode;

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

    // By default, all barcode formats are scanned for.
    // Specifying one or more, performance will increase.
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
};

// Read barcode
BarcodeResults results = BarcodeReader.Read("TryHarderQR.png", options);
Imports IronBarCode

Private options As New BarcodeReaderOptions() With {
	.Speed = ReadingSpeed.ExtremeDetail,
	.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128
}

' Read barcode
Private results As BarcodeResults = BarcodeReader.Read("TryHarderQR.png", options)
VB   C#

Which will read this skewed QR Code:

Scanning a QR code rotated through 45 degrees with C#

In our example, you can see that we can specify the barcode encoding(s) we are looking for or even multiple formats. Doing so greatly improves barcode reading performance and accuracy. The | pipe character, or 'Bitwise OR,' is used to specify multiple formats simultaneously.

The same can be achieved, but with a higher degree of specificity, if we move forward and use the ImageFilters and AutoRotate properties.

:path=/static-assets/barcode/content-code-examples/tutorials/reading-barcodes-3.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection() {
        new AdaptiveThresholdFilter(),
    },

    // Uses machine learning to auto rotate the barcode
    AutoRotate = true,
};

// Read barcode
BarcodeResults results = BarcodeReader.Read("TryHarderQR.png", options);
Imports IronBarCode

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection() From {New AdaptiveThresholdFilter()},
	.AutoRotate = True
}

' Read barcode
Private results As BarcodeResults = BarcodeReader.Read("TryHarderQR.png", options)
VB   C#

Reading Multiple Barcodes

PDF Documents

In our next example, we are going to look at reading a scanned PDF document and find all of the barcodes of one-dimensional format in very few lines of code.

As you can see, it is very similar to reading a single barcode from a single document, except we now have new information about which page number the barcode was found on.

:path=/static-assets/barcode/content-code-examples/tutorials/reading-barcodes-4.cs
using IronBarCode;
using System;

// Multiple barcodes may be scanned up from a single document or image. A PDF document may also used as the input image
BarcodeResults results = BarcodeReader.ReadPdf("MultipleBarcodes.pdf");

// Work with the results
foreach (var pageResult in results)
{
    string Value = pageResult.Value;
    int PageNum = pageResult.PageNumber;
    System.Drawing.Bitmap Img = pageResult.BarcodeImage;
    BarcodeEncoding BarcodeType = pageResult.BarcodeType;
    byte[] Binary = pageResult.BinaryValue;
    Console.WriteLine(pageResult.Value + " on page " + PageNum);
}
Imports IronBarCode
Imports System

' Multiple barcodes may be scanned up from a single document or image. A PDF document may also used as the input image
Private results As BarcodeResults = BarcodeReader.ReadPdf("MultipleBarcodes.pdf")

' Work with the results
For Each pageResult In results
	Dim Value As String = pageResult.Value
	Dim PageNum As Integer = pageResult.PageNumber
	Dim Img As System.Drawing.Bitmap = pageResult.BarcodeImage
	Dim BarcodeType As BarcodeEncoding = pageResult.BarcodeType
	Dim Binary() As Byte = pageResult.BinaryValue
	Console.WriteLine(pageResult.Value & " on page " & PageNum)
Next pageResult
VB   C#

We find the following barcodes on different pages.

C# - Reading Barcodes from a PDF results

Scans TIFFs

In our next example, we can see that the same result can be obtained from a multi-frame TIFF, which will be treated similarly to a PDF in this context.

C# - Reading Barcodes from a multi-frame TIFF image
:path=/static-assets/barcode/content-code-examples/tutorials/reading-barcodes-5.cs
using IronBarCode;

// Multi frame TIFF and GIF images can also be scanned
BarcodeResults multiFrameResults = BarcodeReader.Read("Multiframe.tiff");

foreach (var pageResult in multiFrameResults)
{
    //...
}
Imports IronBarCode

' Multi frame TIFF and GIF images can also be scanned
Private multiFrameResults As BarcodeResults = BarcodeReader.Read("Multiframe.tiff")

For Each pageResult In multiFrameResults
	'...
Next pageResult
VB   C#

MultiThreading

To read multiple documents, we can get better results from IronBarcode by creating a list of the documents and using the BarcodeReader.Read method. This uses multiple threads and potentially all cores of your CPU for the barcode scanning process and can be exponentially faster than reading barcodes one at a time.

:path=/static-assets/barcode/content-code-examples/tutorials/reading-barcodes-6.cs
using IronBarCode;

// The Multithreaded property allows for faster barcode scanning across multiple images or PDFs. All threads are automatically managed by IronBarCode.
var ListOfDocuments = new[] { "image1.png", "image2.JPG", "image3.pdf" };

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Enable multithreading
    Multithreaded = true,
};

BarcodeResults batchResults = BarcodeReader.Read(ListOfDocuments, options);
Imports IronBarCode

' The Multithreaded property allows for faster barcode scanning across multiple images or PDFs. All threads are automatically managed by IronBarCode.
Private ListOfDocuments = { "image1.png", "image2.JPG", "image3.pdf" }

Private options As New BarcodeReaderOptions() With {.Multithreaded = True}

Private batchResults As BarcodeResults = BarcodeReader.Read(ListOfDocuments, options)
VB   C#

Summary

In summary, IronBarcode is a versatile .NET software library and C# QR code generator capable of reading a wide range of barcode formats. It can do so regardless of whether the barcodes are perfect screen captures or imperfect real-world images, such as photographs or scans.

Further Reading

To learn more about working with IronBarcode, you may want to explore the other tutorials in this section, as well as the examples on our homepage, which most developers will find sufficient to get started.

Our API Reference, specifically the BarcodeReader class and the BarcodeEncoding enum, will provide detailed information on what you can achieve with this C# barcode library.

Source Code Downloads

We also highly encourage you to download this tutorial and run it yourself. You can do this by downloading the source code or by forking us on GitHub. The source for this .NET barcode reader tutorial is available as a Visual Studio 2017 Console Application project written in C#.

.NET Software Engineer For many this is the most efficient way to generate PDF files from .NET, because there is no additional API to learn, or complex design system to navigate

Frank Walker

.NET Product Developer

Frank has been a founding force in the development of IronBarcode. Working with IronOCR for the last year, Frank has been influential to building use cases within OCR for IronBarcode to build out into the tool it is now.