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#?
