QR Code Generator In ASP.NET Core Using ZXING.NET

Introduction
 
In this article, we will explain how to create a QR Code Generator in ASP.NET Core 1.0, using Zxing.Net.
 
Background
 
I tried to create a QR Code Generator in ASP.NET Core, using third party libraries but in most of the cases codes are not fully supported in ASP.NET Core because of some version issues etc. I searched a lot in Google but finally I found "Zxing.Net" and it is a library, which supports decoding and generating of the barcodes. I had a discussion with MicJahn and came up  with a great solution.
 
Before reading this article, you must read the articles given below for ASP.NET Core knowledge.
Zxing.Net
 
A library, which supports decoding and generating of the barcodes (Example: QR Code, PDF 417, EAN, UPC, Aztec, Data Matrix, Codabar) within the images.
 
Assemblies required
 
The assemblies given below are required for QR Code Generator.
  1. using Microsoft.AspNetCore.Razor.TagHelpers;  
  2. using System;  
  3. using System.IO;  
  4. using ZXing.QrCode;  
Packages required
 
We need the packages given below for drawing and creating QR Code Generator.
  1. "CoreCompat.System.Drawing""1.0.0-beta006",    
  2. "ZXing.Net""0.15.0"    
C#
 
QRCodeTagHelper class given below contains QR Code Generator methods etc.
  1. namespace QRCodeApp {  
  2.     [HtmlTargetElement("qrcode")]  
  3.     public class QRCodeTagHelper: TagHelper {  
  4.         public override void Process(TagHelperContext context, TagHelperOutput output) {  
  5.             var QrcodeContent = context.AllAttributes["content"].Value.ToString();  
  6.             var alt = context.AllAttributes["alt"].Value.ToString();  
  7.             var width = 250; // width of the Qr Code    
  8.             var height = 250; // height of the Qr Code    
  9.             var margin = 0;  
  10.             var qrCodeWriter = new ZXing.BarcodeWriterPixelData {  
  11.                 Format = ZXing.BarcodeFormat.QR_CODE,  
  12.                     Options = new QrCodeEncodingOptions {  
  13.                         Height = height, Width = width, Margin = margin  
  14.                     }  
  15.             };  
  16.             var pixelData = qrCodeWriter.Write(QrcodeContent);  
  17.             // creating a bitmap from the raw pixel data; if only black and white colors are used it makes no difference    
  18.             // that the pixel data ist BGRA oriented and the bitmap is initialized with RGB    
  19.             using(var bitmap = new System.Drawing.Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))  
  20.             using(var ms = new MemoryStream()) {  
  21.                 var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);  
  22.                 try {  
  23.                     // we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image    
  24.                     System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);  
  25.                 } finally {  
  26.                     bitmap.UnlockBits(bitmapData);  
  27.                 }  
  28.                 // save to stream as PNG    
  29.                 bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);  
  30.                 output.TagName = "img";  
  31.                 output.Attributes.Clear();  
  32.                 output.Attributes.Add("width", width);  
  33.                 output.Attributes.Add("height", height);  
  34.                 output.Attributes.Add("alt", alt);  
  35.                 output.Attributes.Add("src", String.Format("data:image/png;base64,{0}", Convert.ToBase64String(ms.ToArray())));  
  36.             }  
  37.         }  
  38.     }  
  39. }   
Index.chtml
 
The code given below will display QR Code Generator.
  1. @{  
  2.     ViewData["Title"] = "Home";  
  3. }  
  4.    
  5. <h2>@ViewData["Title"].</h2>  
  6. <h3>@ViewData["Message"]</h3>  
  7.    
  8. A library which supports decoding and generating of barcodes (like QR Code, PDF 417, EAN, UPC, Aztec, Data Matrix, Codabar) within images.  
  9.    
  10. <qrcode alt="QR Code" content="https://rajeeshmenoth.wordpress.com/" />  
  11.  https://rajeeshmenoth.wordpress.com/  
_ViewImports.cshtml
 
The code Injecting TagHelper given below is in the entire Application.
  1. @addTagHelper "*, QRCodeApp"  
project.json
 
The dependencies given below are required to create QR Code Application.
  1. {  
  2.   "dependencies": {  
  3.     "Microsoft.AspNetCore.Diagnostics""1.0.0",  
  4.     "Microsoft.AspNetCore.Mvc""1.1.2",  
  5.     "Microsoft.AspNetCore.Mvc.Core""1.1.2",  
  6.     "Microsoft.AspNetCore.Server.IISIntegration""1.0.0",  
  7.     "Microsoft.AspNetCore.Server.Kestrel""1.0.1",  
  8.     "Microsoft.AspNetCore.StaticFiles""1.1.1",  
  9.     "Microsoft.Extensions.Logging.Console""1.0.0",  
  10.     "CoreCompat.System.Drawing""1.0.0-beta006",  
  11.     "ZXing.Net""0.15.0"  
  12.   },  
  13.    
  14.   "tools": {  
  15.     "Microsoft.AspNetCore.Server.IISIntegration.Tools""1.0.0-preview2-final"  
  16.   },  
  17.    
  18.   "frameworks": {  
  19.     "net452": { }  
  20.   },  
  21.    
  22.   "buildOptions": {  
  23.     "emitEntryPoint"true,  
  24.     "preserveCompilationContext"true  
  25.   },  
  26.    
  27.   "publishOptions": {  
  28.     "include": [  
  29.       "wwwroot",  
  30.       "web.config"  
  31.     ]  
  32.   },  
  33.    
  34.   "scripts": {  
  35.     "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]  
  36.   }  
  37. }  
Output
 

Reference
See Also
 
You can download other ASP.NET Core 1.0 source codes from MSDN Code, using the link, mentioned below.
Conclusion
 
We learned how to create a QR Code Generator in ASP.NET Core 1.0, using Zxing.Net. I hope you liked this article. Please share your valuable suggestions and the feedback.

Up Next
    Ebook Download
    View all
    Learn
    View all