In this article I will explain with an example, how to generate QR Code in Windows Forms (WinForms) Application using C# and VB.Net.
This article will make use of QRCoder library for generating QR code image from database using C# and VB.Net.
Download QR Code package
You will need to install the QRCoder package using the following command.
Install-Package QRCoder -Version 1.4.3
For more details and documentation on QRCoder library, please refer following link.
Database
This article makes use of table named Links whose schema is defined as follows.
I have inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Form Design
The following Form consists of a DataGridView.
Namespaces
You will need to import the following namespaces.
C#
using QRCoder;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;
VB.Net
Imports QRCoder
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Drawing
Imports System.Drawing.Imaging
Generating and displaying QR code image in Windows Forms
Inside the Form Load event handler, the DataGridView is populated from database.
The AutoGenerateColumns property of the DataGridView is set to False.
The first two columns are a normal Text column, while the third column is a DataGridViewImage column which is specially meant for displaying QR Code image.
Hence, a new column named Data is created and using For Loop, each Link is read and converted into a Byte Array using the GenerateQRCode method and is copied to the Data column.
Inside the GenerateQRCode method, the value of the QR Code is captured as parameter and passed to the CreateQrCode method of the QRCoder library which returns a Bitmap image.
The Bitmap image is then saved as PNG image in MemoryStream which later is converted to Byte Array.
Finally, the DataTable is assigned to the DataSource property of the DataGridView.
C#
private void Form1_Load(object sender, EventArgs e)
{
dgvQRCode.AllowUserToAddRows = false;
dgvQRCode.AutoGenerateColumns = false;
dgvQRCode.ColumnCount = 2;
dgvQRCode.Columns[0].Name = "LinkId";
dgvQRCode.Columns[0].HeaderText = "Link Id";
dgvQRCode.Columns[0].DataPropertyName = "LinkId";
dgvQRCode.Columns[1].Name = "Link";
dgvQRCode.Columns[1].HeaderText = "Link";
dgvQRCode.Columns[1].DataPropertyName = "Link";
DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
imageColumn.Name = "QRCode";
imageColumn.DataPropertyName = "Data";
imageColumn.HeaderText = "QRCode";
dgvQRCode.Columns.Insert(2, imageColumn);
dgvQRCode.RowTemplate.Height = 100;
dgvQRCode.Columns[2].Width = 100;
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT LinkId, Link FROM Links", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
//Add a new Byte[] Column.
dt.Columns.Add("Data", Type.GetType("System.Byte[]"));
//Convert text to QRCode Image and then Byte Array and copy to DataTable.
foreach (DataRow row in dt.Rows)
{
row["Data"] = GenerateQRCode(row["Link"].ToString());
}
dgvQRCode.DataSource = dt;
}
}
}
}
}
protected byte[] GenerateQRCode(string code)
{
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
using (Bitmap bitMap = qrCode.GetGraphic(3))
{
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, ImageFormat.Png);
return ms.ToArray();
}
}
}
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dgvQRCode.AllowUserToAddRows = False
dgvQRCode.AutoGenerateColumns = False
dgvQRCode.ColumnCount = 2
dgvQRCode.Columns(0).Name = "LinkId"
dgvQRCode.Columns(0).HeaderText = "Link Id"
dgvQRCode.Columns(0).DataPropertyName = "LinkId"
dgvQRCode.Columns(1).Name = "Link"
dgvQRCode.Columns(1).HeaderText = "Link"
dgvQRCode.Columns(1).DataPropertyName = "Link"
Dim imageColumn As DataGridViewImageColumn = New DataGridViewImageColumn()
imageColumn.Name = "QRCode"
imageColumn.DataPropertyName = "Data"
imageColumn.HeaderText = "QRCode"
dgvQRCode.Columns.Insert(1, imageColumn)
dgvQRCode.RowTemplate.Height = 100
dgvQRCode.Columns(2).Width = 100
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("SELECT LinkId, Link FROM Links", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
'Add a new Byte[] Column.
dt.Columns.Add("Data", Type.GetType("System.Byte[]"))
'Convert text to QRCode Image and then Byte Array and copy to DataTable.
For Each row As DataRowIn dt.Rows
row("Data") = GenerateQRCode(row("Link").ToString())
Next
dgvQRCode.DataSource = dt
End Using
End Using
End Using
End Using
End Sub
Protected Function GenerateQRCode(ByVal code As String) As Byte()
Dim qrGenerator As QRCodeGenerator = New QRCodeGenerator()
Dim qrCodeData As QRCodeData = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q)
Dim qrCode As QRCode = New QRCode(qrCodeData)
Using bitMap As Bitmap = qrCode.GetGraphic(3)
Using ms As MemoryStream = New MemoryStream()
bitMap.Save(ms, ImageFormat.Png)
Return ms.ToArray()
End Using
End Using
End Function
Screenshot
Downloads