Windows Forms: How to Encode and Decode QR Code in C#

By FoxLearn 5/23/2017 10:03:43 PM   12.95K
How to Encode and Decode QR Code using MessagingToolkit.QRCode in C#

Step 1Click 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

encrypt decrypt qrcode in c#Step 2: Right click on your project select Manage NuGet Packages -> Search qrcode -> Install

install messagingtoolkit qrcodeStep 3: Design your form as below

encrypt decrypt qrcode in c#

Step 4: Add code to handle your form

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

    private void btnEncode_Click(object sender, EventArgs e)
    {
        using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "JPEG|*.jpg", ValidateNames = true })
        {
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                //Encode qrcode, then save image
                MessagingToolkit.QRCode.Codec.QRCodeEncoder encoder = new MessagingToolkit.QRCode.Codec.QRCodeEncoder();
                encoder.QRCodeScale = 8;
                Bitmap bmp = encoder.Encode(txtEncode.Text);
                pictureBox.Image = bmp;
                bmp.Save(sfd.FileName, ImageFormat.Jpeg);
            }
        }
    }

    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)
            {
                //Decode qrcode from image
                pictureBox.Image = Image.FromFile(ofd.FileName);
                MessagingToolkit.QRCode.Codec.QRCodeDecoder decoder = new MessagingToolkit.QRCode.Codec.QRCodeDecoder();
                txtDecode.Text = decoder.Decode(new QRCodeBitmapImage(pictureBox.Image as Bitmap));
            }
        }
    }

VIDEO TUTORIALS