Generate QR Code in 4 Lines of Code

Sukhpinder Singh - May 23 '21 - - Dev Community

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.

    Install-Package QRCoder

Let’s Start

The “QRCoder” DLL helps to generate QR code 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);
Enter fullscreen mode Exit fullscreen mode




Description of each line of code

Initialize the QR code generator class

Create an instance of the QRCodeGenerator class.

QRCodeGenerator qrGenerator = new QRCodeGenerator();
Enter fullscreen mode Exit fullscreen mode




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);
Enter fullscreen mode Exit fullscreen mode




Generate QR code

The next step is to create the QR-code using the data initialized above.

QRCode qrCode = new QRCode(qrCodeData);
Enter fullscreen mode Exit fullscreen mode




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 QR-code. The GetGraphic method return image in the form of Bitmap by default.

Bitmap qrCodeImage = qrCode.GetGraphic(20);
Enter fullscreen mode Exit fullscreen mode




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");
}

Enter fullscreen mode Exit fullscreen mode




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();
}
}

Enter fullscreen mode Exit fullscreen mode




Optional parameters & overloads


//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"));
Enter fullscreen mode Exit fullscreen mode




Example of QR-code with my publication “The Tech Masters” logo

Github Repo

Thank you for reading. Keep visiting and share this in your network. Please put your thoughts and feedback in the comments section.

Follow me on Medium

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .