Read Barcodes in C#
Published: 2022-02-01 02:23:06

---
canonical_url: https://ironsoftware.com/csharp/barcode/tutorials/reading-barcodes/
---

## How to Read Barcodes in C# and VB.NET

1. Install IronBarcode from [NuGet](https://www.nuget.org/packages/barcode) 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](https://ironsoftware.com/csharp/barcode/packages/IronBarCode.zip) to your project or to your global assembly cache. IronBarcode works well to produce a C# Barcode Scanner application.

`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 with [.NET Barcode Reader](https://ironsoftware.com/csharp/barcode/use-case/net-barcode-reader/). In our first example, we can see how to read this Barcode with one line of code.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s4luvjm4unxual4jwkce.png)

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

**C#:**
```
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);
}
```

**VB:**
```
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
```

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

**C#:**
```
BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("TryHarderQR.png", BarcodeEncoding.QRCode | BarcodeEncoding.Code128 , true);
```

**VB:**
```
Dim Result As BarcodeResult = BarcodeReader.QuicklyReadOneBarcode("TryHarderQR.png", BarcodeEncoding.QRCode Or BarcodeEncoding.Code128, True)
```

Which will read this skewed QR Code:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fqqn67qv4vgir2gj0kwy.png)

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.

**C#:**
```
BarcodeResult Result = IronBarCode.BarcodeReader.ReadASingleBarcode("TryHarderQR.png", BarcodeEncoding.QRCode | BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);
```

**VB:**
```
Dim Result As BarcodeResult = IronBarCode.BarcodeReader.ReadASingleBarcode("TryHarderQR.png", BarcodeEncoding.QRCode Or BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels)
```

## Reading Multiple Barcodes

### PDF Documents

In our next example, we are going to look at reading a [scanned PDF document](https://ironsoftware.com/img/tutorials/reading-barcodes/MultipleBarcodes.pdf) 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.

**C#:**
```
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);
}     
```

**VB:**
```
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
```

We find the following barcodes on different pages.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rp52msr0hgwey6ivly1o.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7h7f7vb66k5rlunqrmb.png)

**C#:**
```
// 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)
  {
    //...
  }
```

**VB:**
```
' 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
```

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

**C#:**
```
// 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;
    //...
}
```

**VB:**
```
' 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
```

## 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#](https://ironsoftware.com/csharp/barcode/use-case/barcode-reader-csharp/) 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2zlbed3bkcjnn7cxnkm.png)

**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);  
```

**VB:**
```
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)
```

### Scans

The next example shows us how we might approach reading QR codes and PDF-417 barcodes from a [scanned PDF](https://ironsoftware.com/img/tutorials/reading-barcodes/Scan.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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/85c1d276y4v4bmail6i5.png)

**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;
  ///...
} 
```

**VB:**
```
' 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
```

### Thumbnails

In the final example, we will see that this [C# Barcode Generator](https://ironsoftware.com/csharp/barcode/use-case/generate-barcode-csharp/) is even capable of reading a corrupted thumbnail of a barcode.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zn7rupzpvvywb2019eyd.png)

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.

**C#:**
```
// 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);
```

**VB:**
```
' 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)
```

## Summary

In summary, Iron Barcode is a versatile .Net software library and [C# QR Code Generator](https://ironsoftware.com/csharp/barcode/use-case/generate-qr-code-csharp/) 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](https://ironsoftware.com/csharp/barcode/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 [.NET Barcode Reader](https://ironsoftware.com/csharp/barcode/use-case/net-barcode-reader/) tutorial are available as Visual Studio 2017 Console Application project written in C#.

- [Tutorial Github Repository](https://github.com/iron-software/Iron-Barcode-Reading-Barcodes-In-CSharp)
- [C# Source Code in a Zip File](https://ironsoftware.com/downloads/assets/tutorials/reading-barcodes/Iron-Barcode-Reading-Barcodes-In-CSharp.zip)