Now a day's QR code becomes the internal part of our digital life. We will see a QR code on any E-tickets such as Stadium, air, bus, hotel booking. One of the best examples is India's UPI payment which is initiated by Indian PM Narendra Modi. You just have to scan the QR code to make the payment digital to the person’s bank account. This initiative is the very game changer since all the money comes to the bank and helped to stop the black money.
There are also many businesses generating the bar codes on E-Receipts to validate the authenticity of the receipt which can give the assurance that it's not been manipulated, which people did in the past simply editing and making false documents. There are many benefits using the QR code, the typical QR code will look like as follows.
Now in this article, we will learn how to generate a QR code using ASP.NET Core MVC. Let's learn step by step by creating an ASP.NET Core application.
Step 1: Create ASP.NET Core MVC Application
- Start then All Programs and select "Microsoft Visual Studio 2019".
- Once the Visual Studio Opens, Then click on Continue Without Code.
- Then Go to Visual Studio Menu, click on File => New Project then choose ASP.NET Core Web App (Model-View-Controller) Project Template.
- Then define the project name, location of the project, Target Framework, then click on the create button.
Step 1: Add QRCoder Nuget Package Reference
- Right click on the Solution Explorer, find Manage NuGet Package Manager and click on it
- After as shown into the image and type in search box "QRCoder"
- Select QRCoder as shown into the image
- Choose version of QRCoder library and click on install button
Step 2: Create the Model Class
using System.ComponentModel.DataAnnotations; namespace GeneratingQRCode.Models { public class QRCodeModel { [Display(Name = "Enter QRCode Text")] public string QRCodeText { get; set; } } }
Step 3: Add the Controller
using GeneratingQRCode.Models; using Microsoft.AspNetCore.Mvc; using QRCoder; using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace GeneratingQRCode.Controllers { public class HomeController : Controller { [HttpGet] public IActionResult CreateQRCode() { return View(); } [HttpPost] public IActionResult CreateQRCode(QRCodeModel qRCode) { QRCodeGenerator QrGenerator = new QRCodeGenerator(); QRCodeData QrCodeInfo = QrGenerator.CreateQrCode(qRCode.QRCodeText, QRCodeGenerator.ECCLevel.Q); QRCode QrCode = new QRCode(QrCodeInfo); Bitmap QrBitmap = QrCode.GetGraphic(60); byte[] BitmapArray = QrBitmap.BitmapToByteArray(); string QrUri = string.Format("data:image/png;base64,{0}", Convert.ToBase64String(BitmapArray)); ViewBag.QrCodeUri = QrUri; return View(); } } //Extension method to convert Bitmap to Byte Array public static class BitmapExtension { public static byte[] BitmapToByteArray(this Bitmap bitmap) { using (MemoryStream ms = new MemoryStream()) { bitmap.Save(ms, ImageFormat.Png); return ms.ToArray(); } } } }
Step 4: Create The View
@model GeneratingQRCode.Models.QRCodeModel @{ ViewData["Title"] = "www.compilemode.com"; } <hr /> <div class="row"> <div class="col-md-4"> <form asp-action="CreateQRCode"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <input asp-for="QRCodeText" class="form-control" /> <span asp-validation-for="QRCodeText" class="text-danger"></span> </div> <div class="form-group"> <input type="submit" value="Generate QR Code" class="btn btn-primary text-center" /> </div> <div class="form-group"> <img src="@ViewBag.QrCodeUri" class="img-thumbnail" /> </div> </form> </div> </div> @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }
Post a Comment