DEV Community

Cover image for Generate QR Codes for Free in C#: A Step-by-Step Guide
David Au Yeung
David Au Yeung

Posted on

Generate QR Codes for Free in C#: A Step-by-Step Guide

Introduction

In this exercise, we will learn how to generate QR codes for free in C# using the QRCoder library. QR codes are widely adopted in modern applications for sharing links, payment information, authentication, and more.

With just a few lines of code, you can generate and save QR codes as image files on your local machine. This approach works with .NET 8/.NET 9 and is completely FREE, making it ideal for personal projects, demos, and enterprise solutions.

Step 1: Install the Required NuGet Package

Run the following command in your project directory:

dotnet add package QRCoder
Enter fullscreen mode Exit fullscreen mode

This installs the QRCoder library which provides multiple renderers (PNG, Bitmap, SVG, etc.).

Step 2: Create a QRCodeHelper Class

We'll create a helper class to encapsulate QR code generation. For cross-platform support, we’ll use the built-in PngByteQRCode renderer, which returns PNG bytes directly.

using System;
using System.IO;
using QRCoder;

namespace MyPlaygroundApp.Util
{
    public static class QRCodeHelper
    {
        public static void GenerateToFile(string text, string filePath, int pixelsPerModule = 20)
        {
            using var qrGenerator = new QRCodeGenerator();
            using var qrData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.Q);
            var qrCode = new PngByteQRCode(qrData);

            // Get PNG as byte array
            byte[] pngBytes = qrCode.GetGraphic(pixelsPerModule);

            // Save to file
            File.WriteAllBytes(filePath, pngBytes);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Implement the Client Code

Now, let’s call our helper from Main() to generate a QR code for a URL:

using MyPlaygroundApp.Utils;

class Program
{
    static void Main(string[] args)
    {
        string url = "https://dev.to";

        string file = @"C:\Users\User\Downloads\devto.png";

        QRCodeHelper.GenerateToFile(url, file);

        Console.WriteLine($"QR code saved to {file}");
    }
}
Enter fullscreen mode Exit fullscreen mode

When you run the program, a QR code image will be saved in the QRCodes folder relative to your project output.

Step 4: Run and See the Result

Build and run your project:

dotnet run
Enter fullscreen mode Exit fullscreen mode

Check the Downloads folder, you'll see a qrcode.png file.

Open it with any image viewer or scan it with your phone’s camera. It should redirect you to https://dev.to.

QR Code Example:

Summary

  • We used QRCoder, a free and open-source library, to generate QR codes in C#.
  • Created a reusable QRCodeHelper class.
  • Generated and saved a QR code as a PNG file.
  • With this setup, you can easily extend the helper to generate QR codes for Wi-Fi credentials, payment links, or authentication tokens.

Reference

QRCoder

Love C#!

Top comments (0)