Read Barcodes in C#
IronBarcode provides a versatile, advanced, and efficient library for reading barcodes in .NET.
How to Read Barcodes in C# and VB.NET
- Install IronBarcode from NuGet or via DLL download
- Use the
BarcodeReader.Read
method to read any barcode or QR - Read Multiple Barcodes or QRs in a single scan, PDF, or a multiframe Tiff file
- Allow IronBarcode to read from imperfect scans and photos
- Download the tutorial project and get scanning now
Installation
Start using IronBarcode in your project today with a free trial.
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.

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;
// Attempt to read one or more barcodes from the specified image file.
BarcodeResults results = BarcodeReader.Read("GetStarted.png");
// Check if the results are not null and at least one barcode was found.
if (results != null && results.Count > 0)
{
// Iterate over each barcode result within the results.
foreach (BarcodeResult result in results)
{
// Output the text value of the barcode result to the console.
// This indicates that reading the barcode was successful.
Console.WriteLine("GetStarted was a success. Read Value: " + result.Text);
}
}
else
{
// If no barcodes were detected, output a message to the console.
Console.WriteLine("No barcodes found in the image.");
}
Imports IronBarCode
Imports System
' Attempt to read one or more barcodes from the specified image file.
Private results As BarcodeResults = BarcodeReader.Read("GetStarted.png")
' Check if the results are not null and at least one barcode was found.
If results IsNot Nothing AndAlso results.Count > 0 Then
' Iterate over each barcode result within the results.
For Each result As BarcodeResult In results
' Output the text value of the barcode result to the console.
' This indicates that reading the barcode was successful.
Console.WriteLine("GetStarted was a success. Read Value: " & result.Text)
Next result
Else
' If no barcodes were detected, output a message to the console.
Console.WriteLine("No barcodes found in the image.")
End If
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
// Import the IronBarCode library to work with barcode operations
using IronBarCode;
// Configure barcode reader options
// This includes setting the reading speed and specifying expected barcode formats
BarcodeReaderOptions options = new BarcodeReaderOptions
{
// Choose a reading speed for barcode analysis
// Possible values: Faster, Balanced, Detailed, ExtremeDetail
// Note: More detail typically results in slower performance but better accuracy
Speed = ReadingSpeed.ExtremeDetail,
// Specify which types of barcodes to expect for improved performance
// If left unspecified, all types will be scanned by default, which may reduce performance
ExpectBarcodeTypes = BarcodeEncoding.QRCode
BarcodeEncoding.Code128
};
// Read the barcode from the specified image file
// It returns the results containing information about recognized barcodes
BarcodeResults results = BarcodeReader.Read("TryHarderQR.png", options);
// Check if any barcodes were found and output their information
if (results != null && results.Barcodes.Count > 0)
{
foreach (var barcode in results.Barcodes)
{
// Output each detected barcode's type and text value
Console.WriteLine($"Barcode Type: {barcode.BarcodeType}, Value: {barcode.Text}");
}
}
else
{
// Inform the user that no barcodes were detected in the image
Console.WriteLine("No barcodes found in the image.");
}
' Import the IronBarCode library to work with barcode operations
Imports IronBarCode
' Configure barcode reader options
' This includes setting the reading speed and specifying expected barcode formats
Private options As New BarcodeReaderOptions With {
.Speed = ReadingSpeed.ExtremeDetail,
.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128
}
' Read the barcode from the specified image file
' It returns the results containing information about recognized barcodes
Private results As BarcodeResults = BarcodeReader.Read("TryHarderQR.png", options)
' Check if any barcodes were found and output their information
If results IsNot Nothing AndAlso results.Barcodes.Count > 0 Then
For Each barcode In results.Barcodes
' Output each detected barcode's type and text value
Console.WriteLine($"Barcode Type: {barcode.BarcodeType}, Value: {barcode.Text}")
Next barcode
Else
' Inform the user that no barcodes were detected in the image
Console.WriteLine("No barcodes found in the image.")
End If
Which will read this skewed QR Code:

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;
// Create an instance of BarcodeReaderOptions to configure barcode reading
BarcodeReaderOptions options = new BarcodeReaderOptions
{
// Choose which filters are to be applied (in order)
ImageFilters = new ImageFilterCollection
{
new AdaptiveThresholdFilter() // Applies an adaptive thresholding filter
},
// Uses machine learning to automatically rotate the barcode if needed
AutoRotate = true
};
// Read barcode from the specified image file using the configured options
BarcodeResults results = BarcodeReader.Read("TryHarderQR.png", options);
// Process the results
foreach (var result in results)
{
// Display the barcode type and text
Console.WriteLine($"Type: {result.BarcodeType}, Value: {result.Text}");
}
Imports IronBarCode
' Create an instance of BarcodeReaderOptions to configure barcode reading
Private options As New BarcodeReaderOptions With {
.ImageFilters = New ImageFilterCollection From {New AdaptiveThresholdFilter()},
.AutoRotate = True
}
' Read barcode from the specified image file using the configured options
Private results As BarcodeResults = BarcodeReader.Read("TryHarderQR.png", options)
' Process the results
For Each result In results
' Display the barcode type and text
Console.WriteLine($"Type: {result.BarcodeType}, Value: {result.Text}")
Next result
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.
:path=/static-assets/barcode/content-code-examples/tutorials/reading-barcodes-4.cs
using System;
using IronBarCode;
// This code is designed to read and process multiple barcodes from a PDF document named "MultipleBarcodes.pdf".
try
{
// Read barcodes from the specified PDF document.
// The BarcodeReader.ReadPdf method returns a collection of BarcodeResult objects.
BarcodeResults results = BarcodeReader.ReadPdf("MultipleBarcodes.pdf");
// Check if any barcodes were found and process the results.
if (results != null && results.Count > 0)
{
// Each BarcodeResult represents one barcode found on a specific page of the PDF.
foreach (var pageResult in results)
{
// Retrieve the value (text) of the barcode.
string value = pageResult.Text; // Use .Text instead of .Value to retrieve the barcode's readable text.
// Retrieve the page number on which the barcode was found.
int pageNum = pageResult.PageNumber;
// Retrieve the barcode image as a Bitmap.
System.Drawing.Bitmap img = pageResult.BarcodeBitmap;
// Retrieve the barcode encoding type.
BarcodeEncoding barcodeType = pageResult.BarcodeFormat; // Use the appropriate property for encoding type.
// Retrieve the binary representation of the barcode value.
byte[] binary = pageResult.BinaryValue;
// Output the barcode value and page number to the console.
Console.WriteLine($"{value} on page {pageNum}");
}
}
else
{
Console.WriteLine("No barcodes found in the PDF document.");
}
}
catch (Exception ex)
{
// Handle exceptions that may occur during barcode reading.
Console.WriteLine($"An error occurred: {ex.Message}");
}
Imports System
Imports IronBarCode
' This code is designed to read and process multiple barcodes from a PDF document named "MultipleBarcodes.pdf".
Try
' Read barcodes from the specified PDF document.
' The BarcodeReader.ReadPdf method returns a collection of BarcodeResult objects.
Dim results As BarcodeResults = BarcodeReader.ReadPdf("MultipleBarcodes.pdf")
' Check if any barcodes were found and process the results.
If results IsNot Nothing AndAlso results.Count > 0 Then
' Each BarcodeResult represents one barcode found on a specific page of the PDF.
For Each pageResult In results
' Retrieve the value (text) of the barcode.
Dim value As String = pageResult.Text ' Use .Text instead of .Value to retrieve the barcode's readable text.
' Retrieve the page number on which the barcode was found.
Dim pageNum As Integer = pageResult.PageNumber
' Retrieve the barcode image as a Bitmap.
Dim img As System.Drawing.Bitmap = pageResult.BarcodeBitmap
' Retrieve the barcode encoding type.
Dim barcodeType As BarcodeEncoding = pageResult.BarcodeFormat ' Use the appropriate property for encoding type.
' Retrieve the binary representation of the barcode value.
Dim binary() As Byte = pageResult.BinaryValue
' Output the barcode value and page number to the console.
Console.WriteLine($"{value} on page {pageNum}")
Next pageResult
Else
Console.WriteLine("No barcodes found in the PDF document.")
End If
Catch ex As Exception
' Handle exceptions that may occur during barcode reading.
Console.WriteLine($"An error occurred: {ex.Message}")
End Try
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.
:path=/static-assets/barcode/content-code-examples/tutorials/reading-barcodes-5.cs
using IronBarCode;
// Multi-frame TIFF and GIF images can also be scanned using IronBarCode.
// The following code reads barcodes from a multi-frame TIFF image.
// Use the BarcodeReader class from the IronBarCode library
// to read barcode data from the specified TIFF file.
BarcodeResults multiFrameResults = BarcodeReader.Read("Multiframe.tiff");
// Iterate over each page result in the multi-frame results.
// Each page in the TIFF can have zero or more barcodes.
foreach (var pageResult in multiFrameResults)
{
// Process each barcode result from the page.
// Below, we output the value of the barcode found on each page.
// Replace this with any processing you need to perform with each barcode.
Console.WriteLine(pageResult.BarcodeValue);
}
Imports IronBarCode
' Multi-frame TIFF and GIF images can also be scanned using IronBarCode.
' The following code reads barcodes from a multi-frame TIFF image.
' Use the BarcodeReader class from the IronBarCode library
' to read barcode data from the specified TIFF file.
Private multiFrameResults As BarcodeResults = BarcodeReader.Read("Multiframe.tiff")
' Iterate over each page result in the multi-frame results.
' Each page in the TIFF can have zero or more barcodes.
For Each pageResult In multiFrameResults
' Process each barcode result from the page.
' Below, we output the value of the barcode found on each page.
' Replace this with any processing you need to perform with each barcode.
Console.WriteLine(pageResult.BarcodeValue)
Next pageResult
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; // Import the IronBarCode namespace for barcode reading functionality
// This example demonstrates how to perform barcode reading on multiple documents efficiently using multithreading.
// IronBarCode library is utilized for barcode scanning. Barcodes from images or PDFs will be read using multiple threads,
// allowing for faster processing of multiple files.
// Define the list of document paths (images or PDFs) to be processed.
var listOfDocuments = new[] { "image1.png", "image2.JPG", "image3.pdf" };
// Configure options for the barcode reader.
// These options will be used to optimize barcode reading performance across multiple files.
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
// Enable multithreading. This allows the barcode reading process to handle multiple images or PDFs concurrently.
Multithreaded = true,
};
// Perform the barcode reading across the list of documents with the specified options.
// batchResults will contain the barcodes read from all provided documents.
BarcodeResults batchResults = BarcodeReader.Read(listOfDocuments, options);
// Note:
// - Ensure that the IronBarCode library is correctly referenced in your project.
// - The images and PDFs specified in `listOfDocuments` must be accessible at the given paths.
Imports IronBarCode ' Import the IronBarCode namespace for barcode reading functionality
' This example demonstrates how to perform barcode reading on multiple documents efficiently using multithreading.
' IronBarCode library is utilized for barcode scanning. Barcodes from images or PDFs will be read using multiple threads,
' allowing for faster processing of multiple files.
' Define the list of document paths (images or PDFs) to be processed.
Private listOfDocuments = { "image1.png", "image2.JPG", "image3.pdf" }
' Configure options for the barcode reader.
' These options will be used to optimize barcode reading performance across multiple files.
Private options As New BarcodeReaderOptions() With {.Multithreaded = True}
' Perform the barcode reading across the list of documents with the specified options.
' batchResults will contain the barcodes read from all provided documents.
Private batchResults As BarcodeResults = BarcodeReader.Read(listOfDocuments, options)
' Note:
' - Ensure that the IronBarCode library is correctly referenced in your project.
' - The images and PDFs specified in `listOfDocuments` must be accessible at the given paths.
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#.
Frequently Asked Questions
How do I install IronBarcode in a .NET project?
IronBarcode can be installed in a .NET project using the NuGet package or by manually downloading the DLL to your project or global assembly cache. The NuGet package can be added using the command: dotnet add package BarCode.
How do I read a barcode using IronBarcode in C#?
To read a barcode using IronBarcode in C#, use the BarcodeReader.Read method. For example: var barcodeResult = BarcodeReader.Read('path_to_barcode_image.png');
Can IronBarcode read multiple barcodes in a single scan?
Yes, IronBarcode can read multiple barcodes or QR codes in a single scan, as well as from PDFs or multi-frame TIFF files.
What options are available for reading challenging barcode images?
IronBarcode provides advanced options like ExtremeDetail in the Speed enum for reading challenging images. Additionally, using properties like ImageFilters and AutoRotate can improve scanning accuracy.
How can I read barcodes from a PDF using IronBarcode?
To read barcodes from a PDF, use the BarcodeReader.ReadPdf method. This will scan the document and retrieve all barcodes within it.
What is the benefit of using multithreading with IronBarcode?
Using multithreading with IronBarcode allows multiple documents to be processed simultaneously, utilizing all CPU cores, which can significantly speed up the barcode reading process.
Where can I find the source code for the IronBarcode tutorial?
The source code for the IronBarcode tutorial is available on GitHub at the Iron-Barcode-Reading-Barcodes-In-CSharp repository. It can also be downloaded as a zip file from the tutorial page.
What barcode formats can IronBarcode read?
IronBarcode can read a wide range of barcode formats, including QR codes and various one-dimensional barcode formats, even from imperfect images.