C# Barcode Reader > C# Barcode Scanner Tutorial > How to scan, read QR Code in C# WPF application using C# Barcode Reader library?

How to scan, read QR Code in C# WPF Windows application?

Detailed tutorial for reading and scanning QR Code image in C# WPF desktop app

C# Barcode Reader Library

Scan QR Code, Data Matrix, Code 128, GS1, EAN/UPC from image files in WPF app.

Download Now
NuGet Ready
.NET 5+
.NET Framework 4.x, 3.x, 2.x




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.





How to scan, read QR Code in C# WPF Windows app?

Requirements

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
  1. Open Visual Studio 2022 application and choose Create a new project from Get Started section
  2. In the Create a new project dialog, choose WPF Application with C# language, and click button Next.
  3. 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.
  4. In the Additional information diaplog, select .NET 8.0 (Long Term Support) for the Framework dropdowns. Click button Create.
  5. 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

  1. In the Solution Explorer window, right click Dependencies, select Add Project Reference...
  2. Select button Browse... in the Reference Manager dialog.
  3. Choose dll file BarcodeLib.BarcodeReader.Common.dll from BarcodeLib downloaded package /Dll/net-standard/. The barcode dll is built for .NET Standard 2.0
  4. Check BarcodeLib.BarcodeReader.Common.dll is selected, and select button OK

  5. 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

  1. Right click Dependencies, select Manage NuGet Packages...
  2. Navigate to tab Browse in the NuGet Package Manager
  3. Enter System.Drawing.Common to search NuGet package
  4. Choose the first searched result System.Drawing.Common by Microsoft
  5. Select button Install

  6. 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.
  1. Adjust the WPF Window Width and Height.

    <Window x: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>
    
  2. 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
    <Window x: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 Margin="0,0,0,-5">
            <Label Content="BarcodeLib C# WPF QR Code Scanner Demo Application" 
                   HorizontalAlignment="Left" Margin="84,19,0,0" VerticalAlignment="Top" 
                   FontSize="18" Background="Transparent" Foreground="#FFF70505"/>
            <Button Name="btSelectFile" Content="Select File" Margin="53,69,0,0" 
                    Width="120" HorizontalAlignment="Left" VerticalAlignment="Top" />
            <TextBox Name="tbFilePath" Text="" Margin="200,71,0,0" Width="169" 
                     HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Button Name="btScanBarcode" Content="Scan QR Code" Margin="98,420,0,0" 
                    Width="162" HorizontalAlignment="Left" VerticalAlignment="Top"
                    Height="24" />
            <TextBox Name="tbScanResult" Margin="393,103,0,0" Width="291" 
                     Height="299" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Image x: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.

using BarcodeLib.BarcodeReader;
using Microsoft.Win32;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Media.Imaging;

namespace BarcodeLib.QRCodeReader.WPF.Demo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.tbFilePath.IsReadOnly = true;
            this.btScanBarcode.IsEnabled = false;
            this.tbScanResult.IsReadOnly = true;
        }

        private void btSelectFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == true)
            {
                try
                {
                    this.tbFilePath.Text = openFileDialog.FileName;
                    this.btScanBarcode.IsEnabled = true;

                    using (var stream = new FileStream(this.tbFilePath.Text, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        BitmapImage bitmap = new BitmapImage();
                        bitmap.BeginInit();
                        bitmap.StreamSource = stream;
                        bitmap.CacheOption = BitmapCacheOption.OnLoad;
                        bitmap.EndInit();
                        this.imgPreview.Source = bitmap;
                    }
                }
                catch (Exception) { }
            }
        }

        private void btScanImage_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                String filePath = this.tbFilePath.Text;
                if (String.IsNullOrEmpty(filePath))
                    throw new Exception("File path is null or empty.");
                if (!File.Exists(filePath))
                    throw new Exception("File does not exist.");


                OptimizeSetting options = new OptimizeSetting(
                    BarcodeReader.BarcodeReader.QRCODE);
                BarCode[] datas = BarcodeReader.BarcodeReader.ReadBarcode(filePath, options);

                StringBuilder sb = new StringBuilder();
                if (datas.Length > 0)
                {
                    sb.Append("Number of QR Code(s): " + datas.Length + "\r\n");
                    foreach (BarCode barcodeInfo in datas)
                        sb.Append("QR Code Text: '" + barcodeInfo.Data + "'\r\n");
                }
                else
                {
                    sb.Append("No QR Code Scanned.");
                }

                this.tbScanResult.Text = sb.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}




5. Add event handlers to WPF controls

Control Button: btSelectFile
<Button Name="btSelectFile" Content="Select File" Margin="53,69,0,0" 
        Width="120" HorizontalAlignment="Left" VerticalAlignment="Top"
        Click="btSelectFile_Click" />


Control Button: btScanBarcode
<Button Name="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.

Related Barcode Library Tutorials