Sunday, 28 July 2013

QR codes and generation using .net ( asp.net )

QR code (Quick Response Code)  type of matrix barcode (or two-dimensional barcode), QR Code system has become popular  due to its fast readability and greater storage capacity. Basically it is an image consists of black dots on white background like shown below.



QR Code can be generated using .net technology , there are number of open source 3rd party libraries. Last weekend I got hands with QR codes and their generation and I used two libraries i.e 

XZing.net and MessagingToolkit.QRCode.1.3.0 , which can be used right from Package Manager console of visual studio by following commands respectively.
PM> Install-Package ZXing.Net
PM> Install-Package MessagingToolkit.QRCode

Lets come to their usage , I did use them with asp.net sample project but you can be use the same code with any type of project i.e. Windows Forms , Library project etc , lets start how to generate QR codes.

First create asp.net project from visual studio project templates , add a text field , a button and an image type. Register click event of button and in code behind file inside event use the following code:


            QRCodeEncoder encoder = new QRCodeEncoder();
            encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H; // 30%
            encoder.QRCodeScale = 4;
            Bitmap img = encoder.Encode(TextArea1.Text);

           
            img.Save(Server.MapPath("images/") + "img.jpg", ImageFormat.Jpeg);
            QRImage.ImageUrl ="~/images/img.jpg"; // will save the image in images folder as well

So that is simply how we can generate QR code using "MessagingToolkit.QRCode.1.3.0" library , code for Zxing.net will be available soon. Sample project can be downloaded from Here.