C# QR Code Generator for .NET

by Jennifer Wright

As you may have read in the Creating a Barcode introductory tutorial, creating, styling, and exporting barcodes as images with IronBarcode is incredibly simple and may often be achieved in a single line of code.

In this example, we will look more in depth at QR codes, which are becoming increasingly popular in industrial applications and also in retail.

Install QR Code Generator Library in C#

C# NuGet Library for

Install with NuGet

Install-Package BarCode
or
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

C# NuGet Library for

Install with NuGet

Install-Package BarCode
or
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer

Check out IronBarcode on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming with C#.

C# NuGet Library for nuget.org/packages/BarCode/
Install-Package BarCode

Consider installing the IronBarcode DLL directly. Download and manually install it for your project or GAC form: IronBarCode.zip

Dll Img related to Install QR Code Generator Library in C#

Manually install into your project

Download DLL

Before we start we need to install the BarCode NuGet Package.

Install-Package BarCode

https://www.nuget.org/packages/BarCode/

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

Importing NameSpaces

For this tutorial we need to make sure our class files reference IronBarCode and also some normal system assemblies.

using IronBarCode;
using System;
using System.Drawing;
using System.Linq;
using IronBarCode;
using System;
using System.Drawing;
using System.Linq;
Imports IronBarCode
Imports System
Imports System.Drawing
Imports System.Linq
VB   C#


Create a QR Code with 1 Line of Code

Our first example shows us how to create a standardized barcode with some simple text, a 500 pixel square diameter, with a medium error correction.

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-1.cs
using IronBarCode;

// Generate a Simple BarCode image and save as PDF
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng("MyQR.png");
Imports IronBarCode

' Generate a Simple BarCode image and save as PDF
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng("MyQR.png")
VB   C#

Error correction allows us to define how easy it will be for a QR code to be read in real world circumstances. Higher error correction levels create larger QR codes with more pixels and more complexity.

C# Create QR Code Image

In the second example, we will look at a use case where a company wishes to add to a logo to their QR code, which is something we see commonly these days. By using the QRCodeWriter.CreateQRCodeWithLogo method, in the code sample below you can see how easy this is.

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-2.cs
using IronBarCode;
using IronSoftware.Drawing;

// You may add styling with color, logo images or branding:
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);
myQRCodeWithLogo.ResizeTo(500, 500).SetMargins(10).ChangeBarCodeColor(Color.DarkGreen);

// Logo will automatically be sized appropriately and snapped to the QR grid.
myQRCodeWithLogo.SaveAsPng("myQRWithLogo.png");
Imports IronBarCode
Imports IronSoftware.Drawing

' You may add styling with color, logo images or branding:
Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo)
myQRCodeWithLogo.ResizeTo(500, 500).SetMargins(10).ChangeBarCodeColor(Color.DarkGreen)

' Logo will automatically be sized appropriately and snapped to the QR grid.
myQRCodeWithLogo.SaveAsPng("myQRWithLogo.png")
VB   C#

This example adds the Visual Studio logo to the barcode. It automatically sizes it to an appropriate size where the pure code is still readable and aligns that 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.

C# Create QR Code With Logo Image

Save as Image PDF or HTML

Finally, we save that QR as a PDF. The final line of code opens the PDF in your default PDF browser for your convenience.

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-3.cs
using IronBarCode;

// You may add styling with color, logo images or branding:
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);

myQRCodeWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen);

// Save as PDF
myQRCodeWithLogo.SaveAsPdf("MyQRWithLogo.pdf");

// Also Save as HTML
myQRCodeWithLogo.SaveAsHtmlFile("MyQRWithLogo.html");
Imports IronBarCode

' You may add styling with color, logo images or branding:
Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo)

myQRCodeWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen)

' Save as PDF
myQRCodeWithLogo.SaveAsPdf("MyQRWithLogo.pdf")

' Also Save as HTML
myQRCodeWithLogo.SaveAsHtmlFile("MyQRWithLogo.html")
VB   C#

Verifying QR Codes

When adding logos to QR codes and changing colors, we want to make sure that our QR is still readable.

By using the GeneratedBarcode.Verify() method, we can test whether or not our barcode is still readable.

In the following code example, we will see that changing a QR code to light blue actually makes it unreadable. We detect this within the code and prefer dark blue.

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-4.cs
using IronBarCode;
using IronSoftware.Drawing;
using System;

// Verifying QR Codes
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode MyVerifiedQR = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);

MyVerifiedQR.ChangeBarCodeColor(System.Drawing.Color.LightBlue);

if (!MyVerifiedQR.Verify())
{
    Console.WriteLine("\t LightBlue is not dark enough to be read accurately.  Lets try DarkBlue");
    MyVerifiedQR.ChangeBarCodeColor(Color.DarkBlue);
}
MyVerifiedQR.SaveAsHtmlFile("MyVerifiedQR.html");

// open the barcode html file in your default web browser
System.Diagnostics.Process.Start("MyVerifiedQR.html");
Imports Microsoft.VisualBasic
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System

' Verifying QR Codes
Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private MyVerifiedQR As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo)

MyVerifiedQR.ChangeBarCodeColor(System.Drawing.Color.LightBlue)

If Not MyVerifiedQR.Verify() Then
	Console.WriteLine(vbTab & " LightBlue is not dark enough to be read accurately.  Lets try DarkBlue")
	MyVerifiedQR.ChangeBarCodeColor(Color.DarkBlue)
End If
MyVerifiedQR.SaveAsHtmlFile("MyVerifiedQR.html")

' open the barcode html file in your default web browser
System.Diagnostics.Process.Start("MyVerifiedQR.html")
VB   C#

The final lines of code export the QR code as a standalone HTML file with no assets and then open that HTML file in your default browser.

C# read and write QR

Reading and Writing Binary Data

QR codes are an excellent format for dealing with binary data. Sometimes binary data is simply more space-efficient or more appropriate than dealing with text.

In this example, we will encode some binary data from a string, write that to a barcode in QR format, and then read that back as a bit array of binary data. You will note that this feature is not common to many barcode libraries, making IronBarcode unique in this capacity.

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-5.cs
using IronBarCode;
using System;
using System.Linq;

// Create Some Binary Data - This example equally well for Byte[] and System.IO.Stream
byte[] BinaryData = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/");

// WRITE QR with Binary Content
QRCodeWriter.CreateQrCode(BinaryData, 500).SaveAsImage("MyBinaryQR.png");

// READ QR with Binary Content
var MyReturnedData = BarcodeReader.Read("MyBinaryQR.png").First();
if (BinaryData.SequenceEqual(MyReturnedData.BinaryValue))
{
    Console.WriteLine("\t Binary Data Read and Written Perfectly");
}
else
{
    throw new Exception("Corrupted Data");
}
Imports Microsoft.VisualBasic
Imports IronBarCode
Imports System
Imports System.Linq

' Create Some Binary Data - This example equally well for Byte[] and System.IO.Stream
Private BinaryData() As Byte = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/")

' WRITE QR with Binary Content
QRCodeWriter.CreateQrCode(BinaryData, 500).SaveAsImage("MyBinaryQR.png")

' READ QR with Binary Content
Dim MyReturnedData = BarcodeReader.Read("MyBinaryQR.png").First()
If BinaryData.SequenceEqual(MyReturnedData.BinaryValue) Then
	Console.WriteLine(vbTab & " Binary Data Read and Written Perfectly")
Else
	Throw New Exception("Corrupted Data")
End If
VB   C#
C# read and write binary data as a QR Code

In summary, the Barcode C# Library is specifically designed with real world QR code usage in mind. Not only does it read QR codes quickly and accurately, but it allows us to style them, add logos, verify barcodes, and encode them with binary data.

Reading QR Codes

To save you jumping between tutorials I will add a code sample for my favorite way to read QR codes using IronBarcode.

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-6.cs
using IronBarCode;
using System;
using System.Linq;

BarcodeResults result = BarcodeReader.Read("QR.png", new BarcodeReaderOptions() { ExpectBarcodeTypes = BarcodeEncoding.QRCode });
if (result != null)
{
    Console.WriteLine(result.First().Value);
}
Imports IronBarCode
Imports System
Imports System.Linq

Private result As BarcodeResults = BarcodeReader.Read("QR.png", New BarcodeReaderOptions() With {.ExpectBarcodeTypes = BarcodeEncoding.QRCode})
If result IsNot Nothing Then
	Console.WriteLine(result.First().Value)
End If
VB   C#

A more advanced form also exists with more developer control:

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-7.cs
using IronBarCode;
using System;
using System.Linq;

BarcodeReaderOptions options = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Faster,
    ExpectMultipleBarcodes = false,
    ExpectBarcodeTypes = BarcodeEncoding.All,
    Multithreaded = false,
    MaxParallelThreads = 0,
    CropArea = null,
    UseCode39ExtendedMode = false,
    RemoveFalsePositive = false,
    ImageFilters = null
};

BarcodeResults result = BarcodeReader.Read("QR.png", options);
if (result != null)
{
    Console.WriteLine(result.First().Value);
}
Imports IronBarCode
Imports System
Imports System.Linq

Private options As New BarcodeReaderOptions With {
	.Speed = ReadingSpeed.Faster,
	.ExpectMultipleBarcodes = False,
	.ExpectBarcodeTypes = BarcodeEncoding.All,
	.Multithreaded = False,
	.MaxParallelThreads = 0,
	.CropArea = Nothing,
	.UseCode39ExtendedMode = False,
	.RemoveFalsePositive = False,
	.ImageFilters = Nothing
}

Private result As BarcodeResults = BarcodeReader.Read("QR.png", options)
If result IsNot Nothing Then
	Console.WriteLine(result.First().Value)
End If
VB   C#

Moving Forward

To learn more about this example and working with QR codes in C#, we encourage you to fork it on our GitHub page or download the source code from our site.

Source Code Downloads

Download this C# QR Code Generator Tutorial and DLL.

Further Documentation

You may also find the QRCodeWriter and BarcodeReader classes within the API Reference of interest.

They document the full feature set of IronBarcode, a VB.NET Barcode Generator, which could not possibly be packed into a single tutorial.

The .NET Barcode library can revolutionise how to connect online and offline product catalogs through producing a unique identifier for both a product and its specific physical batch production code

Jennifer Wright

Head of Application Architecture

Jenny leads software product architecture in a pharmaceutical giant's digital product development arm. Jenny's team are using IronBarcode to connect online and offline product catalogs.