ASP.NET Core Tutorial: Generate QR Codes with C#.NET

How to generate QR codes in asp.net core using C#.net with step-by-step tutorial example. Perfect for developers looking QR code for web development.
In this tutorial I will show you how you can generate a QR code image using c#.net in asp.net core mvc. So, for this article first we will create a new asp.net core 10 mvc application using c#.net and install the nuget package named as "QRCoder.Core". 

My previous article is Asp.Net Core 10: Access/Get RequestQuery String Value In Controller Using C#.Net.

You may like few of my previous articles:

Bind  Webgrid (GridView) with Entity Framework, Asp.net Core MVC, C#, SQLDownload File From wwwroot Folder Asp.Net Core 10 Using C#Render PartialView in Asp.Net Core MVC Using C#.NetMastering the for Loop in C# (.NET 10): Examples and Best PracticesASP.NET Core MVC Custom Error Page Tutorial (C#.NET)jQuery DateTime Picker Calendar in Asp.Net Core MVCStep‑by‑Step Guide: Import and Display Excel Data in ASP.NET Core MVC with C#.

QRCoder.Core NuGet Package

Add a controller class file and add the below code in it.
using Microsoft.AspNetCore.Mvc;
using QRCoder.Core;
using System.Drawing;

namespace QRCodeGeneration.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            var qrCodeData = qrGenerator.CreateQrCode("codemantra99.com", QRCodeGenerator.ECCLevel.Q);
            var qrCode = new QRCode(qrCodeData);
            Bitmap qrCodeImage = qrCode.GetGraphic(25);
            string imageSavePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "qrcode.png");
            qrCodeImage.Save(imageSavePath, System.Drawing.Imaging.ImageFormat.Png);

            return View();
        }
    }
} 
In above code I have generated the QA code and save it in wwwroot folder. Now let's create the view and add the below code. 
@{
    ViewData["Title"] = "QR Code";
}

<div style="text-align:left;">
    <img src="qrcode.png" style="width:200px;height:200px"/>
</div> 
In above view code i have used an image tag to display the QR code. Here qrcode.png have been provided as an image path to display the QR image. Now lets run the code to check the output.

QR Codes
QRCodeGeneration.zip 638KiB

Post a Comment