Read Barcodes in C#

by Frank Walker

Install the Barcode Library to your Visual Studio Project

C# NuGet Library for

Install with NuGet

Install-Package BarCode
or
C#  DLL

Download DLL

Download DLL

Manually install into your project

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 Iron Barcode 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.

using IronBarCode;
using System;

BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("GetStarted.png");

if (Result !=null && Result.Text == "https://ironsoftware.com/csharp/barcode/")
{
  Console.WriteLine("GetStarted was a success.  Read Value: " + Result.Text);
}
using IronBarCode;
using System;

BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("GetStarted.png");

if (Result !=null && Result.Text == "https://ironsoftware.com/csharp/barcode/")
{
  Console.WriteLine("GetStarted was a success.  Read Value: " + Result.Text);
}
Imports IronBarCode
Imports System

Private Result As BarcodeResult = BarcodeReader.QuicklyReadOneBarcode("GetStarted.png")

If Result IsNot Nothing AndAlso Result.Text = "https://ironsoftware.com/csharp/barcode/" Then
  Console.WriteLine("GetStarted was a success.  Read Value: " & Result.Text)
End If
VB   C#

Try Harder and Be Specific

In this next example, we will add our TryHarder variable to the QuicklyReadOneBarcode method. This makes it try harder, literally taking more time, but scanning deeper for a QR code that might be obscured, corrupted, or at a skewed angle.

BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("TryHarderQR.png", BarcodeEncoding.QRCode | BarcodeEncoding.Code128 , true);
BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("TryHarderQR.png", BarcodeEncoding.QRCode | BarcodeEncoding.Code128 , true);
Dim Result As BarcodeResult = BarcodeReader.QuicklyReadOneBarcode("TryHarderQR.png", BarcodeEncoding.QRCode Or BarcodeEncoding.Code128, True)
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) that we are looking for or specify multiple formats. Doing so greatly improves barcode reading performance and accuracy. The | pipe character or 'Bitwize OR' is used to specify multiple formats simultaneously.

The same can be achieved but with a higher degree of specificity if we move forwards to use the BarcodeReader.ReadASingleBarcode Method.

BarcodeResult Result = IronBarCode.BarcodeReader.ReadASingleBarcode("TryHarderQR.png", BarcodeEncoding.QRCode | BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);
BarcodeResult Result = IronBarCode.BarcodeReader.ReadASingleBarcode("TryHarderQR.png", BarcodeEncoding.QRCode | BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);
Dim Result As BarcodeResult = IronBarCode.BarcodeReader.ReadASingleBarcode("TryHarderQR.png", BarcodeEncoding.QRCode Or BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels)
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.


using IronBarCode;
using System;
using System.Drawing;

// Multiple barcodes may be scanned up from a single document or image.  A PDF document may also used as the input image
PagedBarcodeResult[] PDFResults = BarcodeReader.ReadBarcodesFromPdf("MultipleBarcodes.pdf");

// Work with the results
foreach (var PageResult in PDFResults)
{
    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);
}     

using IronBarCode;
using System;
using System.Drawing;

// Multiple barcodes may be scanned up from a single document or image.  A PDF document may also used as the input image
PagedBarcodeResult[] PDFResults = BarcodeReader.ReadBarcodesFromPdf("MultipleBarcodes.pdf");

// Work with the results
foreach (var PageResult in PDFResults)
{
    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
Imports System.Drawing

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

' Work with the results
For Each PageResult In PDFResults
	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 and TIFFs

In our next example, we can see that the same result may be found from a multi-frame TIFF, which will be treated similar to a PDF in this respect.

C# - Reading Barcodes from a multi-frame TIFF image
// Multi frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
PagedBarcodeResult[] MultiFrameResults = BarcodeReader.ReadBarcodesFromMultiFrameTiff("Multiframe.tiff", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);            

 foreach (var PageResult in MultiFrameResults)
  {
    //...
  }
// Multi frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
PagedBarcodeResult[] MultiFrameResults = BarcodeReader.ReadBarcodesFromMultiFrameTiff("Multiframe.tiff", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);            

 foreach (var PageResult in MultiFrameResults)
  {
    //...
  }
' Multi frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
Dim MultiFrameResults() As PagedBarcodeResult = BarcodeReader.ReadBarcodesFromMultiFrameTiff("Multiframe.tiff", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels)

 For Each PageResult In MultiFrameResults
	'...
 Next PageResult
VB   C#

MultiThreading

To read multiple documents, we get better results from Iron Barcode by creating a list of the documents and using the BarcodeReader.ReadBarcodesMultithreaded 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.

// The BarcodeResult.ReadBarcodesMultiThreaded method allows for faster barcode scanning of multiple images or PDFs.  All threads are automatically managed by IronBarCode.
var ListOfDocuments = new[] { "Image1.png", "image2.JPG", "image3.pdf" };
PagedBarcodeResult[] BatchResults = BarcodeReader.ReadBarcodesMultiThreaded(ListOfDocuments);

// Work with the results
foreach (var Result in BatchResults)
{
    string Value = Result.Value;
    //...
}
// The BarcodeResult.ReadBarcodesMultiThreaded method allows for faster barcode scanning of multiple images or PDFs.  All threads are automatically managed by IronBarCode.
var ListOfDocuments = new[] { "Image1.png", "image2.JPG", "image3.pdf" };
PagedBarcodeResult[] BatchResults = BarcodeReader.ReadBarcodesMultiThreaded(ListOfDocuments);

// Work with the results
foreach (var Result in BatchResults)
{
    string Value = Result.Value;
    //...
}
' The BarcodeResult.ReadBarcodesMultiThreaded method allows for faster barcode scanning of multiple images or PDFs.  All threads are automatically managed by IronBarCode.
Dim ListOfDocuments = { "Image1.png", "image2.JPG", "image3.pdf" }
Dim BatchResults() As PagedBarcodeResult = BarcodeReader.ReadBarcodesMultiThreaded(ListOfDocuments)

' Work with the results
For Each Result In BatchResults
	Dim Value As String = Result.Value
	'...
Next Result
VB   C#

Reading Barcodes from Imperfect Images

In real world use cases, we may wish to read barcodes which are not perfect screenshots. They may be imperfect images, scans, or photographs and contain digital noise or be skewed. With most conventional open source .net barcode generator and reader libraries, this would be impossible. However, this Barcode Reader in C# makes this incredibly straightforward.

In our next example, we will look at the TryHarder method of QuicklyReadOneBarcode. This single parameter causes Iron Barcode to try to de-skew and read barcodes from imperfect digital samples.

Photographs

In the photographs example, we will set specific barcode rotation correction and barcode image correction to correct for the digital noise and for the skew and perspective and rotation that we might reasonably expect from a cellphone camera.

Reading a barcode from a phone camera in C#
using IronBarCode;
using System;
using System.Drawing;

// All BarcodeResult.Read methods provide the developer with control to correct image and photograph correction and straightening rotation and perspective from skewed images
// * RotationCorrection   e.g BarcodeReader.BarcodeRotationCorrection.Extreme  un-rotates and removes perspective from barcode images.
// * ImageCorrection      e.g BarcodeReader.BarcodeImageCorrection.DeepCleanPixels  separates Barcodes from background imagery and digital noise.
// * BarcodeEncoding      e.g. BarcodeEncoding.Code128  Setting a specific Barcode format improves speed and reduces the risk of false positive results

// Example with a photo image
var PhotoResult = BarcodeReader.ReadASingleBarcode("Photo.png", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Medium, BarcodeReader.BarcodeImageCorrection.DeepCleanPixels);

string Value = PhotoResult.Value;
System.Drawing.Bitmap Img = PhotoResult.BarcodeImage;
BarcodeEncoding BarcodeType = PhotoResult.BarcodeType;
byte[] Binary = PhotoResult.BinaryValue;
Console.WriteLine(PhotoResult.Value);  
using IronBarCode;
using System;
using System.Drawing;

// All BarcodeResult.Read methods provide the developer with control to correct image and photograph correction and straightening rotation and perspective from skewed images
// * RotationCorrection   e.g BarcodeReader.BarcodeRotationCorrection.Extreme  un-rotates and removes perspective from barcode images.
// * ImageCorrection      e.g BarcodeReader.BarcodeImageCorrection.DeepCleanPixels  separates Barcodes from background imagery and digital noise.
// * BarcodeEncoding      e.g. BarcodeEncoding.Code128  Setting a specific Barcode format improves speed and reduces the risk of false positive results

// Example with a photo image
var PhotoResult = BarcodeReader.ReadASingleBarcode("Photo.png", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Medium, BarcodeReader.BarcodeImageCorrection.DeepCleanPixels);

string Value = PhotoResult.Value;
System.Drawing.Bitmap Img = PhotoResult.BarcodeImage;
BarcodeEncoding BarcodeType = PhotoResult.BarcodeType;
byte[] Binary = PhotoResult.BinaryValue;
Console.WriteLine(PhotoResult.Value);  
Imports IronBarCode
Imports System
Imports System.Drawing

' All BarcodeResult.Read methods provide the developer with control to correct image and photograph correction and straightening rotation and perspective from skewed images
' * RotationCorrection   e.g BarcodeReader.BarcodeRotationCorrection.Extreme  un-rotates and removes perspective from barcode images.
' * ImageCorrection      e.g BarcodeReader.BarcodeImageCorrection.DeepCleanPixels  separates Barcodes from background imagery and digital noise.
' * BarcodeEncoding      e.g. BarcodeEncoding.Code128  Setting a specific Barcode format improves speed and reduces the risk of false positive results

' Example with a photo image
Private PhotoResult = BarcodeReader.ReadASingleBarcode("Photo.png", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Medium, BarcodeReader.BarcodeImageCorrection.DeepCleanPixels)

Private Value As String = PhotoResult.Value
Private Img As System.Drawing.Bitmap = PhotoResult.BarcodeImage
Private BarcodeType As BarcodeEncoding = PhotoResult.BarcodeType
Private Binary() As Byte = PhotoResult.BinaryValue
Console.WriteLine(PhotoResult.Value)
VB   C#

Scans

The next example shows us how we might approach reading QR codes and PDF-417 barcodes from a scanned PDF. Note that we have set an appropriate level of barcode rotation correction and barcode image correction to lightly clean the document, but not to incur a huge performance penalty by over-egging our needs.

Reading barcodes from a scanned PDF document in C#
// Multi frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
var ScanResults = BarcodeReader.ReadBarcodesFromPdf("Scan.pdf", BarcodeEncoding.All, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.LightlyCleanPixels);

// Work with the results
foreach (var PageResult in ScanResults)
{
  string Value = PageResult.Value;
  ///...
} 
// Multi frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
var ScanResults = BarcodeReader.ReadBarcodesFromPdf("Scan.pdf", BarcodeEncoding.All, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.LightlyCleanPixels);

// Work with the results
foreach (var PageResult in ScanResults)
{
  string Value = PageResult.Value;
  ///...
} 
' Multi frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
Dim ScanResults = BarcodeReader.ReadBarcodesFromPdf("Scan.pdf", BarcodeEncoding.All, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.LightlyCleanPixels)

' Work with the results
For Each PageResult In ScanResults
  Dim Value As String = PageResult.Value
  '''...
Next PageResult
VB   C#

Thumbnails

In the final example, we will see that this C# Barcode Generator is even capable of reading a corrupted thumbnail of a barcode.

Automatic barcode thumbnail size correction.  File readable using Iron Barcode in C#

Our barcode reader methods automatically detect barcode images which are too small to reasonably be an actual barcode, and upscale and clean all of the digital noise associated with thumbnailing; allowing them to be readable again.

// Small or 'Thumbnail' barcode images are automatically detected by IronBarCode and corrected for wherever possible even if they have much digital noise.
BarcodeResult SmallResult = BarcodeReader.QuicklyReadOneBarcode("ThumbnailOfBarcode.gif", BarcodeEncoding.Code128);
// Small or 'Thumbnail' barcode images are automatically detected by IronBarCode and corrected for wherever possible even if they have much digital noise.
BarcodeResult SmallResult = BarcodeReader.QuicklyReadOneBarcode("ThumbnailOfBarcode.gif", BarcodeEncoding.Code128);
' Small or 'Thumbnail' barcode images are automatically detected by IronBarCode and corrected for wherever possible even if they have much digital noise.
Dim SmallResult As BarcodeResult = BarcodeReader.QuicklyReadOneBarcode("ThumbnailOfBarcode.gif", BarcodeEncoding.Code128)
VB   C#

Summary

In summary, Iron Barcode is a versatile .NET software library and C# QR Code Generator for reading a wide range of barcode formats, and it can do so whether or not these barcodes are perfect screen grabs or are in fact photographs, scans, or other imperfect real world images.

Further Reading

To learn more about working with Iron Barcode, you may wish to look at the other tutorials within this section, and also the examples on our homepage; which most developers find enough to get them started.

Our API Reference with specific reference to the BarcodeReader class and the BarcodeEncoding Enum will show you in detail what you are capable of achieving using this C# Barcode Library.

Source Code Downloads

We also highly encourage you to download this tutorial and run it for yourself. You can do this by downloading the source code, or by forking us on GitHub. The source for this .NET Barcode Reader tutorial are available as 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 Iron Barcode. Working with IronOCR for the last year, Frank has been influential to building use cases within OCR for Iron Barcode to build out into the tool it is now.