How to write QR codes in C

by Curtis Chau

Introduction

With IronQR, developers can create QR codes for popular image formats and customize them with background colors, margins, logos, and even add them to PDFs. For advanced use, it also offers control over error correction and versions.

This article will explore key features of IronQR with examples, helping you understand how to use it for writing QR codes in C# and apply it effectively in your projects.

Table of Contents

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

First Step:
green arrow pointer

Input Data

Text, URLs, Numbers

IronQR can convert a wide range of data types, including text, URLs, and numbers, into QR codes. Whether you're creating QR code links or text for marketing and communication, numeric codes for inventory management, or encoding binary data or streams into readable QR codes, IronQR provides all the support you need.

Additionally, the API is straightforward. The QrWriter class offers several overloads, enabling different types of data as input, reducing complexity and streamlining the process.

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-1.cs
using IronQr;
using IronSoftware.Drawing;

string text = "Hello, World!";
string url = "https://ironsoftware.com/csharp/qr/";
string alphanumeric = "WATERSKU-12356";

// Create QR code
QrCode textQr = QrWriter.Write(text);
// Save QR code as a bitmap
AnyBitmap textQrImage = textQr.Save();
// Save QR code as file
textQrImage.SaveAs("textQr.png");

QrCode urlQr = QrWriter.Write(url);
AnyBitmap urlQrImage = urlQr.Save();
urlQrImage.SaveAs("urlQr.png");

QrCode alphanumericQr = QrWriter.Write(alphanumeric);
AnyBitmap alphanumericQrImage = alphanumericQr.Save();
alphanumericQrImage.SaveAs("alphanumericQr.png");
Imports IronQr
Imports IronSoftware.Drawing

Private text As String = "Hello, World!"
Private url As String = "https://ironsoftware.com/csharp/qr/"
Private alphanumeric As String = "WATERSKU-12356"

' Create QR code
Private textQr As QrCode = QrWriter.Write(text)
' Save QR code as a bitmap
Private textQrImage As AnyBitmap = textQr.Save()
' Save QR code as file
textQrImage.SaveAs("textQr.png")

Dim urlQr As QrCode = QrWriter.Write(url)
Dim urlQrImage As AnyBitmap = urlQr.Save()
urlQrImage.SaveAs("urlQr.png")

Dim alphanumericQr As QrCode = QrWriter.Write(alphanumeric)
Dim alphanumericQrImage As AnyBitmap = alphanumericQr.Save()
alphanumericQrImage.SaveAs("alphanumericQr.png")
VB   C#

Binary & Streams

Similarly, we can convert binary data and streams into QR codes using the same Write method as mentioned earlier.

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-2.cs
using IronQr;
using IronSoftware.Drawing;
using System.Text;

byte[] bytes = Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/qr/");

// Create QR code
QrCode bytesQr = QrWriter.Write(bytes);

// Save QR code as a bitmap
AnyBitmap qrImage = bytesQr.Save();

// Save QR code bitmap to file
qrImage.SaveAs("bytesQr.png");
Imports IronQr
Imports IronSoftware.Drawing
Imports System.Text

Private bytes() As Byte = Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/qr/")

' Create QR code
Private bytesQr As QrCode = QrWriter.Write(bytes)

' Save QR code as a bitmap
Private qrImage As AnyBitmap = bytesQr.Save()

' Save QR code bitmap to file
qrImage.SaveAs("bytesQr.png")
VB   C#

The Write method has overloads that accept both byte arrays and streams as inputs. For streams, we can create a MemoryStream from the byte array and then convert it into a QR code. This is useful when users require more fine-grained control over data chunks, as streams can be more memory-efficient.

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-3.cs
using IronQr;
using IronSoftware.Drawing;
using System.IO;
using System.Text;

MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/qr/"));

// Create QR code
QrCode streamQr = QrWriter.Write(stream);

// Save QR code as a bitmap
AnyBitmap streamQrImage = streamQr.Save();

// Save QR code bitmap as file
streamQrImage.SaveAs("streamQr.png");
Imports IronQr
Imports IronSoftware.Drawing
Imports System.IO
Imports System.Text

Private stream As New MemoryStream(Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/qr/"))

' Create QR code
Private streamQr As QrCode = QrWriter.Write(stream)

' Save QR code as a bitmap
Private streamQrImage As AnyBitmap = streamQr.Save()

' Save QR code bitmap as file
streamQrImage.SaveAs("streamQr.png")
VB   C#

Export QR Codes

IronQR is flexible and adaptable to various use cases that require different file formats. You can save QR codes in multiple formats such as JPG, PNG, GIF, and TIFF using the SaveAs method.

Save as Image

The SaveAs method from AnyBitmap automatically detects the file format based on the provided file path. In this example, I specified a file path ending with .png.

Please note
When using the SaveAs method, please note that there is no default image format. If you enter an unrecognized extension or make a typo in the file path, the image will be saved with the incorrect extension.

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-4.cs
using IronQr;
using IronSoftware.Drawing;

// Create a QR code object
QrCode qr = QrWriter.Write("hello world");

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save();

// Save QR code bitmap as file
qrImage.SaveAs("qr.png");
Imports IronQr
Imports IronSoftware.Drawing

' Create a QR code object
Private qr As QrCode = QrWriter.Write("hello world")

' Save QR code as a bitmap
Private qrImage As AnyBitmap = qr.Save()

' Save QR code bitmap as file
qrImage.SaveAs("qr.png")
VB   C#

System.Drawing.Images

Converting images to the System.Drawing.Images object from Microsoft allows you to use the Bitmap class to save the QR code to a file path. In this example, the Save method saves the QR code as a PNG file to the path 'qrBitmap.png.'

Please note
Note that System.Drawing.Common is only supported on the Windows platform.

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-5.cs
using IronQr;
using System.Drawing;

// Create a QR code object
QrCode qr = QrWriter.Write("hello world");

// Save QR code as a bitmap
Bitmap qrImage = qr.Save();

// Save QR code bitmap as file
qrImage.Save("qrBitmap.png");
Imports IronQr
Imports System.Drawing

' Create a QR code object
Private qr As QrCode = QrWriter.Write("hello world")

' Save QR code as a bitmap
Private qrImage As Bitmap = qr.Save()

' Save QR code bitmap as file
qrImage.Save("qrBitmap.png")
VB   C#

IronSoftware.Drawing

Due to the lack of cross-platform compatibility of System.Drawing.Common, developers may encounter issues when maintaining cross-platform applications. IronQR can utilize both System.Drawing.Common and IronSoftware.Drawing.

IronQR uses the AnyBitmap class from IronSoftware.Drawing, a universally compatible Bitmap class that implicitly casts to the following:

  • System.Drawing.Bitmap
  • System.Drawing.Image
  • SkiaSharp.SKBitmap
  • SixLabors.ImageSharp
  • Microsoft.Maui.Graphics.Platform.PlatformImage

With this powerful open-source library, IronQR achieves cross-platform support and compatibility with .NET 8, .NET 7, .NET 6, .NET 5, .NET Core, .NET Standard, and .NET Framework 4.6.2+. To learn more about the library, please refer to IronSoftware.Drawing website.

Stamp on PDF

IronQR allows developers to stamp QR codes onto existing PDF documents, making it easy for others to quickly access links or additional resources. Stamping QR codes on both single and multiple pages is supported.

Stamp to a Single Page

After creating the QR code, call the StampToExistingPdfPage method from the QrCode object. This method requires the file path, coordinates (x and y), page number, and an optional password if the PDF is password-protected. Once the arguments are provided, the method stamps the QR code and saves the PDF.

Please note
This method is based on the PDF pages, with page numbering starting at 1 instead of 0.

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-6.cs
using IronQr;

// Create a QR code object
QrCode qr = QrWriter.Write("hello world");

string filepath = "example.pdf";
int x = 100;
int y = 150;
int page = 1;

// Stamp QR code to (100, 150) of the pdf on page 1
qr.StampToExistingPdfPage(filepath, x, y, page);
Imports IronQr

' Create a QR code object
Private qr As QrCode = QrWriter.Write("hello world")

Private filepath As String = "example.pdf"
Private x As Integer = 100
Private y As Integer = 150
Private page As Integer = 1

' Stamp QR code to (100, 150) of the pdf on page 1
qr.StampToExistingPdfPage(filepath, x, y, page)
VB   C#

Stamp to Multiple Pages

Similar to the example above, the main difference is that the StampToExistingPdfPages method takes a list of page numbers instead of just one.

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-7.cs
using IronQr;
using System.Collections.Generic;

// Create a QR code object
QrCode qr = QrWriter.Write("hello world");

string filepath = "example.pdf";
int x = 100;
int y = 150;
List<int> pages = new List<int>();
pages.Add(1);
pages.Add(2);
pages.Add(3);
pages.Add(4);

// Stamp QR code to (100, 150) of the pdf on pages 1-4
qr.StampToExistingPdfPages(filepath, x, y, pages);
Imports IronQr
Imports System.Collections.Generic

' Create a QR code object
Private qr As QrCode = QrWriter.Write("hello world")

Private filepath As String = "example.pdf"
Private x As Integer = 100
Private y As Integer = 150
Private pages As New List(Of Integer)()
pages.Add(1)
pages.Add(2)
pages.Add(3)
pages.Add(4)

' Stamp QR code to (100, 150) of the pdf on pages 1-4
qr.StampToExistingPdfPages(filepath, x, y, pages)
VB   C#

Output from Both Examples

alt text


QR Code Options

IronQR offers extensive customization options for fine-tuning QR code behavior and functionality. The QrOptions class provides several parameters, such as version control, encoding type, character encoding, and error correction levels. Let’s explore these options in more detail.

Encoding

IronQR supports multiple types of QR codes for both creation and reading. Below are the supported types:

  • QRCode: This is the standard QR code, commonly used today. It can store up to 7,089 numeric characters or 4,296 alphanumeric characters.
  • MicroQRCode: A smaller version of the standard QR code, it can store up to 35 numeric characters or 21 alphanumeric characters.
  • RMQRCode: The Rectangular Micro QR Code is a compact version of the QR code, offering flexibility in its aspect ratio.
:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-8.cs
using IronQr;
using IronSoftware.Drawing;

QrOptions options = new QrOptions
{
    // Change encoding to micro QR code
    Encoding = IronQr.Enum.QrEncoding.MicroQRCode,
};

// Create QR code
QrCode qr = QrWriter.Write("1234", options);

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save();

// Save QR code bitmap as file
qrImage.SaveAs("qrImage.png");
Imports IronQr
Imports IronSoftware.Drawing

Private options As New QrOptions With {.Encoding = IronQr.Enum.QrEncoding.MicroQRCode}

' Create QR code
Private qr As QrCode = QrWriter.Write("1234", options)

' Save QR code as a bitmap
Private qrImage As AnyBitmap = qr.Save()

' Save QR code bitmap as file
qrImage.SaveAs("qrImage.png")
VB   C#

Error Correction

IronQR uses standard QR error correction to ensure that all QR codes produced are fault-tolerant and reliable, even in harsh conditions. Additionally, IronQR allows you total control over error correction level for further fine-tuning.

There are four levels of error correction available, provided by QrErrorCorrectionLevel:

  • Highest: 30% error correction
  • High: 25% error correction
  • Medium: 15% error correction
  • Low: 7% error correction
:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-9.cs
using IronQr;
using IronSoftware.Drawing;

QrOptions options = new QrOptions
{
    // Change error correction level to medium
    ErrorCorrectionLevel = QrErrorCorrectionLevel.Medium,
};

// Create QR code
QrCode qr = QrWriter.Write("1234", options);

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save();

// Save QR code bitmap as file
qrImage.SaveAs("qrImage.png");
Imports IronQr
Imports IronSoftware.Drawing

Private options As New QrOptions With {.ErrorCorrectionLevel = QrErrorCorrectionLevel.Medium}

' Create QR code
Private qr As QrCode = QrWriter.Write("1234", options)

' Save QR code as a bitmap
Private qrImage As AnyBitmap = qr.Save()

' Save QR code bitmap as file
qrImage.SaveAs("qrImage.png")
VB   C#

Higher error correction provides greater fault tolerance when reading the QR code, making it more likely to be scanned at lower resolutions compared to one with low error correction. You may test out based on your use cases.

alt text

QR Code Version

You can adjust the QR code version to store more data. Higher versions are ideal for inventory or logistics, while lower versions work well for smaller data, like short URLs. Simply change the Version property in the QrOptions object and pass it to the Write method to generate the QR code as needed.

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-10.cs
using IronQr;
using IronSoftware.Drawing;

QrOptions options = new QrOptions
{
    // Change QR code version to 40
    Version = 40,
};

// Create QR code
QrCode qr = QrWriter.Write("1234", options);

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save();

// Save QR code bitmap as file
qrImage.SaveAs("qrImage.png");
Imports IronQr
Imports IronSoftware.Drawing

Private options As New QrOptions With {.Version = 40}

' Create QR code
Private qr As QrCode = QrWriter.Write("1234", options)

' Save QR code as a bitmap
Private qrImage As AnyBitmap = qr.Save()

' Save QR code bitmap as file
qrImage.SaveAs("qrImage.png")
VB   C#

alt text

As you can see from the output, version 40 of the QR code is highly complex and dense compared to version 5.

Lower versions require more precise scanning and may be difficult to scan without higher-resolution scanners. However, higher versions are easier to scan, even with lower-resolution cameras. For a more detailed guide on choosing the QR version based on capacity, please refer to the QR version list.

Character Encoding

This option determines how the QR code is encoded. In our example, we changed it to 'UTF-32', while the default character encoding is 'ISO-8859-1.'

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-11.cs
using IronQr;
using IronSoftware.Drawing;

QrOptions options = new QrOptions
{
    // Change character encoding to UTF-32
    CharacterEncoding = "UTF-32"
};

// Create QR code
QrCode qr = QrWriter.Write("1234", options);

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save();

// Save QR code bitmap as file
qrImage.SaveAs("qrImage.png");
Imports IronQr
Imports IronSoftware.Drawing

Private options As New QrOptions With {.CharacterEncoding = "UTF-32"}

' Create QR code
Private qr As QrCode = QrWriter.Write("1234", options)

' Save QR code as a bitmap
Private qrImage As AnyBitmap = qr.Save()

' Save QR code bitmap as file
qrImage.SaveAs("qrImage.png")
VB   C#

QR Code Styling

In addition to its easy-to-use methods and flexibility in handling input data, IronQR offers many options for customizing and styling QR codes to make them unique. The QrStyleOptions class provides various parameters for customizing all aspects of a QR code. Let’s explore the available options.

Resize

To resize the QR code, you can set the Dimensions property of the QrStyleOptions object and then pass it to the Save method. By default, the QR code is saved at 300px. In this example, we saved the QR code at 600px instead of 300px.

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-12.cs
using IronQr;
using IronSoftware.Drawing;

QrStyleOptions styleOptions = new QrStyleOptions()
{
    // Change the dimensions to 600px
    Dimensions = 600,
};

string url = "https://ironsoftware.com/csharp/qr/";

// Create QR code
QrCode qr = QrWriter.Write(url);

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save(styleOptions);

// Save QR code bitmap as file
qrImage.SaveAs("qrURLResized.png");
Imports IronQr
Imports IronSoftware.Drawing

Private styleOptions As New QrStyleOptions() With {.Dimensions = 600}

Private url As String = "https://ironsoftware.com/csharp/qr/"

' Create QR code
Private qr As QrCode = QrWriter.Write(url)

' Save QR code as a bitmap
Private qrImage As AnyBitmap = qr.Save(styleOptions)

' Save QR code bitmap as file
qrImage.SaveAs("qrURLResized.png")
VB   C#

alt text

Margins & Borders

To adjust the margins and borders, we can use the Margins property of the QrStyleOptions class. This property controls the margins of the QR code on all sides, with a default value of 10px. In our example, we set the margin to 20px.

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-13.cs
using IronQr;
using IronSoftware.Drawing;

QrStyleOptions styleOptions = new QrStyleOptions()
{
    // Change margins to 20px
    Margins = 20
};

string url = "https://ironsoftware.com/csharp/qr/";

// Create QR code
QrCode qr = QrWriter.Write(url);

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save(styleOptions);

// Save QR code bitmap as file
qrImage.SaveAs("qrURLMarginMultiple.png");
Imports IronQr
Imports IronSoftware.Drawing

Private styleOptions As New QrStyleOptions() With {.Margins = 20}

Private url As String = "https://ironsoftware.com/csharp/qr/"

' Create QR code
Private qr As QrCode = QrWriter.Write(url)

' Save QR code as a bitmap
Private qrImage As AnyBitmap = qr.Save(styleOptions)

' Save QR code bitmap as file
qrImage.SaveAs("qrURLMarginMultiple.png")
VB   C#

alt text

Change Margins for Each Side

IronQR also allows users to specify different margins for each side, providing more fine-grained control.

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-14.cs
using IronQr;
using IronSoftware.Drawing;

QrStyleOptions styleOptions = new QrStyleOptions()
{
    // Change margins
    MarginBottom = 30,
    MarginTop = 100,
    MarginRight = 40,
    MarginLeft = 20,
};

string url = "https://ironsoftware.com/csharp/qr/";

// Create QR code
QrCode qr = QrWriter.Write(url);

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save(styleOptions);

// Save QR code bitmap as file
qrImage.SaveAs("qrURLMarginMultiple.png");
Imports IronQr
Imports IronSoftware.Drawing

Private styleOptions As New QrStyleOptions() With {
	.MarginBottom = 30,
	.MarginTop = 100,
	.MarginRight = 40,
	.MarginLeft = 20
}

Private url As String = "https://ironsoftware.com/csharp/qr/"

' Create QR code
Private qr As QrCode = QrWriter.Write(url)

' Save QR code as a bitmap
Private qrImage As AnyBitmap = qr.Save(styleOptions)

' Save QR code bitmap as file
qrImage.SaveAs("qrURLMarginMultiple.png")
VB   C#

Recolor

We can add colors to the QR code and its background using the QrStyleOptions class. Customizing colors makes the QR code more unique and eye-catching. You can change the color using the Color and BackgroundColor properties. Be sure to import IronSoftware.Drawing for a list of available colors to assign.

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-15.cs
using IronQr;
using IronSoftware.Drawing;

QrStyleOptions styleOptions = new QrStyleOptions()
{
    // Change color
    BackgroundColor = Color.Blue,
    Color = Color.Red
};

string url = "https://ironsoftware.com/csharp/qr/";

// Create QR code
QrCode qr = QrWriter.Write(url);

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save(styleOptions);

// Save QR code bitmap as file
qrImage.SaveAs("qrURLColored.png");
Imports IronQr
Imports IronSoftware.Drawing

Private styleOptions As New QrStyleOptions() With {
	.BackgroundColor = Color.Blue,
	.Color = Color.Red
}

Private url As String = "https://ironsoftware.com/csharp/qr/"

' Create QR code
Private qr As QrCode = QrWriter.Write(url)

' Save QR code as a bitmap
Private qrImage As AnyBitmap = qr.Save(styleOptions)

' Save QR code bitmap as file
qrImage.SaveAs("qrURLColored.png")
VB   C#

alt text

In addition to colors and dimensions, you can also apply your company logo to the QR code. This helps users immediately recognize and associate the QR code with your brand. The Logo property makes it easy to customize a QR code by adding your company’s logo.

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-15.cs
using IronQr;
using IronSoftware.Drawing;

QrStyleOptions styleOptions = new QrStyleOptions()
{
    // Change color
    BackgroundColor = Color.Blue,
    Color = Color.Red
};

string url = "https://ironsoftware.com/csharp/qr/";

// Create QR code
QrCode qr = QrWriter.Write(url);

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save(styleOptions);

// Save QR code bitmap as file
qrImage.SaveAs("qrURLColored.png");
Imports IronQr
Imports IronSoftware.Drawing

Private styleOptions As New QrStyleOptions() With {
	.BackgroundColor = Color.Blue,
	.Color = Color.Red
}

Private url As String = "https://ironsoftware.com/csharp/qr/"

' Create QR code
Private qr As QrCode = QrWriter.Write(url)

' Save QR code as a bitmap
Private qrImage As AnyBitmap = qr.Save(styleOptions)

' Save QR code bitmap as file
qrImage.SaveAs("qrURLColored.png")
VB   C#

alt text

The QrLogo class allows further customization of the logo's appearance. Below are the available properties:

  • Bitmap: Represents the image you want to use as the logo.
  • Width: Represents the logo’s width. The default value is 0.
  • Height: Represents the logo’s height. The default value is 0.
  • CornerRadius: Represents the radius for rounding the logo’s corners. By default, it's set to 0, meaning the logo will have square corners.

Checking Fault Tolerance

Along with extensive flexibility in file formats and customizations, the flexibility extends to debugging and error-handling aspects. IronQR provides various tools for developers to handle exceptions and write unit tests to verify applications.

CheckSums

QR codes may sometimes become damaged, but IronQR includes built-in checksums and data correction to keep them functional. It uses the Reed-Solomon error correction algorithm, ensuring that QR codes remain fault-tolerant.

Detailed Error Messages

IronQR provides detailed error messages that help users quickly identify issues. These messages contain a list of specific exceptions, making debugging and problem-solving more straightforward. Below is a list of IronQrException used by the library.

  • IronQrEncodingException: A subclass of IronQrException, this error occurs when there is an issue with writing the QR code. For instance, it will appear if a user attempts to create a QR code from an empty string.

alt text

  • IronQrFileException: A subclass of IronQrException, this error occurs when a file-related issue arises.

  • IronQrPdfPasswordExcception: A subclass of IronQrException, this error occurs when the PDF a user is trying to stamp is password-protected, and either no password or an incorrect password is provided. It also covers other PDF-related errors, such as when the PDF cannot be opened, as shown in the example.

alt text

Conclusion

IronQR provides a comprehensive set of methods for generating and customizing QR codes within .NET applications. With its robust features, developers can easily create QR codes with various data encodings, visual styles, and error correction levels. The library's support for diverse output formats and seamless integration into existing documents makes it a versatile tool for any QR code project. Whether you need basic QR codes or advanced, branded solutions, IronQR offers the flexibility and functionality to meet your needs efficiently.

To learn more, check out the IronQR documentation, start exploring with a free trial, and review the licensing options to see which plan best suits your needs.