Generate Linear And 2-D Barcodes In A WPF Application

Barcodes are widely used in various applications like shipment tracking, inventory management etc. The library I used to integrate barcode generation capability into a WPF application is called DynamicBarcode Creator for .NET.

DynamicBarcode Creator for .NET supports various linear and 2-D barcode types including some of the more popular ones like QR Code, Data Matrix & PDF417. An evaluation edition of this product can be downloaded here. The evaluation is fully functional and there is no time limit and the barcodes generated are randomly watermarked with evaluation text. DynamicBarcode Creator provides drag-and-drop barcode controls for WPF, WinForms and ASP.NET and these controls are added by default to Visual Studio toolbars when the product is installed.

toolbox

There is a separate control for each 2-D barcode type (DataMatrix, Pdf417, QRCode & StackedGS1DataBar) and a single control (LinearBarcode) for all supported linear barcodes types. List of all supported barcode types is available here.

Let’s create a simple WPF application and drag & drop the Linear Barcode control onto the form. In the Properties window of the control we will specify the following values:

a. Select Code128 for “Linear Barcode Type”.
b. Select “Show Text” in order to display the text under the barcode.
c. Set “Symbol Height” to 20, this is the height of the bars drawn in the barcode.
d. Set “XDimension” to 1, this is the width of a single bar drawn in the barcode.

properties

Next, we will add a text box to input the barcode text and a button to refresh the barcode image.

barcode

In the button click event handler we will set the barcode control’s Text property with the text entered in the text box. If we need to set other barcode properties during runtime we can always add additional form controls to accept input from the user and assign those values to the barcode.

  1. private void btnUpdateCode128_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     code128.Text = tbxcode128.Text;  
  4. }  
2-D barcodes can be generated in a similar way. To illustrate the 2-D barcode generation we will drag and drop the QR Code control onto the form and update the QR Code properties as needed.

properties

Then add a textbox and button controls to update the QR code text.

QR code

We will add the following code in the button click event handler to update the QR Code text.
  1. private void btnUpdateQRCode_Click(object sender, RoutedEventArgs e)   
  2. {  
  3.     qrCode.Text = tbxQRCode.Text;  
  4. }  
If we need to update other barcode properties during runtime we can always add additional form controls to accept input from the user and assign those values to the barcode. This blog post illustrates the simple steps to create barcodes in WPF apps. However, DynamicBarcode Creator has a lot of other features that allow you to handle various scenarios. 
 
Read more articles on WPF: