Creating QRCode Image in C#

Creating QRCode Image in C# form2

Creating QRCode Image in C#

In this post I’ll be showing code for creating QRCode image  in C# using ZXing.

About ZXing Libraring :

ZXing is widely used code for generating barcode images and scanning barcode value including platforms like Android and iOS too. And it is open source under Apache open source license. Thanks ZXing for making it available for free and empowering digital world.

Download ZXing using this link.

Our Project :

If you want to see directly code, jump to Step 3.

Step 1 : Design

Creating Barcode Image in C# Form 1

Take a Windows form in your Application and in it take a textbox control, 2 buttons and a picturebox. Below are their IDs accordingly.

Textbox : txtBarcodeValue

Generate button : btnGenerate

Save button : btnSave

Picturebox : picBarcode

What the application will be doing?

It will accept the string/integer/symbolic value in a textbox and convert the value in a QRCode image using ZXing library. To achieve this, we will need to import ZXing dll into our project. You will get this from above provide link. Add net 2.0 dll file in your project. And create a folder name BarcodeData in D drive.

Our application will be generating image in this folder, hence it is important. Or else you will get GDI+ errors. You can always change the location in below code.

Step 2 : Coding the Application.

Double Click on both buttons once from your design page of form to create their Click methods respectively. So, We will be creating methods for their click actions. Method names are :

For btnGenerate click method name : btnGenerate_Click(object sender, EventArgs e)

For btnSave click method name : btnSave_Click(object sender, EventArgs e)

And edit both methods respectively :

btnGenerate click method:

private void btnGenerate_Click(object sender, EventArgs e)
 {
 GenerateBarcode();

}

public void GenerateBarcode()
 {
 try
 {
 ZXing.QrCode.QRCodeWriter dd = new ZXing.QrCode.QRCodeWriter();
 String data = txtBarcodeValue.Text;

ZXing.Common.BitMatrix byteIMGNew1 = dd.encode(data, ZXing.BarcodeFormat.QR_CODE, 1, 1);

String Imagename = "D://BarcodeData//barcodeImage" + System.DateTime.Now.ToString("ddMMyyyyHHMMss") + ".png";
 if (!File.Exists(Imagename)) File.Create(Imagename).Close();

byteIMGNew1.ToBitmap(ZXing.BarcodeFormat.QR_CODE, data).Save(Imagename, System.Drawing.Imaging.ImageFormat.Png);
 byteIMGNew1.ToBitmap(ZXing.BarcodeFormat.QR_CODE, data).Dispose();

picBarcode.Image = System.Drawing.Image.FromFile(@"D://BarcodeData//barcodeImage.png");

byteIMGNew1.clear();
 }
 catch (Exception ex)
 {
 }
 }

And btnSave click method:

private void btnSave_Click(object sender, EventArgs e)
 {
 FolderBrowserDialog fdb = new FolderBrowserDialog();
 fdb.Description = "fdf";
 if (fdb.ShowDialog() == DialogResult.OK)
 {
 picBarcode.Image.Save(@fdb.SelectedPath + "\\Image" + System.DateTime.Now.ToString("ddMMyyyyHHmmss") + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
 MessageBox.Show("Barcode Image saved Successfully. Path : " + fdb.SelectedPath);
 }

}

Below is my full code of my form :

using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
 using System.Data.OleDb;
 using System.Configuration;
 using ZXing.PDF417.Internal.EC;
 using System.IO;

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

private void btnGenerate_Click(object sender, EventArgs e)
 {
 GenerateBarcode();

}

public void GenerateBarcode()
 {
 try
 {
 ZXing.QrCode.QRCodeWriter dd = new ZXing.QrCode.QRCodeWriter();
 String data = txtBarcodeValue.Text;

ZXing.Common.BitMatrix byteIMGNew1 = dd.encode(data, ZXing.BarcodeFormat.QR_CODE, 1, 1);

String Imagename = "D://BarcodeData//barcodeImage" + System.DateTime.Now.ToString("ddMMyyyyHHMMss") + ".png";
 if (!File.Exists(Imagename)) File.Create(Imagename).Close();

byteIMGNew1.ToBitmap(ZXing.BarcodeFormat.QR_CODE, data).Save(Imagename, System.Drawing.Imaging.ImageFormat.Png);
 byteIMGNew1.ToBitmap(ZXing.BarcodeFormat.QR_CODE, data).Dispose();

picBarcode.Image = System.Drawing.Image.FromFile(@"D://BarcodeData//barcodeImage"+System.DateTime.Now.ToString("ddMMyyyyHHMMss") +".png");

byteIMGNew1.clear();
 }
 catch (Exception ex)
 {
 }
 }

private void btnSave_Click(object sender, EventArgs e)
 {
 FolderBrowserDialog fdb = new FolderBrowserDialog();
 fdb.Description = "fdf";
 if (fdb.ShowDialog() == DialogResult.OK)
 {
 picBarcode.Image.Save(@fdb.SelectedPath + "\\Image" + System.DateTime.Now.ToString("ddMMyyyyHHmmss") + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
 MessageBox.Show("Barcode Image saved Successfully. Path : " + fdb.SelectedPath);
 }
 }
 }
 }

Run the above code, you will be able to successfully create a QRCode and save it to your desired location.

Creating QRCode Image in C# form2

Step 3 : Only code explanation

First create the variable to create QRCode using ZXing’s QRCodeWriter.

ZXing.QrCode.QRCodeWriter dd = new ZXing.QrCode.QRCodeWriter();

Create an Bitmap variable of ZXing’s Bitmatrix which can save create our barcode/qrcode image.

ZXing.Common.BitMatrix byteIMGNew1 = dd.encode(data, ZXing.BarcodeFormat.QR_CODE, 1, 1);

Now, Assign a file name and create it if not exists.

String Imagename = "D://BarcodeData//barcodeImage" + System.DateTime.Now.ToString("ddMMyyyyHHMMss") + ".png";
 if (!File.Exists(Imagename)) File.Create(Imagename).Close();

And Create and save Image with your dataValue using bitmap instance and dispose the same.

byteIMGNew1.ToBitmap(ZXing.BarcodeFormat.QR_CODE, dataValue).Save(Imagename, System.Drawing.Imaging.ImageFormat.Png);
 byteIMGNew1.ToBitmap(ZXing.BarcodeFormat.QR_CODE, data).Dispose();

Now, assign the generated image to our Picturebox.

picBarcode.Image = System.Drawing.Image.FromFile(@"D://BarcodeData//barcodeImage"+System.DateTime.Now.ToString("ddMMyyyyHHMMss") +".png");

And dispose the bitmap instance

byteIMGNew1.clear();

That’s all. 🙂


1 thought on “Creating QRCode Image in C#”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.