DEV Community

Mehr Muhammad Hamza
Mehr Muhammad Hamza

Posted on • Updated on

How to generate QR code in C# : Full tutorial

In this tutorial, we will learn how to generate QR Codes and Barcodes using C# Language in a dot Net Framework. We will cover the following topics:

  • What is a Barcode.?
  • What is QR Code.?
  • Introduction to a Barcode Library
  • Step-by-Step Guide to generate a QR code.
  • Create Visual Studio Project
  • Download Barcode Library
  • Download using Package Manager Console
  • Download using Package Manager Solution
  • Download using direct link
  • Write Code to generate Barcodes
  • Write Code to generate QR codes
  • Adding a logo to the QR Code
  • Styling a QR Code
  • Supported QR Code and Barcode Format
  • Summary

What are Barcodes?

A barcode is a set of lines of different widths and sizes representing data that identifies a scannable object. Barcodes are often used to help organize and index information or pricing for a given item.

Barcodes are generally used to help track inventory, but they are also used on admission tickets in movie theaters and other events to uniquely identify and verify the validity of the ticket before the customer can enter. They’re also used to count sales generated by the event, which in turn makes it a lot more convenient to keep track of revenue.

What are QR Codes?

QR Code stands for Quick Response Code, a type of barcode that consists of a printed square pattern of small black and white squares that encode data which can be scanned into a computer system. The black and white squares can represent numbers from 0 to 9 and letters from A to Z, and are typically used for storing URLs or other information that can be read by a smartphone.

QR codes are frequently used to track information about products in a supply chain and – because many smartphones have built-in QR readers – they are often used in marketing and advertising campaigns.

C# QR Code Generator Library:

The Iron Barcode Library written in C# provides functions that help us to create QR codes and barcodes with just a single line of code. It also enables us to save the QR code or barcode in our desired file format. We will use the Barcode Library to create QR codes.

Step-By-Step Tutorial:

Let's begin the step-by-step tutorial to generate QR codes and barcodes.
Image description

Step # 1: Create a Visual Studio Project:

I am using Visual Studio 2019 and the console application template for this demonstration. You can use any of your choice.

Open Visual Studio = > Click on Create New Project = > Select Console Application Template = > Press Next => Name the Project => Press Next => Select your Target dot Net framework => Click on Create Button.
Image description

Step # 2: Install Barcode Library:

Before we begin, we need to install the Barcode Library. You can install it by using one of the following three methods:

Package Manager Console:

Write the following command in the Package Manager console. It will download and install the package for you.
Install-Package Barcode
Image description

Nuget Package Manager Solution:

You can also install the Barcode Library by using the Nuget Package Solution. Simply follow these steps:

Click on Tools => Nuget Package Manager => Manage Nuget package Solution.

This will open Nuget Package Manager for you. Click on Browse and search BarCode, then install the library.
Image description

Download From the Link:

As an alternative, the IronBarCode.Dll can be downloaded and added to your project as a reference from [.Net Barcode DLL].

Step # 3: Write Code to create Barcode:

Add Namespace:
using IronBarCode;
Write the following code:

GeneratedBarcode barcode = IronBarCode.BarcodeWriter.CreateBarcode("", BarcodeEncoding.Code128);
Enter fullscreen mode Exit fullscreen mode

Code Explanation:

GeneratedBarCode is the object type for storing barcode. BarcodeWriter.CreateBarcode is the function provided by the IronBarCode, and the link in the parameter is the text which we want to encode. I have added a link for the example, but you can add any as per your requirements.

In the second Parameter, we have to set the Encoding Scheme; for barcodes we select Code128.

After that, the SaveAsPng function will save your barcode as a png file in your storage device, as you can see. The IronBarCode provides several file formats for saving your barcode image, such as pdf, gif, png, Html, jpeg, image and WindowsBitmap, etc.

We can see that barcodes can be generated with just a single line of code.

Output - Barcode Image:

Image description

Step # 4: Write Code to Create QR Code:

When generating QR codes using Iron Barcode, we may opt to use the QR Code Writer class instead of the BarcodeWriter class. This class gives some new and interesting features for QR code writing. It allows us to set the QR error-correction level, allowing you to decide on a balance between the size of your QR code and its ease of readability.

GeneratedBarcode Qrcode = IronBarCode.QRCodeWriter.CreateQrCode("");

Enter fullscreen mode Exit fullscreen mode

Output - QR Code Image:

Image description

Adding a Logo to the QR Code:

Next I will show you how we can add a company logo to the QR Code. This practice is now in everyday use.

Write the following code:

var MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("", "CSharp-logo.png", 500);
Enter fullscreen mode Exit fullscreen mode

This example adds the C# logo to the QR code. It automatically sizes it to an appropriate size where the pure code is still readable, and aligns the logo to the QR code square grid so that it looks appropriate. The next line of code colors the barcode dark green. However, it does not discolor the logo.

Output - QR Code with Logo:

Image description

Styling a QR Code:

The visual depiction of a QR code or a Barcode can be manipulated very easily using Iron Barcode. The output of every BarcodeWriter operation is a generated barcode object. This generated barcode object has a Fluent API, allowing the graphical settings of the barcode to be set in a single line of code, similar to Linq.

Popular features for styling barcodes include resizing barcodes, setting margins, changing background colors, changing barcode colors, and verifying that the output barcode is still readable.

In a similar fashion, we may also add annotations such as text to a barcode in any typeface we want, or place the value of the barcode very easily under or above the barcode.


var MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("", "CSharp-logo.png", 500);
            MyQRWithLogo.AddAnnotationTextAboveBarcode("Product URL:");
Enter fullscreen mode Exit fullscreen mode

Image description

Supported QR Code and Barcode Format:

Iron Barcode supports a large number of commonly used barcode formats:

  • QR codes, including decorated and branded QR codes with logos and colors
  • Multi-format barcodes, such as Aztec, Data Matrix, CODE 93, and CODE 128
  • RSS Expanded Databar, UPS MaxiCode, and USPS IMb OneCode barcodes
  • Stacked linear barcodes such as RSS-14 and PDF-417
  • Conventional numerical barcode formats such as UPCA, UPCE, EAN-8, EAN-13, Codabar, ITF, MSI, and Plessey
var MyBarCode = QRCodeWriter.CreateQrCode("");
Enter fullscreen mode Exit fullscreen mode


Iron Barcode features a friendly API for developers to read and write barcodes for .NET, optimizing accuracy and ensuring a low error rate in real-world-use cases. For more information about the Iron Barcode, please visit this link.

Currently, if you buy the complete Iron Suite, you can get five libraries for the price of two. For more details, please click here.

Discussion (0)