Windows Forms: Generate qr code with logo in C#

This post shows you How to generate QR Code with Logo using ZXing.Net in C# .NET Windows Forms Application.

To learn how to generate QR Code in c# windows application, you can create a simple demo by dragging the PictureBox, TextBox and Button controls from the visual studio toolbox to your winform, then layout your UI as shown below allows you to create QR Code with a custom logo inside and decode QR Code.

generate qr code with logo in c#

To play the demo, you need to install zxing qr code generator c# from the Manage Nuget Packages in your Visual Studio.

As you know, ZXing.Net is an open source qr code generator that helps you create barcodes and qr codes. You can also use zxing.net library to decode barcodes and qr codes.

To create zxing qr code generator c#, you should add the click event hanlder to the Create button that allows you to create QR Code with logo in the middle. You can use the BarcodeWriter class to create the QR Code, then add an image to the middle of the qr code in c# as shown below.

private void btnCreate_Click(object sender, EventArgs e)
{
    BarcodeWriter barcodeWriter = new BarcodeWriter();
    EncodingOptions encodingOptions = new EncodingOptions() { Width = 300, Height = 300, Margin = 0, PureBarcode = false };
    encodingOptions.Hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    barcodeWriter.Renderer = new BitmapRenderer();
    barcodeWriter.Options = encodingOptions;
    barcodeWriter.Format = BarcodeFormat.QR_CODE;
    Bitmap bitmap = barcodeWriter.Write(txtInput.Text);
    Bitmap logo = new Bitmap($"{Application.StartupPath}/foxlearn.png");
    Graphics g = Graphics.FromImage(bitmap);
    g.DrawImage(logo, new Point((bitmap.Width - logo.Width) / 2, (bitmap.Height - logo.Height) / 2));
    pictureBox1.Image = bitmap;
}

You should copy your logo image into your project, then set the Copy to Output Directory property of your file to 'Copy always'. Each time you rebuild your project, the image logo file will be copied into the Debug folder.

Next, Add the click event handler to the Read button allows you to decode QR Code. You can also use the BarcodeReader class to decode QR Code in c# as shown below.

private void btnRead_Click(object sender, EventArgs e)
{
    BarcodeReader barcodeReader = new BarcodeReader();
    var result = barcodeReader.Decode(new Bitmap(pictureBox1.Image));
    if (result != null)
        txtOutput.Text = result.Text;
}

And don't forget to add the namespaces below to your winform.

using ZXing;
using ZXing.Common;
using ZXing.QrCode.Internal;
using ZXing.Rendering;

Press F5 to build your project, then enter the text and select the logo you want to display in the middle qr code. Next, click the Generate button to create qr code with logo image and click the Reader button to decode the QR Code with logo image in c# using ZXing.Net library.

Through the c# example, you've learned how to use zxing.net to write a simple c# qr code generator. You can also use zxing.net to write c# qr code reader, zxing.net is a qr code reader c# open source.

VIDEO TUTORIAL