C# QR Code Generating Guide

Generating QR codes with C# is a practical solution for many common development tasks—such as encoding URLs, login tokens, or product information. With the help of a .NET-compatible barcode library, you can quickly create scannable QR images and integrate them into your applications or documents.

This tutorial shows how to build a simple yet powerful C# QR code generator: from generating a QR code from a string, embedding a logo, to inserting the image into PDF, Word, and Excel files. It also covers usage in both Windows and ASP.NET applications, using clean and reusable C# code examples.

Quick Navigation


1. How QR Code Generation Works in C#

Before diving into the code, it’s useful to understand what a QR code is. QR codes are 2D matrix barcodes that can encode URLs, text, contact info, and more. In C#, QR code generation involves three steps:

  1. Encode your content (usually a string)
  2. Configure QR code options (error correction, size, margin, etc.)
  3. Export the result as an image

In this tutorial, we use a simple .NET-compatible library Spire.Barcode for .NET to keep things straightforward and flexible.

Install Spire.Barcode for .NET via NuGet

PM> Install-Package Spire.Barcode

You can also choose Free Spire.Barcode for .NET for smaller tasks:

PM> Install-Package FreeSpire.Barcode

2. Generate QR Code from String in C#

Let’s start with a basic example: generating a QR code from a string in C#. Using the BarcodeSettings and BarCodeGenerator classes provided by Spire.Barcode for .NET, you can easily encode any text or URL into a scannable QR code.

The key steps include:

  • Defining QR code content and format using BarcodeSettings, including data string, error correction level, size, margin, and optional logo;
  • Generating the image via BarCodeGenerator, which produces a ready-to-use System.Drawing.Image object;
  • Saving or embedding the result in external documents or applications.

The following code demonstrates how to configure the QR code and export it as a PNG image.

using Spire.Barcode;
using System.Drawing;
using System.Drawing.Imaging;

namespace GenerateQRCode
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize barcode settings
            BarcodeSettings settings = new BarcodeSettings();
            // Specify the barcode type as QR Code
            settings.Type = BarCodeType.QRCode;
            // Set the QR code data
            settings.Data = "https://www.example.com/";
            // Set the text to display (optional)
            settings.Data2D = "Scan to go to example.com";
            settings.ShowTextOnBottom = true;
            settings.TextFont = new Font(FontFamily.GenericSansSerif, 16.0f);

            // Set data mode and error correction level
            settings.QRCodeDataMode = QRCodeDataMode.Auto;
            settings.QRCodeECL = QRCodeECL.H;
            // (Optional) Embed a logo image in the QR code
            settings.QRCodeLogoImage = Image.FromFile("Logo.png");
            // Set the width of bar module
            settings.X = 3.0f;

            // Generate the QR code image
            BarCodeGenerator generator = new BarCodeGenerator(settings);
            // Generate the QR code image
            Image qr = generator.GenerateImage();
            // Save the image to a file
            qr.Save("QR Code.png", ImageFormat.Png);
        }
    }
}

You can change the settings.Data to encode any text or URL. To embed a logo, use QRCodeLogoImage with a local image path. Error correction level H ensures that the QR code remains scannable even with a logo.

The QR code generated using C#:

QR Code Image Generated Using C#

You may also like: How to Scan QR Code Using C#


3. Insert QR Codes into Documents (PDF, Word, Excel)

A common use case for a c# qr generator is embedding QR codes into documents—automatically generating reports, receipts, or certificates. The generated QR image can be inserted into files using standard .NET Office libraries.

Insert into PDF

PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.AppendPage();

page.Canvas.DrawImage(PdfImage.FromImage(qr), 100, 400, 100, 100);

pdf.SaveToFile("output.pdf");

Use cases: digital invoices, shipping labels, authentication docs.

Insert into Word Document

Document doc = new Document();
Section section = doc.AddSection();
Paragraph para = section.AddParagraph();

DocPicture picture = para.AppendPicture(qr);

doc.SaveToFile("output.docx", FileFormat.Docx);

Use cases: contracts, personalized letters, ID cards.

Insert into Excel Worksheet

Workbook book = new Workbook();
Worksheet sheet = book.Worksheets[0];

sheet.Pictures.Add(2, 2, qr);  // Cell C3

book.SaveToFile("output.xlsx", ExcelVersion.Version2016);

Use cases: customer records, product sheets, logistics tracking.

Recommended article: Read Barcodes from PDF Documents


4. QR Code Generator in Windows and Web Applications

Windows Application

In a Windows Forms app, you can display the generated QR image in a PictureBox, and allow users to save it. This is useful for desktop-based systems like check-in kiosks or internal tools.

pictureBox1.Image = qr;

ASP.NET Web Application

In ASP.NET, return the QR image in a memory stream:

MemoryStream ms = new MemoryStream();
qr.Save(ms, ImageFormat.Png);
return File(ms.ToArray(), "image/png");

These approaches are lightweight and do not depend on heavy frameworks.


FAQ

Q: How to generate a QR code in .NET C# by code? A: Use a barcode library like Spire.Barcode for .NET. Simply define your QR code content, set the desired options (e.g., error correction level, logo image), and export it as an image. It's straightforward to integrate into C# applications.

Q: What is the best QR code generator for C#? A: There are several good options available. Spire.Barcode for .NET is a solid choice—it supports multiple barcode formats, allows logo insertion, and integrates well with Office documents.

Q: Can I adjust the size and layout of the generated QR code? A: Yes. You can modify properties like X, LeftMargin, and TopMargin to control the module size and spacing around the QR code.

Q: How can I use the generated QR code in documents or applications? A: The generated image can be embedded into PDFs, Word documents, Excel sheets, or displayed in Windows Forms or ASP.NET pages. This makes it useful in reports, labels, and web systems.


Conclusion

Creating a QR code generator in C# is simple yet highly effective. This tutorial demonstrated how to generate QR codes with a few lines of C# code, insert them into documents, and deploy them in both desktop and web environments. You can further expand on this by exploring batch generation, QR styling, or integrating scanning features in future enhancements.

To try it yourself, you can apply for a free temporary license here to unlock the full capabilities of Spire.Barcode for .NET.

How to create Code39 barcodes in C#

2017-06-12 07:45:38 Written by Koohji

Code39 is also known as Alpha39, Code 3 of 9, Code 3/9, Type 39, USS Code 39, USD-3. This article will show you how to use Spire.Barcode to create Code39 barcode. It supports 43 characters, consisting of uppercase letters (A through Z), numeric digits (0 through 9) and a number of special characters (*, -, $, /, +, %, and space). Usually Code39 starts and ends with “*”. Here comes to the steps of how to create Code39 barcodes.

Step 1: Create a BarcodeSettings instance.

BarcodeSettings bs = new BarcodeSettings();

Step 2: Set the BarcodeType property to Code39

bs.Type = BarCodeType.Code39;

Step 3: Set the data for the barcode.

bs.Data = "*ABC 12345* ";

Step 4: Generate barcode image using BarCodeGenerator.

BarCodeGenerator bg = new BarCodeGenerator(bs);
bg.GenerateImage().Save("Code39Code.png");    

Effective screenshot of Code39 barcode image:

How to create Code39 barcodes in C#

Full codes:

using Spire.Barcode;


namespace Code39
{
    class Program
    {
        static void Main(string[] args)
        {
            BarcodeSettings bs = new BarcodeSettings();

            bs.Type = BarCodeType.Code39;
            bs.Data = "*ABC 12345* ";


            BarCodeGenerator bg = new BarCodeGenerator(bs);

            bg.GenerateImage().Save("Code39Code.png");
            System.Diagnostics.Process.Start("Code39Code.png");
        }
    }
}

Convert PDF Page to SVG in C#/VB.NET

2017-06-09 06:41:39 Written by Koohji

Spire.PDF allows us to convert a PDF document, a single page or a range of pages in a PDF document to SVG file format. We have introduced how to convert a PDF document to SVG, in this tutorial, we are going to demonstrate how to convert a PDF page to SVG.

Below is the source PDF file we used in this example:

Convert PDF Page to SVG in C#, VB.NET

Code snippets:

Step 1: Instantiate an object of PdfDocument class and load the PDF document.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Test.pdf");

Step 2: Convert the first page of the PDF document to SVG using the SaveToFile(string filename, int startIndex, int endIndex, FileFormat fileFormat) method.

doc.SaveToFile("Result.svg", 0, 0, FileFormat.SVG);

The resultant SVG file looks as follows:

Convert PDF Page to SVG in C#, VB.NET

Full code:

[C#]
using Spire.Pdf;

namespace PDF_Page_to_SVG
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("Test.pdf");
            doc.SaveToFile("Result.svg", 0, 0, FileFormat.SVG);
        }
    }
}
[VB.NET]
Imports Spire.Pdf

Namespace PDF_Page_to_SVG
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New PdfDocument()
			doc.LoadFromFile("Test.pdf")
			doc.SaveToFile("Result.svg", 0, 0, FileFormat.SVG)
		End Sub
	End Class
End Namespace
page 200