C# Barcode Reader Library in WPF Core Windows app

How to scan, read barcode images in WPF Core Windows application with free C# barcode example source code

Using Free C# Souce Code to read barcode images in .NET WPF Core Windows Projects



This C# tutorial provides a step by step guide for building a WPF (.NET Core) Windows app, where user can upload a image with barcode and scan, read the barcode data message from it.

Windows Presentation Foundation (WPF) is a desktop UI framework which is resolution-independent and uses a vector-based rendering engine, built to take advantage of modern graphics hardware. WPF application is for Microsoft Windows only.

After this tutorial, you will be albe to build a WPF desktop app to
  • Read, recognize 20+ barcodes data text from image files, PDF, Word, office documents in WPF (.NET 5+) desktop application
  • Support System.Drawing.Common for Windows
  • Support .NET 9, 8, 7, 6, 5 and .NET Core 3.1, 2.1 WPF Windows application

How to read linear, 2d barcodes in WPF Core Windows app using C#

  1. Download C#.NET Barcode Reader Library
  2. Install C# library to scan barcode images in WPF Core app
  3. Step by Step Tutorial












Prerequisites

Top
Download and install the following software on your computer
  1. Visual Studio 2022
  2. .NET 6.0 SDK
  3. OnBarcode.Barcode.BarcodeScanner.dll

Create a WPF Core Windows app with barcode reader using C#

Top

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.





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.





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.
<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)
  • 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.
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

Top

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: After you have integrated the C# Barcode Reader library into your .NET project, you can further customize the barcode reading performance and handle the complex barcode data message. View details here: How to read barcodes using C#?
















OnBarcode is a market-leading provider of barcode imaging generator, reader controls and components for ASP.NET, Windows Forms, WPF, as well Java, Android, iOS (iPhone, iPad) across all major enterprise development platforms. We provides comprehensive tutorials and how-tos for various linear, 2d barcode information, such as C# in ASP.NET, C# .NET, C# Barcode Encoding, C# Barcode Image, VB.NET in ASP.NET, VB.NET Winforms, VB.NET Barcode Encoding. OnBarcode barcode products are supported by RasterEdge ASP.NET Document Viewer, which supports ASP.NET PDF Viewer, ASP.NET MVC Document Viewer, ASP.NET PDF Editor, MVC PDF Viewer. And provide high quality C# Convert PDF to Tiff, C# Convert PDF to Word, C# Convert PDF to HTML, C# Convert PDF to Jpeg images, and their easy and simple documents, like C# PDF SDK, C# extract text from PDF, C# Compress PDF, Print PDF in C# and C# extract image from PDF.
Terms of Use | Privacy Policy
Copyright © OnBarcode.com . All rights reserved.