Generate QR Code in 4 Lines of Code
Tags: beginners, csharp, webdev, tutorial
Published: 2021-05-23 06:10:58

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

![](https://cdn-images-1.medium.com/max/3158/1*GBJfp_W-WOMcEldZRCQTjw.gif)

## 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);

## 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 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
    **//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"));

### Example of QR-code with my publication “[The Tech Masters](https://medium.com/the-tech-masters)” logo

![](https://cdn-images-1.medium.com/max/2000/1*OdVSbx5DtYrxdJ2Q9fEf7A.png)

[Github Repo](https://github.com/ssukhpinder/QRCodeExample)

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](https://singhsukhpinder.medium.com/)