Reading Barcodes is one important functions for many developers building an invoicing, delivery or accounting system. IronBarcode provides the ability to do this from within .Net projects.  This tutorial provides advice and instruction on using Iron Barcode to do so.

C# Barcode .Net

3rd November 2018 by Frank Walker

Reading Barcodes in C#, VB .NET, and the .NET Framework

Reading Barcodes in .Net

How to Read Barcodes in C# and VB.NET

  1. Install IronBarcode from Nuget or the DLL download
  2. Use the BarcodeReader.QuicklyReadOneBarcode method to read any barcode or QR
  3. Read Multiple Barcodes or QRs in a single scan, PDF, or a multiframe Tiff file
  4. Allow Iron Barcode to read from imperfect scans and photos
  5. Download the tutorial project and get scanning now

Install the Barcode Library to your Visual Studio Project

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

 PM > Install-Package Barcode

Read your First Barcode

Reading a Barcode or QR Code in .NET is incredibly easy using the Iron Barcode class library. 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);
}
Imports IronBarCode
Imports System
Dim Result As BarcodeResult = BarcodeReader.QuicklyReadOneBarcode("GetStarted.png")
If ((Not (Result) Is 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);
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 acuracy. 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);
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);
}     
Imports IronBarCode
Imports System
Imports System.Drawing
Dim 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
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)
  {
    //...
  }
Dim MultiFrameResults() As PagedBarcodeResult = BarcodeReader.ReadBarcodesFromMultiFrameTiff("Multiframe.tiff", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels)
For Each PageResult In MultiFrameResults
    '...
Next
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;
    //...
}
Dim ListOfDocuments As var
,"image2.JPG"
,"image3.pdf"
UnknownDim BatchResults() As PagedBarcodeResult = BarcodeReader.ReadBarcodesMultiThreaded(ListOfDocuments)
' Work with the results
For Each Result In BatchResults
    Dim Value As String = Result.Value
    '...
Next
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 conventional barcode reading libraries and most open source libraries, this would be impossible. However, Iron Barcode 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);  
Imports IronBarCode
Imports System
Imports System.Drawing
Dim PhotoResult As var = BarcodeReader.ReadASingleBarcode("Photo.png", BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Medium, BarcodeReader.BarcodeImageCorrection.DeepCleanPixels)
Dim Value As String = PhotoResult.Value
Dim Img As System.Drawing.Bitmap = PhotoResult.BarcodeImage
Dim BarcodeType As BarcodeEncoding = PhotoResult.BarcodeType
Dim 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.LighlyCleanPixels);

// Work with the results
foreach (var PageResult in ScanResults)
{
  string Value = PageResult.Value;
  ///...
} 
Dim ScanResults As var = BarcodeReader.ReadBarcodesFromPdf("Scan.pdf", BarcodeEncoding.All, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.LighlyCleanPixels)
' Work with the results
For Each PageResult In ScanResults
    Dim Value As String = PageResult.Value
    '''...
Next
VB   C#

Thumbnails

In the final example, we will see that Iron Barcode 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);
Dim SmallResult As BarcodeResult = BarcodeReader.QuicklyReadOneBarcode("ThumbnailOfBarcode.gif", BarcodeEncoding.Code128)
VB   C#

Summary

In summary, Iron Barcode is a versatile .Net software library library 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 Object 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 "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.