How to Generate QR codes and Barcodes in C# for .NET 5 using Iron Barcode

In this tutorial, you'll learn how to generate barcodes and QR codes in C# using Iron Barcode within a .NET framework. The process begins with installing the Iron Barcode library via the NuGet package manager. You can use either the console or the graphical user interface to do so. Once installed, including the Iron Barcode namespace is essential. The guide walks you through writing a single line of code to create a barcode, illustrating how to encode data such as a product ID or website link. The tutorial explains saving the generated barcode in various formats, including PNG and HTML. It also covers how to generate a QR code by altering the encoding scheme and demonstrates adding annotations like text above or below the barcode or QR code. The tutorial concludes by showing how to access the saved barcode file in your project directory, providing a straightforward solution for integrating barcode and QR code generation into your C# applications.

Here is the corrected and formatted code snippet to generate a barcode and QR code using Iron Barcode:

// Install IronBarcode from NuGet package manager before using this code.
// Use the following command in the Package Manager Console: Install-Package IronBarCode

using IronBarCode; // Import the IronBarcode namespace

class BarcodeExample
{
    static void Main(string[] args)
    {
        // Creating a Barcode
        // "123456" is the data to be encoded in the barcode.
        BarcodeWriter.CreateBarcode("123456", BarcodeEncoding.Code128)
                     .SaveAsPng("barcode.png");

        // Creating a QR Code
        // "https://example.com" is the data to be encoded in the QR code.
        var qrCode = BarcodeWriter.CreateQRCode("https://example.com");
        qrCode.SaveAsPng("qrcode.png");

        // Adding annotations to the QR Code
        qrCode.AddAnnotationTextAboveBarcode("Visit our Website")
              .AddAnnotationTextBelowBarcode("Scan the QR code for more info.")
              .SaveAsPng("annotated_qrcode.png");

        // Information
        // The data will be saved in the default project directory unless a different path is specified.
        System.Console.WriteLine("Barcodes have been generated and saved successfully.");
    }
}
// Install IronBarcode from NuGet package manager before using this code.
// Use the following command in the Package Manager Console: Install-Package IronBarCode

using IronBarCode; // Import the IronBarcode namespace

class BarcodeExample
{
    static void Main(string[] args)
    {
        // Creating a Barcode
        // "123456" is the data to be encoded in the barcode.
        BarcodeWriter.CreateBarcode("123456", BarcodeEncoding.Code128)
                     .SaveAsPng("barcode.png");

        // Creating a QR Code
        // "https://example.com" is the data to be encoded in the QR code.
        var qrCode = BarcodeWriter.CreateQRCode("https://example.com");
        qrCode.SaveAsPng("qrcode.png");

        // Adding annotations to the QR Code
        qrCode.AddAnnotationTextAboveBarcode("Visit our Website")
              .AddAnnotationTextBelowBarcode("Scan the QR code for more info.")
              .SaveAsPng("annotated_qrcode.png");

        // Information
        // The data will be saved in the default project directory unless a different path is specified.
        System.Console.WriteLine("Barcodes have been generated and saved successfully.");
    }
}
' Install IronBarcode from NuGet package manager before using this code.
' Use the following command in the Package Manager Console: Install-Package IronBarCode

Imports IronBarCode ' Import the IronBarcode namespace

Friend Class BarcodeExample
	Shared Sub Main(ByVal args() As String)
		' Creating a Barcode
		' "123456" is the data to be encoded in the barcode.
		BarcodeWriter.CreateBarcode("123456", BarcodeEncoding.Code128).SaveAsPng("barcode.png")

		' Creating a QR Code
		' "https://example.com" is the data to be encoded in the QR code.
		Dim qrCode = BarcodeWriter.CreateQRCode("https://example.com")
		qrCode.SaveAsPng("qrcode.png")

		' Adding annotations to the QR Code
		qrCode.AddAnnotationTextAboveBarcode("Visit our Website").AddAnnotationTextBelowBarcode("Scan the QR code for more info.").SaveAsPng("annotated_qrcode.png")

		' Information
		' The data will be saved in the default project directory unless a different path is specified.
		System.Console.WriteLine("Barcodes have been generated and saved successfully.")
	End Sub
End Class
$vbLabelText   $csharpLabel

Further Reading: C# QR Code Generator for .NET

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he says it’s one of his favorite aspects of working with Iron Software. Jordi grew up in Miami, Florida and studied Computer Science and Statistics at University of Florida.
< PREVIOUS
How to Create and Stamp Barcodes in PDF Documents Using IronBarcode
NEXT >
How to use a Barcode Scanner in a C# Windows Application