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
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.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.
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 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
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 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.
Adjust the WPF Window Width and Height.
<Windowx: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>
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
<Windowx: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><ButtonName="btSelectFile"Content="File"Margin="25,35,0,0"Width="120"HorizontalAlignment="Left"VerticalAlignment="Top"/><TextBoxName="tbFilePath"Text=""Margin="155,36,0,0"Width="300"HorizontalAlignment="Left"VerticalAlignment="Top"/><LabelContent="Barode Type:"Margin="470,31,0,0"HorizontalAlignment="Left"VerticalAlignment="Top"/><ComboBoxName="cbBarcodeType"Margin="560,33,0,0"Width="120"HorizontalAlignment="Left"VerticalAlignment="Top"/><ButtonName="btScanBarcode"Content="Scan Barcode"Margin="280,87,0,0"Width="150"HorizontalAlignment="Left"VerticalAlignment="Top"/><TextBoxName="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.