In this article I will explain with an example, how to dynamically generate and display QR code Image in Windows Forms (WinForms) Application using C# and VB.Net.
In this article, I will make use of QRCoder which is an Open Source Library QR code generator.
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.
Form Design
The following Form consists of a TextBox, a Button and a PictureBox control.
Dynamically generate and display QR code Image using C# and VB.Net
You will need to import the following namespaces.
using QRCoder;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
Imports QRCoder
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Generating and displaying QR code image
The following event handler is executed, when the Generate Button is clicked.
Inside the event handler, the Text from the TextBox is passed to the CreateQRCode method of the QRCoder library which returns a Bitmap image.
Then, Bitmap is saved in the MemoryStream object and then, converted to Image using the FormStream function of the Image class.
Finally, the Image is displayed in PictureBox control.
private void OnGenerate(object sender, EventArgs e)
    string code = txtCode.Text;
    QRCodeGenerator qrGenerator = new QRCodeGenerator();
    QRCodeData qrCodeData = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
    QRCode qrCode = new QRCode(qrCodeData);
    using (Bitmap bitMap = qrCode.GetGraphic(4))
        using (MemoryStream ms = new MemoryStream())
            bitMap.Save(ms, ImageFormat.Png);
            pictureBox1.Height = 150;
            pictureBox1.Width = 150;
            pictureBox1.Image = Image.FromStream(ms);
PrivateSub OnGenerate(ByVal sender As Object, ByVal e As EventArgs) Handles btnGenerate.Click
    Dim code As String = txtCode.Text
    Dim qrGenerator As QRCodeGenerator = New QRCodeGenerator()
    Dim qrCodeData As QRCodeData = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q)
    Dim qrCode = New QRCode(qrCodeData)
    Using bitMap As Bitmap = qrCode.GetGraphic(4)
       Using ms As MemoryStream = New MemoryStream()
            bitMap.Save(ms, ImageFormat.Png)
            pictureBox1.Height = 150
            pictureBox1.Width = 150
            pictureBox1.Image = Image.FromStream(ms)
        End Using
    End Using
End Sub
Dynamically generate and display QR code Image using C# and VB.Net
Dynamically generate and display QR code Image using C# and VB.Net