Dynamically Generated QR Codes in ASP.Net

300px-CodeCmmt002.svgWhat we want to do here is generate a QR code on pageload and show it to user in a designated div. The problem is, Image control takes an url for a source and your generated picture doesn’t have an url…  yet.

This elegant solution was proposed by my good friend Istemi. Without further ado let’s dive in.

Aspx Side:

 

As you can see we don’t have anything special except for a curious div which has an id of qrcode and runs at server. Turns out we need this to:

Codebehind:

As I’ve said, we need this to refer to it from codebehind and add our generated image/bitmap to the div as an Image with a source. But, you may ask, what does generate this QR code. Well, turns out we need a library to do that. I have used Zxing.net for generating my image with this code:

Easy as pies, right?

Edit:

If you are interested in QR code in WPF and ASP.Net MVC, Scott Hanselman had written a perfect guide for that. Bonus is, you don’t even have to save the file in the server!

http://www.hanselman.com/blog/HowToDisplayAQRCodeInASPNETAndWPF.aspx

Leave a Reply