How to scan, read QR Code images in C# WPF application?
Here we will provide a step-by-step C# WPF desktop project tutorial to enable QR Code scanning function in a new WPF project using BarcodeLib
C# Barcode Reader library. By the end of guide, you will get a simple WPF application with QR Code scanner feature included.
This is a simple WPF tutorial that teaches the basics of building a WPF (.NET Core) application to scan and read QR Codes from image files on the WPF Window Application.
1. Create a new WPF (.NET Core) app
Here we will quickly create a new WPF (.NET Core) desktop app using Visual Studio .NET
Open Visual Studio 2022 application and choose Create a new project from Get Started section
In the Create a new project dialog, choose WPF Application with C# language, and click button Next.
In the Configure your new project dialog, enter BarcodeLib.QRCodeReader.WPF.Demo for Project name. It is important to
name the proejct BarcodeLib.QRCodeReader.WPF.Demo, including letter case, so the namespaces will match when you copy, paste the following
C# example source code. Then click button Next.
In the Additional information diaplog, select .NET 8.0 (Long Term Support) for the Framework dropdowns.
Click button Create.
Now we have created a new WPF application project successfully. We will enable QR Code scanning function in the WPF app.
2. Install Barcode Reader library and Nuget package
Now we will add and install BarcodeLib C# Barcode Reader library dll file to the new WPF application project
and also add the a Nuget package named System.Drawing.Common.
Add BarcodeLib C# Barcode Reader library
In the Solution Explorer window, right click Dependencies, select Add Project Reference...
Select button Browse... in the Reference Manager dialog.
Choose dll file BarcodeLib.BarcodeReader.Common.dll from BarcodeLib downloaded package /Dll/net-standard/. The barcode dll is built for .NET Standard 2.0
Check BarcodeLib.BarcodeReader.Common.dll is selected, and select button OK
Now we have added the barcodelib barcode scanner component dll to the WPF project.
We will start to add a library System.Drawing.Common from Nuget
Install NuGet Package
Right click Dependencies, select Manage NuGet Packages...
Navigate to tab Browse in the NuGet Package Manager
Enter System.Drawing.Common to search NuGet package
Choose the first searched result System.Drawing.Common by Microsoft
Select button Install
Now we have added the C# barcode reader library dll file and nuget package to the WPF project successfully.
3. Design QR Code scanner the user interface in WPF Designer
Select and click MainWindow.xaml in the "Solution Explorer" to open the WPF Designer. Switch to XAML view.
Adjust the WPF Window Width and Height.
<Windowx:Class="BarcodeLib.QRCodeReader.WPF.Demo.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:BarcodeLib.QRCodeReader.WPF.Demo"mc:Ignorable="d"Title="BarcodeLib C# QR Code Scanner WPF Windows App Demo"Height="500"Width="720"><Grid></Grid></Window>
Add all controls into the <Grid> tag.
A Label with application title displayed
A Button with a TextBox for choosing a source image file
A Button to start QR Code scanning
A TextBox for displaying the QR Code scan result
<Windowx:Class="BarcodeLib.QRCodeReader.WPF.Demo.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:BarcodeLib.QRCodeReader.WPF.Demo"mc:Ignorable="d"Title="BarcodeLib C# QR Code Scanner WPF Windows App Demo"Height="500"Width="720"><GridMargin="0,0,0,-5"><LabelContent="BarcodeLib C# WPF QR Code Scanner Demo Application"HorizontalAlignment="Left"Margin="84,19,0,0"VerticalAlignment="Top"FontSize="18"Background="Transparent"Foreground="#FFF70505"/><ButtonName="btSelectFile"Content="Select File"Margin="53,69,0,0"Width="120"HorizontalAlignment="Left"VerticalAlignment="Top"/><TextBoxName="tbFilePath"Text=""Margin="200,71,0,0"Width="169"HorizontalAlignment="Left"VerticalAlignment="Top"/><ButtonName="btScanBarcode"Content="Scan QR Code"Margin="98,420,0,0"Width="162"HorizontalAlignment="Left"VerticalAlignment="Top"Height="24"/><TextBoxName="tbScanResult"Margin="393,103,0,0"Width="291"Height="299"HorizontalAlignment="Left"VerticalAlignment="Top"/><Imagex:Name="imgPreview"HorizontalAlignment="Left"Height="297"Margin="42,103,0,0"VerticalAlignment="Top"Width="334"/></Grid></Window>
4. Add C# code to enable QR Code scanning in WPF app
Click MainWindow.xaml.cs to open the C# file in the Visual Studio.
Replace all contents in the MainWindow.xaml.cs file with the following C# codes.
usingBarcodeLib.BarcodeReader;
usingMicrosoft.Win32;
usingSystem.IO;
usingSystem.Text;
usingSystem.Windows;
usingSystem.Windows.Media.Imaging;
namespaceBarcodeLib.QRCodeReader.WPF.Demo
{
publicpartialclassMainWindow : Window
{
publicMainWindow()
{
InitializeComponent();
this.tbFilePath.IsReadOnly=true;
this.btScanBarcode.IsEnabled=false;
this.tbScanResult.IsReadOnly=true;
}
privatevoidbtSelectFile_Click(objectsender, RoutedEventArgse)
{
OpenFileDialogopenFileDialog=newOpenFileDialog();
if (openFileDialog.ShowDialog() ==true)
{
try
{
this.tbFilePath.Text=openFileDialog.FileName;
this.btScanBarcode.IsEnabled=true;
using (varstream=newFileStream(this.tbFilePath.Text, FileMode.Open, FileAccess.Read, FileShare.Read))
{
BitmapImagebitmap=newBitmapImage();
bitmap.BeginInit();
bitmap.StreamSource=stream;
bitmap.CacheOption=BitmapCacheOption.OnLoad;
bitmap.EndInit();
this.imgPreview.Source=bitmap;
}
}
catch (Exception) { }
}
}
privatevoidbtScanImage_Click(objectsender, RoutedEventArgse)
{
try
{
StringfilePath=this.tbFilePath.Text;
if (String.IsNullOrEmpty(filePath))
thrownewException("File path is null or empty.");
if (!File.Exists(filePath))
thrownewException("File does not exist.");
OptimizeSettingoptions=newOptimizeSetting(
BarcodeReader.BarcodeReader.QRCODE);
BarCode[] datas=BarcodeReader.BarcodeReader.ReadBarcode(filePath, options);
StringBuildersb=newStringBuilder();
if (datas.Length>0)
{
sb.Append("Number of QR Code(s): "+datas.Length+"\r\n");
foreach (BarCodebarcodeInfoindatas)
sb.Append("QR Code Text: '"+barcodeInfo.Data+"'\r\n");
}
else
{
sb.Append("No QR Code Scanned.");
}
this.tbScanResult.Text=sb.ToString();
}
catch (Exceptionex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
<ButtonName="btScanBarcode"Content="Scan QR Code"Margin="98,420,0,0"Width="162"HorizontalAlignment="Left"VerticalAlignment="Top"Click="btScanImage_Click"Height="24"/>
5. Run the demo WPF QR Code Scanner Windows app
Now we have completed a simpel WPF QR Code Scanner application. Run the WPF app directly from Visual Studio.
You can select a QR Code image from your local computer and scan to view the scanned QR Code data.