How to Create Barcodes as HTML in C# 1

In this tutorial, we explore how to use the Iron Barcode library to generate and manage QR codes. After setting up the library, we start by importing the Iron Barcode namespace, which provides necessary methods for barcode operations. The tutorial demonstrates creating a QR code that encodes a URL using the CreateBarcode method. This method allows specifying the encoding type, such as QR code, and generates the corresponding code. Once generated, the QR code can be saved as an HTML file, making it accessible through any web browser. Additionally, we explore generating a data URL, which is a Base64 encoded string representing the QR code, useful for embedding directly into web pages without needing external files. Another approach involves using the ToHtmlTag method to get an HTML representation of the QR code. Running the project, we observe the console output showcasing the data URL and HTML tag representation. These methods offer a compact and efficient way to integrate QR codes into web applications, with easy sharing and display options. The tutorial concludes by encouraging viewers to subscribe for more tips from Iron Software and to explore their trial subscription for firsthand experience with the software.

Here is an illustrative C# example using the Iron Barcode library:

using System;
using IronBarCode;

class Program
{
    static void Main(string[] args)
    {
        // Creating a QR code that encodes a URL
        var url = "https://example.com";

        // Generate the QR code
        GeneratedBarcode qrCode = BarcodeWriter.CreateBarcode(url, BarcodeWriterEncoding.QRCode);

        // Saving the QR code as an HTML file
        qrCode.SaveAsHtmlFile("QRCode.html");
        Console.WriteLine("QR Code saved as HTML file: QRCode.html");

        // Generating and displaying the data URL (Base64 representation)
        string dataUrl = qrCode.ToDataUrl();
        Console.WriteLine("QR Code Data URL: " + dataUrl);

        // Generating and displaying the HTML tag representation
        string htmlTag = qrCode.ToHtmlTag();
        Console.WriteLine("QR Code HTML Tag: " + htmlTag);
    }
}
using System;
using IronBarCode;

class Program
{
    static void Main(string[] args)
    {
        // Creating a QR code that encodes a URL
        var url = "https://example.com";

        // Generate the QR code
        GeneratedBarcode qrCode = BarcodeWriter.CreateBarcode(url, BarcodeWriterEncoding.QRCode);

        // Saving the QR code as an HTML file
        qrCode.SaveAsHtmlFile("QRCode.html");
        Console.WriteLine("QR Code saved as HTML file: QRCode.html");

        // Generating and displaying the data URL (Base64 representation)
        string dataUrl = qrCode.ToDataUrl();
        Console.WriteLine("QR Code Data URL: " + dataUrl);

        // Generating and displaying the HTML tag representation
        string htmlTag = qrCode.ToHtmlTag();
        Console.WriteLine("QR Code HTML Tag: " + htmlTag);
    }
}
Imports System
Imports IronBarCode

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Creating a QR code that encodes a URL
		Dim url = "https://example.com"

		' Generate the QR code
		Dim qrCode As GeneratedBarcode = BarcodeWriter.CreateBarcode(url, BarcodeWriterEncoding.QRCode)

		' Saving the QR code as an HTML file
		qrCode.SaveAsHtmlFile("QRCode.html")
		Console.WriteLine("QR Code saved as HTML file: QRCode.html")

		' Generating and displaying the data URL (Base64 representation)
		Dim dataUrl As String = qrCode.ToDataUrl()
		Console.WriteLine("QR Code Data URL: " & dataUrl)

		' Generating and displaying the HTML tag representation
		Dim htmlTag As String = qrCode.ToHtmlTag()
		Console.WriteLine("QR Code HTML Tag: " & htmlTag)
	End Sub
End Class
$vbLabelText   $csharpLabel

Key Points

  1. Import the Iron Barcode Namespace: Ensure you have imported the IronBarCode namespace to access the necessary classes and methods.

  2. Create and Configure Barcode: Use the BarcodeWriter.CreateBarcode method to generate a QR code with a specified encoding type.

  3. Save as HTML: Save the generated QR code to an HTML file with SaveAsHtmlFile for easy web browser access.

  4. Data URL and HTML Tag: Use ToDataUrl and ToHtmlTag for convenient representations, which can be embedded directly into web applications.

  5. Console Output: The program prints the data URL and HTML tag to the console, showcasing different ways to present the QR code.

Further Reading: How to Export Barcodes as HTML

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 Barcodes as PDFs in C#
NEXT >
How to Create and Stamp Barcodes in PDF Documents Using IronBarcode

Report an Issue