How to Encode and Decode QR Code in C#

By FoxLearn 11/22/2024 8:34:22 AM   13.05K
To encode and decode QR codes using the MessagingToolkit.QRCode library in a Windows Forms application, you need to follow these steps

QR codes have become an essential tool for storing and sharing information. From links and contact details to payment options and authentication, QR codes are widely used in many applications.

In this article, we'll explore how to create a simple Windows Forms application in C# that encodes and decodes QR codes using the MessagingToolkit.QRCode library.

How to Encode and Decode QR Code in C#?

Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "QRCode" and then click OK

To start using MessagingToolkit.QRCode, you need to install it via NuGet.

Right click on your project select Manage NuGet Packages -> Search qrcode -> Install

install messagingtoolkit qrcode

Drag and drop the PictureBox, Label, TextBox and Button controls from the Visual Toolbox onto your form designer, then design your form as shown below.

encrypt decrypt qrcode in c#

Add code to handle your form

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    // Event handler for encoding the QR code
    private void btnEncode_Click(object sender, EventArgs e)
    {
        using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "JPEG|*.jpg", ValidateNames = true })
        {
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                /// Initialize the QR code encoder
                MessagingToolkit.QRCode.Codec.QRCodeEncoder encoder = new MessagingToolkit.QRCode.Codec.QRCodeEncoder();
                encoder.QRCodeScale = 8;
                // Encode the text from txtEncode TextBox
                Bitmap bmp = encoder.Encode(txtEncode.Text);
                // Display the generated QR code in the PictureBox
                pictureBox.Image = bmp;
                // Save the QR code as a JPEG image to the selected path
                bmp.Save(sfd.FileName, ImageFormat.Jpeg);
            }
        }
    }

    // Event handler for decoding the QR code
    private void btnDecode_Click(object sender, EventArgs e)
    {
        using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "JPEG|*.jpg", ValidateNames = true, Multiselect = false })
        {
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                // Load the selected image into the PictureBox
                pictureBox.Image = Image.FromFile(ofd.FileName);
                // Initialize the QR code decoder
                MessagingToolkit.QRCode.Codec.QRCodeDecoder decoder = new MessagingToolkit.QRCode.Codec.QRCodeDecoder();
                // Decode the QR code from the image and display the result in txtDecode TextBox
                txtDecode.Text = decoder.Decode(new QRCodeBitmapImage(pictureBox.Image as Bitmap));
            }
        }
    }
}

btnEncode_Click Event Handler: This method is triggered when the user clicks the 'Encode' button. It opens a SaveFileDialog, allowing the user to select the location and filename to save the QR code image.

Next, it uses the QRCodeEncoder class from MessagingToolkit.QRCode to encode the text entered in the txtEncode TextBox into a QR code. The QR code image is then displayed in the pictureBox and saved as a JPEG file.

btnDecode_Click Event Handler: This method is triggered when the user clicks the 'Decode' button. It opens an OpenFileDialog for the user to select the image containing a QR code, then loads the selected image into the pictureBox.

Next, it uses the QRCodeDecoder class to decode the QR code from the image and display the decoded text in the txtDecode TextBox.

This simple Windows Forms application demonstrates how to use the MessagingToolkit.QRCode library to encode and decode QR codes in C#.

VIDEO TUTORIAL