Prerequisites
Download and install the following software on your computer
- Visual Studio 2022
- .NET 6.0 SDK
- OnBarcode.Barcode.BarcodeScanner.dll
Create a WPF Core Windows app with barcode reader using C#
1. Create new WPF Core app
Start Visual Studio 2022 and select "Create a new project".
Select "WPF Application" in the dialog, and then press "Next" button.
Create a new web project with name "OnBarcodeReaderWPFDotNetCoreDemo".
Choose ".NET 6.0 (Long-term support)"
Now, all auto-generated files for WPF Core barcode reader demo project could be found in the solution explorer.
Select "WPF Application" in the dialog, and then press "Next" button.
Create a new web project with name "OnBarcodeReaderWPFDotNetCoreDemo".
Choose ".NET 6.0 (Long-term support)"
Now, all auto-generated files for WPF Core barcode reader demo project could be found in the solution explorer.
2. Install Barcode Reader Library dll and NuGet Package
Add OnBarcode Barcode Reader library for WPF Core DLL reference "OnBarcode.Barcode.BarcodeScanner.dll".
Right-click "Dependencies" in the Solution Explorer, and select "Manage NuGet Packages".
Select "Browse" and use the search control to find "System.Drawing.Common" from the package source "nugget.org".
Choose the version 6.0.0 or later to install the package.
Check "Packages" in the Solution Explorer to ensure the installation is success.
Right-click "Dependencies" in the Solution Explorer, and select "Manage NuGet Packages".
Select "Browse" and use the search control to find "System.Drawing.Common" from the package source "nugget.org".
Choose the version 6.0.0 or later to install the package.
Check "Packages" in the Solution Explorer to ensure the installation is success.
3. Design the user interface in WPF project
Select MainWindow.xaml in the Solution Explorer to open the WPF Designer.
Step 1: adjust attributes of the Window tag as below.
Step 1: adjust attributes of the Window tag as below.
<Window x:Class="OnBarcodeReaderWPFDotNetCoreDemo.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:OnBarcodeReaderWPFDotNetCoreDemo" mc:Ignorable="d" Title="MainWindow" Height="480" Width="720"> <Grid> </Grid> </Window>
Step 2: add all controls into the <Grid> tag.
List of controls to add (total 5 controls)
List of controls to add (total 5 controls)
- A Button with a TextBox for choosing a source image file
- A Label with a ComboBox for barcode type
- A Button for Scan Image
- A TextBox for showing the scan result
<Window x:Class="OnBarcodeReaderWPFDotNetCoreDemo.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:OnBarcodeReaderWPFDotNetCoreDemo" mc:Ignorable="d" Title="MainWindow" Height="480" Width="720"> <Grid> <!-- For choosing source image file --> <Button Name="btChooseFile" Content="Button" 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"/> <!-- For selecting barcode type --> <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"/> <!-- For reading barcode in the file --> <Button Name="btScanImage" Content="Scan Image" 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 behind code
Click "MainWindow.xaml.cs" to open the C# file.
Replace all contents in the MainWindow.xaml.cs file by following codes.
Replace all contents in the MainWindow.xaml.cs file by following codes.
using System; using System.Text; using System.IO; using System.Windows; using Microsoft.Win32; using OnBarcode.Barcode.BarcodeScanner; namespace OnBarcodeReaderWPFDotNetCoreDemo { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // Initial Controls this.tbFilePath.IsReadOnly = true; String[] types = new String[] { "AustraliaPost", "Codabar", "Code39", "Code39Extension", "Code93", "Code128", "DataMatrix", "EAN8", "EAN13", "Identcode", "IntelligentMail", "Interleaved2of5", "ISBN", "ISSN", "ITF14", "Leitcode", "PatchCode", "PDF417", "Planet", "Postnet", "QRCode", "RM4SCC", "UPCA", "UPCE" }; this.cbBarcodeType.Items.Clear(); foreach (String type in types) this.cbBarcodeType.Items.Add(type); // Default Barcode Type: Code 128 this.cbBarcodeType.SelectedIndex = 5; this.btScanImage.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.btScanImage.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."); // Get enum BarcodeType from ComboBox selected index. BarcodeType barcodeType = (BarcodeType)this.cbBarcodeType.SelectedIndex; String[] result = BarcodeScanner.Scan(filePath, barcodeType); // Show scan result in the scan result TextBox. StringBuilder sb = new StringBuilder(); if (result.Length > 0) { sb.Append("Number of Barcode: " + result.Length + "\r\n"); foreach (String s in result) sb.Append("Message: '" + s + "'\r\n"); } else { sb.Append("No Barcode Found."); } this.tbScanResult.Text = sb.ToString(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } } }
5. Add event handler to Button controls
Button: btChooseFile
<Button Name="btChooseFile" Content="Button" Margin="25,35,0,0" Width="120" HorizontalAlignment="Left" VerticalAlignment="Top" Click="btChooseFile_Click"/>
TextBox: btScanImage
<Button Name="btScanImage" Content="Scan Image" Margin="280,87,0,0" Width="150" HorizontalAlignment="Left" VerticalAlignment="Top" Click="btScanImage_Click"/>
6. It is done. Now press "Ctrl+F5" to run the project.
Screenshot for the Demo application.
Use "Choose File" button to browse the source image file and then press "Scan Image" button to read the barcode in the file. The scan result is shown in the textbox.
Conclusion
Now we have successfully created a new WPF Windows app with barcode scanning feature included. OnBarcode also provides detailed step-by-step tutorials to read,
recognize barcodes in other .NET project templates:
- How to read barcodes in ASP.NET Core web app using C#?
- How to read barcodes in ASP.NET Core MVC web app using C#?
- How to read barcodes in ASP.NET framework web application using C#?
- How to read barcodes in Windows Forms (Core) using C#?
Common Asked Questions
How can I set up a C# WPF project to read barcodes?
To create a WPF desktop application with barcode reading function enabled using C#, open Visual Studio, create a WPF project,
install C# Barcode Reader library from NuGet Package Manager, use the following C# codes
BarcodeScanner.Scan()
to scan, detect barcodes in C# WPF application.
What is the recommended method to download, install a C# barcode library in a WPF project?
You can download the C# Barcode Reader library from OnBarcode website, add the downloaded dll to your WPF project reference.
Or you can download and install the library from nuget.org using Visual Studio NuGet Package Manager.
Is it possible to detect barcodes from image file or image object in memory in WPF app?
Yes. OnBarcode C# Barcode Reader library supports both decoding barcodes from image files or image objects in memory in WPF project.
Is it possible to read barcodes from poor quality images in WPF project?
Yes. C# Barcode Scanner SDK includes anti-noise algorithms to improve the barcode recognition rate on low quality images in WPF application.
Can C# Barcode Reader library handle rotated barcodes effectively?
Yes. The C# Barcode Reader library supports rotated barcode images reading. And the library supports customization with different direction scanning in WPF app.
What barcode types are supported by the C# Barcode Reader library?
The Barcode Scanner C# library supports the most common 2d and 1d barcode formats, including
QR Code, Data Matrix, Code 128, EAN/UPC. The scanner library supports both ISO standard and GS1 standard barcode formats, such
as GS1 QR Code, QR Code (ISO standard).
Is it possible to further optimize the barcode reading speed in WPF application?
OnBarcode C# Barcode Reader library provides various scanning options to improve the barcode reading performance in C# WPF project,
including specified image areas scanning, selected scanning directions, multi-threading.
Is it easy to enable barcode scanning function in a .NET WPF Windows application?
Yes. It is really easy to enable barcode scanning function in C# WPF Windows application. Create a WPF project, add barcode library to the project reference.
Enable barcode scanning function with one line of C# code
BarcodeScanner.Scan().
Is it possible to read multiple barcodes with different format in a WPF application?
Yes. Using C# Barcode Scanner library, you can scan and read multiple barcodes with different barcode formats from a single image file in
C# WPF application.
How does C# Barcode library verify data integrity during barcode decoding?
The barcode scanner component will apply the barcode check digit or the error correction level (for 2d barcodes) to verify the scanned
barcode data in C# WPF Windows application.
How to get a trial license of C# Barcode Reader library?
You can download the free trial package from OnBarcode website or install the library from nuget.org through Visual Studio NuGet Package Manager.