Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
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.
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.
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
The Barcode Library can be installed using one of the following three methods:
Write the following command in the Package Manager Console. It will download and install the package for you.
Install-Package BarCode
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
Reading a barcode or QR code in .NET is incredibly easy using the IronBarcode library with .NET Barcode Reader.
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
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
The barcode scanner displays the barcode data in the text box as follows:
Barcode Image to be Scanned with C#
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
The output will be as follows after opening the skewed QR code image:
Skewed QrCode Image
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
Barcode and QR code present in PDF files:
C# - Reading Barcodes from a PDF results
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.
The IronBarcode Library uses the C# Barcode Generator, which is even capable of reading a corrupted thumbnail of a barcode.
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")
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.
9 .NET API products for your office documents