Generate QR Code in WPF

Share this

In this article, you will learn how to generate QR Code in WPF application. vCard 3.0 format ( can be saved in your cell phone’s address book by scanning the QR Code.

Source Code : Complete source code for the article at GitHub repo (along with sample excel sheet)

Use Case : A fictitious company’s HR department has an excel sheet with the employee data. HR department needs to upload this excel sheet in a desktop application. A button should be available for each employee data row which can be clicked to save the employee’s v3 format QR code.

Key Learnings :

(1) Create WPF DataGrid with paging & searching & ComboBox to change number of records displayed per page

(2) Generate v3 format QR Code

Technologies Used :

(1) WPF

(2) Open XML – for reading excel file and aggregating data in DataTable object

Third Party Libraries Used : ZXing.Net (v0.15.0) – can be installed from NuGet

Solution Design : Click the upload excel button. This will trigger an OpenFileDialog where you can browse and select the employee excel sheet.The excel sheet structure with sample data looks as below.


The application’s home screen after uploading the employee excel sheet with sample data looks as below:


For a employee row, click the “QR Code” button to generate and save the QR Code. For demonstration, I clicked the fifth row with first name “Sudipta4” and last name “Chaudhari4”. This will open a SaveFileDialog to save the QRCode image in format i.e. Sudipta4Chaudhari4.png. The saved QR Code image looks as below.


On scanning the QR Code using my cell phone with a bar code scanner app looks as shown below:


Pressing Add contact opens phone’s contact manager app which populates the QR Code encode data in the contact fields as shown below:


Finally, after saving the contact, it look as below:


Code :

Step 1 : Create a WPF application. In the MainWindow.xaml, add the below code to layout the screen field elements using Grid and Stack Panel

MainWindow.xaml.cs has all the code for:

  • OpenFileDialog – to upload excel sheet
  • Importing the excel sheet data using OpenXML to a DataTable object
  • Creating a WPF grid
  • Adding paging and searching functionality
  • Adding ComboBox to change the number of records displayed per page of the DataGrid
  • Adding template field to grid for button to generate QR code for every row of DataGrid
  • Generating the QR Code using ZXing.Net library
  • Saving the QR Code image using a SaveFileDialog by converting QR Code image to a Bitmap image and saving it to MemoryStream object for downloading
  • Clear Sort button clears the sorting if applied on the DataGrid by clicking the column headers to revert back to the original state where the data was uploaded from the excel sheet
  • UploadExcel button also checks if excel sheet is not in correct format (field names as set and shown in the excel sheet screenshot above in the article should be the same), warns the user using a MessageBox message display

I hope you followed the article. If you have any comments, questions or suggestions, leave a message and I will try to respond at my earliest.

Share this

Leave a Reply