How to Generate QR Code in C# Windows Applications

In this tutorial, we will take an in-depth look at how to create QR codes, which are becoming increasingly popular in industrial applications and the retail sector. We will be using IronBarcode, one of the most popular and powerful libraries, to generate QR codes.

What are QR Codes?

QR codes are two-dimensional barcodes that can store text or other digital data. These codes are typically square and black, but they come in many shapes and sizes, varying from industrial to military applications. QR code is one of the most widely used formats of these types of codes.

QR code is an abbreviation for "Quick Response Code", which acts as a universal product code for transporting textual information using a machine-readable medium that can encode both visual and non-visual data. As the popularity of QR codes has increased, it has become more common for people to share their contact details by scanning a QR code with their mobile phone camera. We have seen QR codes used to promote events, contact details, and even business cards.

How to Generate QR Code in C# Windows Form Applications

  1. Create a Windows Forms Application in Microsoft Visual Studio
  2. Installing the QR code library
  3. Importing namespaces to create barcodes
  4. Creating a QR code with one line of code
  5. Adding a logo to a QR code image
  6. Saving an image as PDF or HTML

1. Create a Windows Forms Application in Microsoft Visual Studio

Open Visual Studio > Click on Create New Project > Select Windows Forms Application Template > Press Next > Name the Project > Press Next > Select your Target .NET Framework > Click the Create Button.

After creating the project, design the form as follows from the Visual Studio toolbox: PictureBox, Label, Textbox, and Button controls.

2. Install the QR Code Generator .NET Library in C#

Before we begin, we need to install the barcode library. You can install this by using one of the following three methods:

2.1. Package Manager Console

Write the following command in the Package Manager console. It will download and install the package for you.

Install-Package BarCode

3.2. NuGet Packages Manager Solution

You can also install the barcode library by using the NuGet Package Solution. Simply follow these steps:

Click on Tools > NuGet Package Manager > Manage NuGet package Solution.

This will open the NuGet Package Manager for you. Click on Browse and search BarCode, then install the class library.

As an alternative, the IronBarCode.Dll can be downloaded and added to your project as a reference from [.NET Barcode DLL].

3. Importing NameSpaces

For this tutorial, we need to make sure our class files reference IronBarCode, and also some normal system assemblies in our project.

using IronBarCode;
using System;
using System.Drawing;
using System.Linq;
using IronBarCode;
using System;
using System.Drawing;
using System.Linq;
Imports IronBarCode
Imports System
Imports System.Drawing
Imports System.Linq
VB   C#

4. Create a QR Code with 1 Line of Code

The following sample code helps us to generate a QR code image with just one line of code. Type the desired text in the text box for which you want to generate a QR code. Place this code in the Generate PNG button click. The QR code barcode images can be saved in PNG format.

// Simple QR Code generation
private void button1_Click(object sender, EventArgs e)
{
GeneratedBarcode Qrcode = QRCodeWriter.CreateQrCode(textBox1.Text);
Qrcode.SaveAsPng("QrCode.png");
}
// Simple QR Code generation
private void button1_Click(object sender, EventArgs e)
{
GeneratedBarcode Qrcode = QRCodeWriter.CreateQrCode(textBox1.Text);
Qrcode.SaveAsPng("QrCode.png");
}
' Simple QR Code generation
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim Qrcode As GeneratedBarcode = QRCodeWriter.CreateQrCode(textBox1.Text)
Qrcode.SaveAsPng("QrCode.png")
End Sub
VB   C#

Here is the output of the QR code generator:

QR code of: https://ironsoftware.com/csharp/barcode/docs/

5. Adding a Logo to a QR Code Image

By using the QRCodeWriter.CreateQRCodeWithLogo method, we can add a logo to our QR code generator. In the sample code, you can see how easy this is.

Browse the logo from your computer, and it will open in PictureBox. The code goes as follows:

// open file dialog
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg; *.png; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK) {
// display image in picture box
pictureBox1.Image = new Bitmap(open.FileName);
// store image file path in class data member. Initialize it as string ImageFileName;
ImageFileName = open.FileName;
}
// open file dialog
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg; *.png; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK) {
// display image in picture box
pictureBox1.Image = new Bitmap(open.FileName);
// store image file path in class data member. Initialize it as string ImageFileName;
ImageFileName = open.FileName;
}
' open file dialog
Dim open As New OpenFileDialog()
' image filters
open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg; *.png; *.jpeg; *.gif; *.bmp"
If open.ShowDialog() = DialogResult.OK Then
' display image in picture box
pictureBox1.Image = New Bitmap(open.FileName)
' store image file path in class data member. Initialize it as string ImageFileName;
ImageFileName = open.FileName
End If
VB   C#

Next, simply type the text in the text box, place this code in the Generate PNG button, and click.

// Adding a Logo
GeneratedBarcode Qrcode = QRCodeWriter.CreateQrCodeWithLogo(textBox1.Text, ImageFileName,500);
Qrcode.SaveAsPng("QrCodeWithImage.png");
// Adding a Logo
GeneratedBarcode Qrcode = QRCodeWriter.CreateQrCodeWithLogo(textBox1.Text, ImageFileName,500);
Qrcode.SaveAsPng("QrCodeWithImage.png");
' Adding a Logo
Dim Qrcode As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo(textBox1.Text, ImageFileName,500)
Qrcode.SaveAsPng("QrCodeWithImage.png")
VB   C#

This code adds the Iron logo to the barcode. It automatically sizes it to an appropriate size where the pure code is still readable and aligns that logo to the QR code square grid so that it looks appropriate.

C# Create QR Code With Logo Image

6. Save as a PDF or HTML Image

Finally, we can also save that QR as a PDF or HTML image. The final line of code opens the PDF in your default PDF browser for your convenience. Add the SaveAsPdf in the Generate PDF button and SaveAsHtmlFile in the Generate HTML button.

// Adding a Logo
GeneratedBarcode Qrcode = QRCodeWriter.CreateQrCodeWithLogo(textBox1.Text, ImageFileName,500);

//Save as PDF
Qrcode.SaveAsPdf("QRWithLogo.pdf");

//Also Save as HTML
Qrcode.SaveAsHtmlFile("QRWithLogo.html");
// Adding a Logo
GeneratedBarcode Qrcode = QRCodeWriter.CreateQrCodeWithLogo(textBox1.Text, ImageFileName,500);

//Save as PDF
Qrcode.SaveAsPdf("QRWithLogo.pdf");

//Also Save as HTML
Qrcode.SaveAsHtmlFile("QRWithLogo.html");
' Adding a Logo
Dim Qrcode As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo(textBox1.Text, ImageFileName,500)

'Save as PDF
Qrcode.SaveAsPdf("QRWithLogo.pdf")

'Also Save as HTML
Qrcode.SaveAsHtmlFile("QRWithLogo.html")
VB   C#

Summary

IronBarcode features a friendly API for developers to read and write data to barcodes and QR codes for C# .NET, optimizing accuracy and ensuring a low error rate in real-world-use cases. For more information about IronBarcode, please visit this link.

Currently, if you buy the complete Iron Suite, you can get five libraries for the price of just two. For more details, please click here.

You can download the software product from this link.