How to Generate a QR Code Image in Four Lines of Code

Written by ssukhpinder | Published 2021/06/13
Tech Story Tags: csharp | dotnet | qr-codes | qr-code-api | aspnet | programming | c-sharp | net-core | web-monetization

TLDR A QR code is a computer-readable identification that contains data about the item to which it is attached. The article demonstrates how to return the QR Code image as a response from ASP.Net Core API. The “QRCoder” DLL helps generate QR codes with just four lines of code in C#. The author provides this code and software ‘AS IS’, without warranty of any kind, express or implied, including but not limited to fitness for a particular purpose and non-infringement.via the TL;DR App

A QR code is a computer-readable identification that contains data about the item to which it is attached. The article demonstrates how to return the QR Code image as a response from .Net Core API.

QR-Code Live Demo

Prerequisites

  • Visual Studio or Visual Studio Code already installed
  • Install the below package, i.e., “QRCoder” via NuGet package manager.

Let’s Start

The “QRCoder” DLL helps generate QR codes with just four lines of code in C#.
4 Lines of Code Snippet
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrText, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
Bitmap qrCodeImage = qrCode.GetGraphic(20);

Description of each line of code

Initialize the QR code generator class: Create an instance of the QRCodeGenerator class.
QRCodeGenerator qrGenerator = new QRCodeGenerator();
Create QR code data: The next step is to initialize QR code data using the CreateQrCode method, which takes two arguments, i.e., string text for encoding inside the QR code, and another case defines the error correction level, i.e., ECCLevel.
Here, four different levels L (7%), M (15%), Q (25%), and H (30%) are available, whereby the percentage represents the hidden portion of the QR-code until the error correction algorithm can’t recreate the original message encoded in the QR code.
QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrText, QRCodeGenerator.ECCLevel.Q);
Generate QR code: The next step is to create the QR-code using the data initialized above.
QRCode qrCode = new QRCode(qrCodeData);
Create a graphical image: Finally, represent the QR code into a graphical image, as shown below. The
GetGraphic
method takes one argument, which defines the size of the QR-code. The
GetGraphic
method return image in the form of Bitmap by default.
Bitmap qrCodeImage = qrCode.GetGraphic(20);

How to return QR-code Image from .Net Core API response?

Controller GET route snippet uses the QR-code library and returns the QR code image as a response.
[HttpGet]
[Route("qenerate/{qrText}")]
public IActionResult GetQrCode(string qrText)
{
	QRCodeGenerator qrGenerator = new QRCodeGenerator();
	QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrText, QRCodeGenerator.ECCLevel.Q);
	QRCode qrCode = new QRCode(qrCodeData);
	Bitmap qrCodeImage = qrCode.GetGraphic(20);
	return File(BitmapToBytes(qrCodeImage), "image/jpeg");
}
Bitmap to bytes function code snippet: QR-code, by default, generates a Bitmap, below function used to convert Bitmap to bytes.
private static Byte[] BitmapToBytes(Bitmap img)
{
	using (MemoryStream stream = new MemoryStream())
	{
		img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
		return stream.ToArray();
	}
}
Optional parameters & overloads: Example of QR-code with my publication “The Tech Masters” logo
//Set color by using Color-class types
Bitmap qrCodeImage = qrCode.GetGraphic(20, Color.DarkRed, Color.PaleGreen, true);
//Set color by using HTML hex color notation
Bitmap qrCodeImage = qrCode.GetGraphic(20, "#000ff0", "#0ff000");
//Set logo in center of QR-code
Bitmap qrCodeImage = qrCode.GetGraphic(20, Color.Black, Color.White, (Bitmap)Bitmap.FromFile("C:\\myimage.png"));
Github Repo: consist of default & custom colored QR-code generation implemented in ASP.Net Core API.
Thank you for reading. Keep visiting and share this in your network.
Disclaimer: The author provides this code and software “AS IS”, without
warranty of any kind, express or implied, including but not limited to fitness for a particular purpose and non-infringement. In no event shall the
author be liable for any claim, damages or other liability in connection with the software or code provided here



Written by ssukhpinder | Programmer by heart | C# | Python | .Net Core | Xamarin | Angular | AWS
Published by HackerNoon on 2021/06/13