How to Use Barcode Scanners in C# Windows Application

In this tutorial, we will look at how to scan QR codes and barcodes in C# console applications and .NET Windows Forms Application, with an example using the Iron Barcode library.

We will see how easy it is to scan and read barcodes. Using the IronBarcode library we can read multiple barcodes at once, and also scan imperfect images successfully. Let's first clarify what a barcode scanner is.

What is a Barcode Scanner?

A barcode is a square or rectangular image consisting of a series of parallel black lines and white spaces of varying widths. A barcode scanner or barcode reader is a device that can read printed barcodes, decode the data contained in the barcode and send the data to a computer.

Here we are going to make our own barcode scanner with the help of the Iron Barcode Library.

How to Read Barcodes in C#

  • Create a .NET Windows Forms Application project in Microsoft Visual Studio
  • Install the barcode library
  • Read any barcode or QR code
  • Read multiple barcodes or QRs in a single scan
  • Allow Iron Barcode to read from imperfect scans and photos

1. Create a Windows Forms Application in Microsoft Visual Studio

Open Visual Studio = > Click on Create New Project = > Select Windows Forms Application Template = > Press Next => Name the Project => Press Next => Select your target .NET Framework => Click on the Create Button.

After creating the project, design the form as follows from the Visual Studio toolbox: PictureBox, Label, Textbox, and Button controls.

Barcode Scanner

2. Install the BarCode .NET Library in C

Before we begin, we need to install the Barcode Library. You can install it by using one of the following three methods:

1. Package Manager Console

Write the following command in the Package Manager console. It will download and install the package for you.

PM > Install-Package BarCode

2. NuGet Packages Manager Solution

You can also install the Barcode Library by using the NuGet Package Solution. Simply follow these steps:

Click on Tools => NuGet Package Manager => Manage NuGet package Solution.

This will open NuGet Package Manager for you. Click on Browse and search for Barcode, then install the class library.

As an alternative, the IronBarCode.Dll can be downloaded and added to your project as a reference from [.NET Barcode DLL].

After downloading, add the following references to your barcode reader project.

using IronBarCode;
using IronBarCode;
Imports IronBarCode
VB   C#

3. Read any Barcode or QR Code

Reading a barcode or QR code in .NET is incredibly easy using the Iron Barcode class library with .NET Barcode Reader.

Barcode Scanner

In your project browse for the image, you wish to read. It will open it in PictureBox; now click on "scan code". The text will appear in the text box.

Here is the code for the "browse" button to open an image:

// open file dialog   
OpenFileDialog open = new OpenFileDialog();  
// image filters  
open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg; *.png; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK) {  
// display image in picture box
    pictureBox1.Image = new Bitmap(open.FileName); 
// store image file path in class data member. Initialize it as string ImageFileName;
    ImageFileName = open.FileName; 
}
// open file dialog   
OpenFileDialog open = new OpenFileDialog();  
// image filters  
open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg; *.png; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK) {  
// display image in picture box
    pictureBox1.Image = new Bitmap(open.FileName); 
// store image file path in class data member. Initialize it as string ImageFileName;
    ImageFileName = open.FileName; 
}
' open file dialog   
OpenFileDialog open = new OpenFileDialog()

' image filters  
open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg; *.png; *.jpeg; *.gif; *.bmp"
if (open.ShowDialog() = DialogResult.OK)
If True Then
	
' display image in picture box
	pictureBox1.Image = new Bitmap(open.FileName)
	
' store image file path in class data member. Initialize it as string ImageFileName;
	ImageFileName = open.FileName
	
End If
VB   C#

The code for the "scan code" button:

BarcodeResult Result=BarcodeReader.QuicklyReadOneBarcode(ImageFileName);
textBox1.Text = Result.Text;
BarcodeResult Result=BarcodeReader.QuicklyReadOneBarcode(ImageFileName);
textBox1.Text = Result.Text;
Dim Result As BarcodeResult=BarcodeReader.QuicklyReadOneBarcode(ImageFileName)
textBox1.Text = Result.Text
VB   C#

Our barcode scanner displays the barcode data in the text box as follows:

Barcode Image to be Scanned with C#

QR Code Scanner

Here we will scan a skewed QR code to check the complexity level of the library. Although the skewed angle QR code can be handled and read by the QuicklyReadOneBarcode method, it can nevertheless take more time to resolve. The Iron Barcode library provides a different method to deal with such image input. The code goes as follows:

BarcodeResult Result = IronBarCode.BarcodeReader.ReadASingleBarcode(ImageFileName, BarcodeEncoding.QRCode | BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);textBox1.Text = Result.Text;
BarcodeResult Result = IronBarCode.BarcodeReader.ReadASingleBarcode(ImageFileName, BarcodeEncoding.QRCode | BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);textBox1.Text = Result.Text;
Dim Result As BarcodeResult = IronBarCode.BarcodeReader.ReadASingleBarcode(ImageFileName, BarcodeEncoding.QRCode Or BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels)
textBox1.Text = Result.Text
VB   C#

The output will be as follows after opening the skewed QR code image:

Skewed QrCode Image

Reading Multiple Barcodes in a Single Scan

PDF Documents

Here we are going to discuss only PDF documents. Barcode images can be scanned from a PDF file and each result can be displayed appropriately as desired. The following sample code allows you to read multiple barcodes from a PDF file.

// Multiple barcodes may be scanned up from a single document or image.  A PDF document may also used as the input 
imagePagedBarcodeResult[] 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);
}  
// Multiple barcodes may be scanned up from a single document or image.  A PDF document may also used as the input 
imagePagedBarcodeResult[] 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);
}  
' Multiple barcodes may be scanned up from a single document or image.  A PDF document may also used as the input 
Dim PDFResults() As imagePagedBarcodeResult = 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#

Barcode and QR code present in PDF files:

C# - Reading Barcodes from a PDF results

Reading Barcodes from Imperfect Images

In real-world-use cases, we may wish to read barcodes that are not perfect screenshots. They may be imperfect images, scans, thumbnails, or photographs and contain digital noise or be skewed. Here in our barcode reader, we will look at the example of reading the barcode data from thumbnails.

Thumbnails

Our barcode scanner made using 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#

It automatically detects barcode images that are too small to reasonably represent an actual barcode, and then upscales and cleans all of the digital noise associated with thumbnailing, thereby 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

Iron Barcode is a versatile .NET software library and C# QR Code Generator for scanning and reading a wide range of barcode image 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. For more information about Iron Barcode, please visit this link.

Currently, if you buy the complete Iron Suite, you can get five libraries for the price of just two. For more details, please click here.