How to generate and read QR code in asp.net

June 24, 2013 by Anuraj

.Net ASP.Net Visual Studio Windows Forms Windows Phone

QR code (abbreviated from Quick Response Code) is the trademark for a type of matrix barcode (or two-dimensional barcode) first designed for the automotive industry in Japan; a barcode is an optically machine-readable label that is attached to an item and that records information related to that item: The information encoded by a QR code may be made up of four standardized types (“modes”) of data (numeric, alphanumeric, byte / binary, Kanji) or, through supported extensions, virtually any type of data. - Wikipedia.

Today I found a nice library which helps to create and read the QR code, - ZXing.net is a port of ZXing, an open-source, multi-format 1D/2D barcode image processing library originally implemented in Java. You can find more details and source code from the codeplex website.

You can install ZXing.Net using Package Manager console.

Install-Package ZXing.Net 

You can generate QR code using following code

var writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
var result = writer.Write("http://www.dotnetthoughts.net");
var barcodeBitmap = new Bitmap(result);
barcodeBitmap.Save
    (context.Response.OutputStream, ImageFormat.Jpeg);
context.Response.ContentType = "image/jpeg";
context.Response.End();

You can find details about the Barcode Contents from here. And you can read / decode the QR code using following code.

var reader = new BarcodeReader();
//Saving the uploaded image and reading from it
var fileName =
    Path.Combine(Request.MapPath("~/Imgs"), "QRImage.jpg");
fileUpload.SaveAs(fileName);
var result = reader.Decode(new Bitmap(fileName));
Response.Write(result.Text);

Here is the QR code generated using the code

dotnetthoughts - QR Code

Happy Programming.

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub