How to Generate QR Code with Logo in C#

By FoxLearn 7/19/2024 2:26:19 AM   21.87K
You can generate a QR code with a logo using the ZXing.Net library in a C# Windows Forms Application by following these steps.

C# QR Code Generator with Logo

To learn how to generate QR Code in c# windows forms application, you can create a simple demo by drag and drop 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.

Or you can install the ZXing.Net library via NuGet Package Manager Console using the following command.

Install-Package ZXing.Net

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.

Add PictureBox control to your Windows Forms Application: This will be used to display the generated QR code.

And Button controls to trigger the generation of the QR code and QR code reading.

To create a simple zxing qr code generator c#, you should add a 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.

// c# generate qr code with logo
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 a 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 this c# example, You've learned how to use zxing.net to create a simple c# qr code generator. You can also use zxing.net to create c# qr code reader. As you know, zxing.net library is a free qr code reader c# open source.

VIDEO TUTORIAL