This article shows you how to generate a qr code with logo or image in c# code examples using ZXing .NET.
To play the demo, you can drag a PictureBox and a Button from your visual toolbox into your winform, then layout your UI as simple as below.
You can easily generate a custom QRCode with logo image by using ZXing.Net library.
Next, You need to download and install ZXing.Net from the Manage Nuget Packages.
As you know, ZXing.Net is a port of ZXing. It's an open-source that helps you generate qr code or barcode.
Add code to handle your windows forms as shown below.
using System;
using System.Drawing;
using System.Windows.Forms;
using ZXing;
using ZXing.Common;
using ZXing.QrCode.Internal;
using ZXing.Rendering;
namespace QRApp1
{
public partial class frmQRCoder : Form
{
public frmQRCoder()
{
InitializeComponent();
}
public bool CheckQRCode(Bitmap bmp)
{
BarcodeReader reader = new BarcodeReader();
var result = reader.Decode(bmp);
if (result == null)
return false;
return result.Text == url;
}
public Bitmap GenerateQRCode(int width, int height, string text)
{
var barcodeWriter = new BarcodeWriter();
var encodingOptions = new EncodingOptions { Width = width, Height = height, 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(text);
Bitmap overlay = new Bitmap(Application.StartupPath + "/logo.png");
Graphics g = Graphics.FromImage(bitmap);
g.DrawImage(overlay, new Point((bitmap.Width - overlay.Width) / 2, (bitmap.Height - overlay.Height) / 2));
return bitmap;
}
private void frmQRCoder_Load(object sender, EventArgs e)
{
pictureBox1.Image = GenerateQRCode(400, 400, @"http://c-sharpcode.com");
pictureBox1.Height = 400;
pictureBox1.Width = 400;
}
private void btnValid_Click(object sender, EventArgs e)
{
MessageBox.Show(CheckQRCode(new Bitmap(pictureBox1.Image)).ToString());
}
}
}
You can easily get data from qr code image by using result.Text