In this article, we are going to learn how to Create a QR Code in ASP.NET Core which will work on Windows Hosting as well as on Linux Hosting.

QR Code in ASP.NET Core

Let’s Create a QR Code in ASP.NET Core

Because if you think of hosting applications across platforms then we must think that we are not using any windows dependent library in the application.

In this scenario, one library which is used by Most people for generating QR codes is QRCoder which supports windows only because it uses System.Drawing library due to this it is not possible to use this library in a Linux environment.

For that, we are going to use another library QRCoder-ImageSharp which is written by Joerg Plenert and uses ImageSharp assembly that supports more platforms.

One thing about QRCoder-ImageSharp is it can only use to generate QR codes but other features of QRCoder are not available in this library.

  • QRCoder-ImageSharp is using ImageSharp instead of System.Drawing
  • QRCoder-ImageSharp is not supporting ArtQRCode
  • QRCoder-ImageSharp does not make rounded rectangles when including logos in QRCode
  • QRCoder-ImageSharp does not support Russian QRCode payloads (we stand with Ukraine!)

Table of Contents

  • Creating Project
  • Project structure
  • Adding Controller and View
  • Adding QRCoder-ImageSharp package NuGet Package
  • Generating QR Code
  • Rendering QRCode on View
  • Setting Different Colours to QR Code
  • Rendering QRCode on View For Different Colour Samples
  • Final Output

Creating Project

We are going to create a new application, as shown below.

Next, we are going to set Project Name WebQRCodeDemo and location. In the last part, we will choose the .Net Core framework and ASP.NET Core Version 6.0 as the framework for the application and a few advanced settings such as configuring HTTPS and enabling docker; we are not going to enable docker settings for this project.

Now finally, click on create button to create a project.

Project structure

The project structure is generated according to the configuration.

After creating the project next, we are going to Controller and View.

Adding Controller and View for Demo

using Microsoft.AspNetCore.Mvc;

namespace WebQRCodeDemo.Controllers
{
    public class DemoController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

Adding QRCoder-ImageSharp package NuGet Package

For creating QR Code we are going to install the package QRCoder-ImageSharp.

After adding the NuGet package Next, we are going to Add a View.

Generating QR Code

Here we have written code for generating QR Code where we are embedding the URL and generating QRCode.

using Microsoft.AspNetCore.Mvc;
using QRCoder;

namespace WebQRCodeDemo.Controllers
{
    public class DemoController : Controller
    {
        public IActionResult Index()
        {
            // Url which we want to embed in QR Code
            var url = "https://tutexchange.com/";

            QRCodeData qrCodeData;
            using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
            {
                qrCodeData = qrGenerator.CreateQrCode(url, QRCodeGenerator.ECCLevel.Q);
            }

            // Image Format
            var imgType = Base64QRCode.ImageType.Png;
         
            var qrCode = new Base64QRCode(qrCodeData);
            //Base64 Format
            string qrCodeImageAsBase64 = qrCode.GetGraphic(20, SixLabors.ImageSharp.Color.DeepPink, SixLabors.ImageSharp.Color.White, true, imgType);

            //Sending Image format and base64 format to View
            TempData["qrCodeImageAsBase64"] = qrCodeImageAsBase64;
            TempData["imgType"] = imgType.ToString().ToLower();

            return View();
        }
    }
}

ECC level (error correction code level) will be used to increase the strength of a QR Code barcode symbol.

Rendering QRCode on View

@{
    ViewData["Title"] = "Index";
}

<h1>Index</h1>

<img alt="Embedded QR Code" style='width:250px; height:250px'
     src="data:image/@TempData["imgType"];base64,@Html.Raw(TempData["qrCodeImageAsBase64"])" />

Setting Different Colours to QR Code

Wow, we have QRCode in Pink Colour do we have the option to change the colour of it?

Yes, we have the option to change the colour of the QR code.

  • 1.         darkColor
  • 2.         lightColor

Code Snippet

string qrCodeImageAsBase64 = qrCode.GetGraphic(20, SixLabors.ImageSharp.Color.DeepPink, SixLabors.ImageSharp.Color.White, true, imgType);
using Microsoft.AspNetCore.Mvc;
using QRCoder;

namespace WebQRCodeDemo.Controllers
{
    public class DemoController : Controller
    {
        public IActionResult Index()
        {
            // Url which we want to embed in QR Code
            var url = "https://tutexchange.com/";

            QRCodeData qrCodeData;
            using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
            {
                qrCodeData = qrGenerator.CreateQrCode(url, QRCodeGenerator.ECCLevel.Q);
            }

            // Image Format
            var imgType = Base64QRCode.ImageType.Png;
         
            var qrCode = new Base64QRCode(qrCodeData);
            //Base64 Format
            string qrCodeImageAsBase64 = qrCode.GetGraphic(20, SixLabors.ImageSharp.Color.DeepPink, SixLabors.ImageSharp.Color.White, true, imgType);

            //Sending Image format and base64 format to View
            TempData["qrCodeImageAsBase64"] = qrCodeImageAsBase64;
            TempData["imgType"] = imgType.ToString().ToLower();


            //Base64 Format
            string qrCodeImageAsBase64_orange = qrCode.GetGraphic(20, SixLabors.ImageSharp.Color.DarkOrange, SixLabors.ImageSharp.Color.White, true, imgType);

            //Sending Image format and base64 format to View
            TempData["qrCodeImageAsBase64_orange"] = qrCodeImageAsBase64_orange;
            TempData["imgType"] = imgType.ToString().ToLower();


            //Base64 Format
            string qrCodeImageAsBase64_red = qrCode.GetGraphic(20, SixLabors.ImageSharp.Color.DarkRed, SixLabors.ImageSharp.Color.White, true, imgType);

            //Sending Image format and base64 format to View
            TempData["qrCodeImageAsBase64_red"] = qrCodeImageAsBase64_red;
            TempData["imgType"] = imgType.ToString().ToLower();


            //Base64 Format
            string qrCodeImageAsBase64_Purple = qrCode.GetGraphic(20, SixLabors.ImageSharp.Color.DarkMagenta, SixLabors.ImageSharp.Color.White, true, imgType);

            //Sending Image format and base64 format to View
            TempData["qrCodeImageAsBase64_Purple"] = qrCodeImageAsBase64_Purple;
            TempData["imgType"] = imgType.ToString().ToLower();

            return View();
        }
    }
}

Rendering QRCode on View For Different Colour Samples


@{
    ViewData["Title"] = "https://tutexchange.com/";
}

<h4>https://tutexchange.com</h4>

<img alt="Embedded QR Code" style='width:250px; height:250px'
     src="data:image/@TempData["imgType"];base64,@Html.Raw(TempData["qrCodeImageAsBase64"])" />


<img alt="Embedded QR Code" style='width:250px; height:250px'
     src="data:image/@TempData["imgType"];base64,@Html.Raw(TempData["qrCodeImageAsBase64_orange"])" />

<img alt="Embedded QR Code" style='width:250px; height:250px'
     src="data:image/@TempData["imgType"];base64,@Html.Raw(TempData["qrCodeImageAsBase64_red"])" />

<img alt="Embedded QR Code" style='width:250px; height:250px'
     src="data:image/@TempData["imgType"];base64,@Html.Raw(TempData["qrCodeImageAsBase64_Purple"])" />

Final Output

Reference

https://github.com/JPlenert/QRCoder-ImageSharp

Other Articles you may like

How to Create a Zip file in ASP.NET Core

https://tutexchange.com/how-to-create-a-zip-file-in-asp-net-core/

https://www.facebook.com/Tutexchange

Originally tweeted by Saineshwar Bageri | Microsoft MVP (@saihacksoft) on November 27, 2022.

By Saineshwar

Microsoft MVP for Developer Technologies | C# Corner MVP | Code project MVP | Senior Technical Lead | Author | Speaker | Love .Net | Full Stack developer | Open source contributor.