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

How to scan, read barcode image in C# WPF Windows application?

Detailed tutorial for reading and scanning barcode image in C# Windows Presentation Foundation (WPF) 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 barcode 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 barcodes from image files on the WPF Window Application.

After finish the tutorial, you can also further customize the C# to add more barcode scanning options, such as multi-thread scanning.





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.BarcodeReader.WPFDemo for Project name. It is important to name the proejct BarcodeLib.BarcodeReader.WPFDemo, 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 barcode 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 barcode 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.BarcodeReader.WPFDemo.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.BarcodeReader.WPFDemo"
            mc:Ignorable="d"
            Title="BarcodeLib C# Barcode Reader WPF Windows App Demo" Height="480" Width="720">
        <Grid>
    
        </Grid>
    </Window>
    
  2. Add all controls into the <Grid> tag.
    • A Button with a TextBox for choosing a source image file
    • A Label with a ComboBox for barcode type selecting
    • A Button to start barcode scanning
    • A TextBox for displaying the barcode scan result
    <Window x:Class="BarcodeLib.BarcodeReader.WPFDemo.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.BarcodeReader.WPFDemo"
            mc:Ignorable="d"
            Title="BarcodeLib C# Barcode Reader WPF Windows App Demo" Height="480" Width="720">
        <Grid>
            <Button Name="btSelectFile" Content="File" Margin="25,35,0,0" Width="120" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <TextBox Name="tbFilePath" Text="" Margin="155,36,0,0" Width="300" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    
            <Label Content="Barode Type:" Margin="470,31,0,0"  HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <ComboBox Name="cbBarcodeType" Margin="560,33,0,0" Width="120" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    
            <Button Name="btScanBarcode" Content="Scan Barcode" Margin="280,87,0,0" Width="150" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <TextBox Name="tbScanResult" Margin="25,140,0,0" Width="668" Height="299" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        </Grid>
    </Window>
    





4. Add C# code to enable barcode 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 Microsoft.Win32;
using System.IO;
using System.Text;
using System.Windows;

namespace BarcodeLib.BarcodeReader.WPFDemo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.tbFilePath.IsReadOnly = true;
            String[] types = new String[] {
                "Code 128", "Code 39", "Code 39 extension", "Interleaved 2 of 5", "EAN-13", 
                "EAN-8", "UPC-A", "UPC-E", "QR Code", "PDF-417", "Data Matrix"
            };
            this.cbBarcodeType.Items.Clear();
            foreach (String type in types)
                this.cbBarcodeType.Items.Add(type);

            this.cbBarcodeType.SelectedIndex = 0;
            this.btScanBarcode.IsEnabled = false;
            this.tbScanResult.IsReadOnly = true;
        }

        private void btChooseFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == true)
            {
                try
                {
                    this.tbFilePath.Text = openFileDialog.FileName;
                    this.btScanBarcode.IsEnabled = true;
                }
                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.");

                int barcodeType = this.cbBarcodeType.SelectedIndex + 1;
                OptimizeSetting options = new OptimizeSetting(barcodeType);
                BarCode[] datas = BarcodeReader.ReadBarcode(filePath, options);

                StringBuilder sb = new StringBuilder();
                if (datas.Length > 0)
                {
                    sb.Append("Number of Barcode(s): " + datas.Length + "\r\n");
                    foreach (BarCode barcodeInfo in datas)
                        sb.Append("Barcode Text: '" + barcodeInfo.Data + "'\r\n");
                }
                else
                {
                    sb.Append("No Barcode 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="File" Margin="25,35,0,0" 
        Width="120" HorizontalAlignment="Left" VerticalAlignment="Top"
        Click="btSelectFile_Click"/>


Control Button: btScanBarcode
<Button Name="btScanBarcode" Content="Scan Barcode" Margin="280,87,0,0" 
        Width="150" HorizontalAlignment="Left" VerticalAlignment="Top"
        Click="btScanBarcode_Click"/>





5. Run the demo WPF Barcode Scanner Windows app

Now we have completed a simpel WPF barcode scanner application. Run the WPF app directly from Visual Studio .NET.

You can select a barcode image from your local computer and scan to view the barcode text.

Related Barcode Library Tutorials