
A barcode contains information about a product or a company, visually represented in a machine-readable form. Barcodes are widely used to track shipments and inventory management. We can easily generate various types of barcodes in WPF applications. In this article, we will learn how to generate and display a barcode image in the WPF application. After following the mentioned steps, we will have our own WPF Barcode Generator in C#. So let’s begin.
The article shall cover the following topics:
- Features of WPF Barcode Generator
- C# Barcode Generator API
- Steps to Create WPF Barcode Generator
- Generate Barcode with Additional Options
- Demo WPF Barcode Generator
- Download Source Code
Features of WPF Barcode Generator
Our WPF barcode generator will have the following features.
- Generate the following types of barcode symbologies:
- Code128
- Code11
- Code39
- QR
- Datamatrix
- EAN13
- EAN8
- ITF14
- PDF417
- Save the generated barcode image in the following formats:
- Preview the generated barcode image.
C# Barcode Generator API
We will be using the Aspose.BarCode for .NET API to generate barcode images and preview them in the WPF application. It is a feature-rich API that lets you generate, scan, and read a wide range of barcode types. Moreover, it allows to manipulate the appearance of the generated barcodes such as background color, bar color, rotation angle, x-dimension, image quality, resolution, captions, size and much more.
Steps to Create WPF Barcode Generator
We can generate and display a barcode image in the WPF application by following the steps given below:
- Firstly, create a new project and select the WPF Application project template.

Select the project template.
Next, enter the name of the project e.g. “BarcodeGen”.
Then, select the .NET framework and then choose to create.
Next, open NuGet Package Manager and install Aspose.BarCode for .NET package.

Install Aspose.BarCode for .NET
- Then, add a new class Barcode.cs to define the barcode.
public class Barcode | |
{ | |
public string? Text { get; set; } | |
public BaseEncodeType? BarcodeType { get; set; } | |
public BarCodeImageFormat ImageType { get; set; } | |
} |
- Next, open the MainWindow.xaml and add the required controls as shown below:

Add the required controls
You may also replace the content of MainWindow.xaml with the following script.
<Window x:Class="BarcodeGen.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:BarcodeGen" | |
mc:Ignorable="d" | |
Title="MainWindow" Height="450" Width="800"> | |
<Grid Width="800" Height="384"> | |
<Grid.RowDefinitions> | |
<RowDefinition Height="191*"/> | |
<RowDefinition/> | |
</Grid.RowDefinitions> | |
<Label Content="Select Barcode Type:" HorizontalAlignment="Left" Margin="10,16,0,0" VerticalAlignment="Top" FontSize="14" FontWeight="Bold"/> | |
<ComboBox x:Name="comboBarcodeType" HorizontalAlignment="Left" Margin="10,47,0,305" Width="273" Text="Select Barcode Type" IsReadOnly="True" SelectedIndex="-1" FontSize="14" Height="30"> | |
<ComboBoxItem Content="Code128"></ComboBoxItem> | |
<ComboBoxItem Content="Code11"></ComboBoxItem> | |
<ComboBoxItem Content="Code32"></ComboBoxItem> | |
<ComboBoxItem Content="QR"></ComboBoxItem> | |
<ComboBoxItem Content="DataMatrix"></ComboBoxItem> | |
<ComboBoxItem Content="EAN13"></ComboBoxItem> | |
<ComboBoxItem Content="EAN8"></ComboBoxItem> | |
<ComboBoxItem Content="ITF14"></ComboBoxItem> | |
<ComboBoxItem Content="PDF417"></ComboBoxItem> | |
</ComboBox> | |
<Button Name="btnGenerate" Click="btnGenerate_Click" Content="Generate Barcode" HorizontalAlignment="Left" Margin="10,346,0,0" VerticalAlignment="Top" Height="28" Width="273" FontSize="14" FontWeight="Bold"/> | |
<Label Content="Enter Your Text:" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" FontSize="14" FontWeight="Bold"/> | |
<TextBox Name="tbCodeText" TextWrapping="Wrap" Margin="10,123,517,134" Width="273" Height="125"/> | |
<Label Content="Select Image Format:" HorizontalAlignment="Left" Margin="10,253,0,0" VerticalAlignment="Top" FontSize="14" FontWeight="Bold"/> | |
<RadioButton Name="rbPng" Content="Png" GroupName="rbImageType" Margin="10,285,739,77" Width="51" Height="20" FontSize="14" IsChecked="True"/> | |
<RadioButton Name="rbJpg" Content="Jpeg" GroupName="rbImageType" Margin="121,285,628,77" Width="51" Height="20" FontSize="14"/> | |
<RadioButton Name="rbBmp" Content="Bmp" GroupName="rbImageType" Margin="232,285,517,77" Width="51" Height="20" FontSize="14"/> | |
<CheckBox Name="cbGenerateWithOptions" Height="20" Margin="10,321,517,41" Content="Generate with Options" /> | |
<GroupBox Header="View Generated Barcode" Margin="317,0,22,0" FontSize="14" FontWeight="Bold"> | |
<Image Name="imgDynamic" Margin="6,-6,7,6" Stretch="None" /> | |
</GroupBox> | |
</Grid> | |
</Window> |
- Then, open the MainWindow.xaml.cs class and add btnGenerate_Click event to handle the click action for Generate Barcode button.
private void btnGenerate_Click(object sender, RoutedEventArgs e) | |
{ | |
// Set default as Png | |
var imageType = "Png"; | |
// Get the user selected image format | |
if(rbPng.IsChecked == true) | |
{ | |
imageType = rbPng.Content.ToString(); | |
} | |
else if(rbBmp.IsChecked == true) | |
{ | |
imageType = rbBmp.Content.ToString(); | |
} | |
else if(rbJpg.IsChecked == true) | |
{ | |
imageType = rbJpg.Content.ToString(); | |
} | |
// Get image format from enum | |
var imageFormat = (BarCodeImageFormat)Enum.Parse(typeof(BarCodeImageFormat), imageType.ToString()); | |
// Set default as Code128 | |
var encodeType = EncodeTypes.Code128; | |
// Get the user selected barcode type | |
if (!string.IsNullOrEmpty(comboBarcodeType.Text)) | |
{ | |
switch (comboBarcodeType.Text) | |
{ | |
case "Code128": | |
encodeType = EncodeTypes.Code128; | |
break; | |
case "ITF14": | |
encodeType = EncodeTypes.ITF14; | |
break; | |
case "EAN13": | |
encodeType = EncodeTypes.EAN13; | |
break; | |
case "Datamatrix": | |
encodeType = EncodeTypes.DataMatrix; | |
break; | |
case "Code32": | |
encodeType = EncodeTypes.Code32; | |
break; | |
case "Code11": | |
encodeType = EncodeTypes.Code11; | |
break; | |
case "PDF417": | |
encodeType = EncodeTypes.Pdf417; | |
break; | |
case "EAN8": | |
encodeType = EncodeTypes.EAN8; | |
break; | |
case "QR": | |
encodeType = EncodeTypes.QR; | |
break; | |
} | |
} | |
// Initalize barcode object | |
Barcode barcode = new Barcode(); | |
barcode.Text = tbCodeText.Text; | |
barcode.BarcodeType = encodeType; | |
barcode.ImageType = imageFormat; | |
try | |
{ | |
string imagePath = ""; | |
if (cbGenerateWithOptions.IsChecked == true) | |
{ | |
// Generate barcode with additional options and get the image path | |
imagePath = GenerateBarcodeWithOptions(barcode); | |
} | |
else | |
{ | |
// Generate barcode and get image path | |
imagePath = GenerateBarcode(barcode); | |
} | |
// Display the image | |
Uri fileUri = new Uri(Path.GetFullPath(imagePath)); | |
imgDynamic.Source = new BitmapImage(fileUri); | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.Message); | |
} | |
} |
- After that, add a function to generate a barcode.
private string GenerateBarcode(Barcode barcode) | |
{ | |
// Image path | |
string imagePath = comboBarcodeType.Text + "." + barcode.ImageType; | |
// Initilize barcode generator | |
BarcodeGenerator generator = new BarcodeGenerator(barcode.BarcodeType, barcode.Text); | |
// Save the image | |
generator.Save(imagePath, barcode.ImageType); | |
return imagePath; | |
} |
- Finally, run the application.
Generate Barcode with Additional Options
We can also generate barcodes with additional options specific to barcode types. In the WPF barcode generator, we have added a checkbox to generate a barcode with options. It will call the following function specifying additional options for different barcode types.
private string GenerateBarcodeWithOptions(Barcode barcode) | |
{ | |
// Image path | |
string imagePath = comboBarcodeType.Text + "." + barcode.ImageType; | |
// Initilize barcode generator | |
BarcodeGenerator generator = new BarcodeGenerator(barcode.BarcodeType, barcode.Text); | |
if(barcode.BarcodeType == EncodeTypes.QR) | |
{ | |
generator.Parameters.Barcode.XDimension.Pixels = 4; | |
//set Auto version | |
generator.Parameters.Barcode.QR.QrVersion = QRVersion.Auto; | |
//Set Auto QR encode type | |
generator.Parameters.Barcode.QR.QrEncodeType = QREncodeType.Auto; | |
} | |
else if(barcode.BarcodeType == EncodeTypes.Pdf417) | |
{ | |
generator.Parameters.Barcode.XDimension.Pixels = 2; | |
generator.Parameters.Barcode.Pdf417.Columns = 3; | |
} | |
else if(barcode.BarcodeType == EncodeTypes.DataMatrix) | |
{ | |
//set DataMatrix ECC to 140 | |
generator.Parameters.Barcode.DataMatrix.DataMatrixEcc = DataMatrixEccType.Ecc200; | |
} | |
else if(barcode.BarcodeType == EncodeTypes.Code32) | |
{ | |
generator.Parameters.Barcode.XDimension.Millimeters = 1f; | |
} | |
else | |
{ | |
generator.Parameters.Barcode.XDimension.Pixels = 2; | |
//set BarHeight 40 | |
generator.Parameters.Barcode.BarHeight.Pixels = 40; | |
} | |
// Save the image | |
generator.Save(imagePath, barcode.ImageType); | |
return imagePath; | |
} |
You may read more about Generation Specifics for Barcode Types in the documentation.
Demo WPF Barcode Generator
The following is the demonstration of the WPF Barcode Generator application that we have just created.

Demo WPF Barcode Generator
Download Source Code
You can download the complete source code of the WPF Barcode Generator application from GitHub.
Get a Free License
You can get a free temporary license to try the library without evaluation limitations.
Conclusion
In this article, we have learned how to generate various types of barcodes in a WPF application. We have also seen how to preview the generated barcode image programmatically. Besides, you can learn more about Aspose.BarCode for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on our forum.