C# QR Code Generator for .NET
Published: 2021-12-13 08:04:28

---
canonical_url: https://ironsoftware.com/csharp/barcode/tutorials/csharp-qr-code-generator/
---

As you may have read in the Creating a Barcode introductory tutorial, creating, styling, and exporting barcodes as images with Iron Barcode 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.

## How to Read and Generate QR Codes in C#: Follow these 5 Steps

<ol>
  <li>Download QR code Generator Library</li>
  <li>Create a QR code by adding one line of code</li>
  <li>Add your logo to a QR code</li>
  <li>Verify your QR code is Readable</li>
  <li>Verify your QR code is Readable</li>
</ol>

## Install QR Code Generator Library in C#

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

`PM > Install-Package Barcode`

<a href="https://www.nuget.org/packages/BarCode/" target="_blank" rel="nofollow">https://www.nuget.org/packages/BarCode/</a>

As an alternative, the <a href="https://ironsoftware.com/csharp/barcode/packages/IronBarCode.zip" target="_blank">IronBarCode DLL can be downloaded</a> and added to your project as a reference from [.NET Barcode DLL].

{% youtube nFExd00E5Ag %}

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


## 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.

**C#:**
```cs
// Generate a Simple BarCode image and save as PDF
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng("MyQR.png");
```
**VB:**
```vb
' Generate a Simple BarCode image and save as PDF
QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng("MyQR.png")
```
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.

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fcomjnwcfm8rp1xm8guh.png)

## Adding a Logo

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.

**C#:**
```cs
// Adding a Logo
var MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "visual-studio-logo.png", 500);
MyQRWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen);
```
**VB:**
```vb
' Adding a Logo
Dim MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "visual-studio-logo.png", 500)
MyQRWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen)
```

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.

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ovu2paivq7ud0w99pfl.png)

### 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.

**C#:**
```cs
// Adding a Logo
var MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "visual-studio-logo.png", 500);
MyQRWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen);
//Save as PDF
MyQRWithLogo.SaveAsPdf("MyQRWithLogo.pdf");
//Also Save as HTML
MyQRWithLogo.SaveAsHtmlFile("MyQRWithLogo.html");
```
**VB:**
```vb
' Adding a Logo
Dim MyQRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "visual-studio-logo.png", 500)
MyQRWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen)
'Save as PDF
MyQRWithLogo.SaveAsPdf("MyQRWithLogo.pdf")
'Also Save as HTML
MyQRWithLogo.SaveAsHtmlFile("MyQRWithLogo.html")
```

## 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.

**C#:**
```cs
// Verifying QR Codes
// using System.Drawing;
var MyVerifiedQR = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "visual-studio-logo.png", 350);
MyVerifiedQR.ChangeBarCodeColor(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 htm, file in your default web browser
System.Diagnostics.Process.Start("MyVerifiedQR.html");
```
**VB:**
```vb
Imports Microsoft.VisualBasic
' Verifying QR Codes
' using System.Drawing;
Dim MyVerifiedQR = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", "visual-studio-logo.png", 350)
MyVerifiedQR.ChangeBarCodeColor(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 htm, file in your default web browser
System.Diagnostics.Process.Start("MyVerifiedQR.html")
```

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.

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nratw57o4j404a5v0a2d.png)

## 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 Iron Barcode unique in this capacity.

**C#:**
```cs
// Reading and Writing Binary Data
// 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.QuicklyReadOneBarcode("MyBinaryQR.png");
if (BinaryData.SequenceEqual(MyReturnedData.BinaryValue))
{
    Console.WriteLine("\t Binary Data Read and Written Perfectly");
}
else
{
    throw new Exception("Corrupted Data");
}
```
**VB:**
```vb
Imports Microsoft.VisualBasic
' Reading and Writing Binary Data
' using System.Linq;
'Create Some Binary Data - This example equally well for Byte[] and System.IO.Stream
Dim 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.QuicklyReadOneBarcode("MyBinaryQR.png")
If BinaryData.SequenceEqual(MyReturnedData.BinaryValue) Then
	Console.WriteLine(vbTab & " Binary Data Read and Written Perfectly")
Else
	Throw New Exception("Corrupted Data")
End If
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yjl8xmdfxbz4ujjoe3l5.png)

In summary, the <a href="https://ironsoftware.com/csharp/barcode/use-case/barcode-library-csharp/" target="_blank">Barcode C# Library</a> 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 Iron BarCode.

**C#:**
```cs
using IronBarCode;
using System;
using System.Drawing;
//...
BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode("QR.png", BarcodeEncoding.QRCode);
if (Result != null)
{
    Console.WriteLine(QRResult.Value);
}
```
**VB:**
```vb
Imports IronBarCode
Imports System
Imports System.Drawing
'...
Private Result As BarcodeResult = BarcodeReader.QuicklyReadOneBarcode("QR.png", BarcodeEncoding.QRCode)
If Result IsNot Nothing Then
	Console.WriteLine(QRResult.Value)
End If
```
A more advanced form also exists with more developer control:

**C#:**
```cs
using IronBarCode;
using System;
using System.Drawing;
//...
BarcodeResult Result = BarcodeReader.ReadASingleBarcode("QR.png",  BarcodeEncoding.QRCode, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.None);
if (Result != null)
{
    Console.WriteLine(QRResult.Value);
}
```
**VB:**
```vb
Imports IronBarCode
Imports System
Imports System.Drawing
'...
Private Result As BarcodeResult = BarcodeReader.ReadASingleBarcode("QR.png", BarcodeEncoding.QRCode, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.None)
If Result IsNot Nothing Then
	Console.WriteLine(QRResult.Value)
End If
```
## Moving Forward

To learn more about this example and working with <a href"https://ironsoftware.com/csharp/barcode/use-case/qr-code-csharp/" target="_blank">QR does in C#</a>, we encourage you to fork it on out GitHub page or download the source code from our site.

### Source Code Downloads

Download this C# QR Code Generator Tutorial and DLL

- Github Repository
- Source Code Zip

### Further Documentation
You may also find the <a href="https://ironsoftware.com/csharp/barcode/object-reference/api/IronBarCode.QRCodeWriter.html" target=_blank">QRCodeWriter</a> and <a href="https://ironsoftware.com/csharp/barcode/object-reference/api/IronBarCode.BarcodeWriter.html" target="_blank">BarcodeReader</a> classes within the API Reference of interest.

They document the full feature set of Iron Barcode, a <a href="https://ironsoftware.com/csharp/barcode/use-case/vb-net-barcode/" target="_blank">VB .NET Barcode Generator</a>, which could not possibly be packed into a single tutorial





---