USING IRONBARCODE

How to Use Barcode Scanners in C# Windows Application

Updated January 20, 2024
Share:

This tutorial will demonstrate how to scan QR codes and barcodes in C# Console Applications and .NET Windows Forms Applications, using the IronBarcode library as an example.

Using the IronBarcode library, multiple barcodes can be scanned and read simultaneously, and it can also successfully scan imperfect images. 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.

The following steps will introduce how to create a barcode scanner with the help of the IronBarcode 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 QR codes in a single scan
  • Allow IronBarcode 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.

How to Use Barcode Scanners in C# Windows Application, Figure 1: Barcode Scanner Barcode Scanner

2. Install the Barcode .NET Library in C#

The Barcode Library can be installed 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.

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 Packages for 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.

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 IronBarcode 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   
Dim open As New OpenFileDialog()
' image filters  
open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg;*.png;*.jpeg;*.gif;*.bmp"
If open.ShowDialog() = DialogResult.OK 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.Read(ImageFileName);
textBox1.Text = Result.Text;
BarcodeResult Result = BarcodeReader.Read(ImageFileName);
textBox1.Text = Result.Text;
Dim Result As BarcodeResult = BarcodeReader.Read(ImageFileName)
textBox1.Text = Result.Text
VB   C#

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

How to Use Barcode Scanners in C# Windows Application, Figure 2: Barcode Image to be Scanned with C# Barcode Image to be Scanned with C#

QR Code Scanner

In this section, the IronBarcode Library effectively handles real-world situations involving skewed QR codes. Although the skewed angle QR code can be handled and read by the Read method, it can nevertheless take more time to resolve. The IronBarcode library provides a customized way of using BarcodeReaderOptions as an extra parameter to deal with such image input. The code goes as follows:

// Choose which filters are to be applied (in order);
var filtersToApply = new ImageFilterCollection() {
    new SharpenFilter(),
    new InvertFilter(),
    new ContrastFilter(),
    new BrightnessFilter(),
    new AdaptiveThresholdFilter(),
    new BinaryThresholdFilter()
};

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Set chosen filters in BarcodeReaderOptions:
    ImageFilters = filtersToApply,

    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
};
BarcodeResult Result = BarcodeReader.Read(ImageFileName, myOptionsExample);
textBox1.Text = Result.Text;
// Choose which filters are to be applied (in order);
var filtersToApply = new ImageFilterCollection() {
    new SharpenFilter(),
    new InvertFilter(),
    new ContrastFilter(),
    new BrightnessFilter(),
    new AdaptiveThresholdFilter(),
    new BinaryThresholdFilter()
};

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Set chosen filters in BarcodeReaderOptions:
    ImageFilters = filtersToApply,

    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
};
BarcodeResult Result = BarcodeReader.Read(ImageFileName, myOptionsExample);
textBox1.Text = Result.Text;
' Choose which filters are to be applied (in order);
Dim filtersToApply = New ImageFilterCollection() From {
	New SharpenFilter(),
	New InvertFilter(),
	New ContrastFilter(),
	New BrightnessFilter(),
	New AdaptiveThresholdFilter(),
	New BinaryThresholdFilter()
}

Dim myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = filtersToApply,
	.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128
}
Dim Result As BarcodeResult = BarcodeReader.Read(ImageFileName, myOptionsExample)
textBox1.Text = Result.Text
VB   C#

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

How to Use Barcode Scanners in C# Windows Application, Figure 4: Skewed QrCode Image Skewed QrCode Image

Reading Multiple Barcodes in a Single Scan

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.ReadPdf("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.ReadPdf("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.ReadPdf("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:

How to Use Barcode Scanners in C# Windows Application, Figure 3: C# - Reading Barcodes from a PDF results C# - Reading Barcodes from a PDF results

Reading Barcodes from Imperfect Images

In real-world use cases, barcodes are often found with imperfections in images, scans, thumbnails, or photographs, and may contain digital noise or be skewed. This section demonstrates how to read barcode data from thumbnails.

Thumbnails

The IronBarcode Library uses the C# Barcode Generator, which is even capable of reading a corrupted thumbnail of a barcode.

How to Use Barcode Scanners in C# Windows Application, Figure 5: Automatic barcode thumbnail size correction. File readable using IronBarcode in C# Automatic barcode thumbnail size correction. File readable using IronBarcode 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.Read("ThumbnailOfBarcode.gif");
// 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.Read("ThumbnailOfBarcode.gif");
' 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.Read("ThumbnailOfBarcode.gif")
VB   C#

Summary

IronBarcode 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. Additionally, IronBarcode offers a wide range of customization options to improve barcode reading speed, such as crop regions or multi-threading, and the accuracy of the ML model. Visit the official documents page for more information about IronBarcode.

Currently, if you buy the complete Iron Suite, you can get five libraries for the price of just two.

< PREVIOUS
Barcode Generator .NET Tutorial
NEXT >
How to Generate Barcodes in ASP.NET using C#

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 1,246,703 View Licenses >