Get started with IronBarcode

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

First Step:
green arrow pointer

Export Barcode as Data URL

Before discussing how to export a barcode as a Data URL, we need to first know what exactly is a data URL. Data URL, also known as Data URI, is a type of Uniform Resource Identifier (URI) that allows data to be embedded directly in the URL string, inline in web pages as if they were external resources. This can be in any format, which includes text, images, audio, video, and binary data. The obtained Data URL can later be used in HTML inside an image tag as a src attribute. Here's a code snippet demonstrating how we can convert our GeneratedBarcode into a Data URL.

:path=/static-assets/barcode/content-code-examples/how-to/ExportBarcodeAsDataUrl.cs
// Import necessary namespaces for the program
using IronBarCode; // This library is used to create and work with barcodes
using System;

// Generate a QR code from a URL using the IronBarCode library
// Set up and generate a barcode
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode(
    "https://ironsoftware.com/csharp/barcode/", // The URL to be encoded into the QR code
    BarcodeEncoding.QRCode // Use QR code encoding
);

// Convert the generated barcode to a data URL format, which can be used in HTML or similar applications
var dataUrl = myBarcode.ToDataUrl();

// Print the data URL to the console to allow users to view or use the QR code
Console.WriteLine(dataUrl);
' Import necessary namespaces for the program

Imports IronBarCode ' This library is used to create and work with barcodes

Imports System



' Generate a QR code from a URL using the IronBarCode library

' Set up and generate a barcode

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode)



' Convert the generated barcode to a data URL format, which can be used in HTML or similar applications

Private dataUrl = myBarcode.ToDataUrl()



' Print the data URL to the console to allow users to view or use the QR code

Console.WriteLine(dataUrl)
$vbLabelText   $csharpLabel

From the code snippet above, we started off with creating a barcode using the CreateBarcode() method from the BarcodeWriter class with the barcode value and the barcode encoding as the arguments for this method. In order to get the Data URL of the barcode, we attached the ToDataUrl() method to the GeneratedBarcode.

Export Barcode as HTML Tag

Another way to export our GeneratedBarcode to HTML is by exporting it into an HTML tag using the ToHtmlTag() method. This method renders the GeneratedBarcode object as a fully formed HTML tag that can be injected directly into HTML without having to reference it in any JavaScript, CSS, or image dependencies. The following code snippet demonstrates how to export as an HTML tag.

:path=/static-assets/barcode/content-code-examples/how-to/ExportBarcodeAsHtmlTag.cs
// Import necessary namespaces
using IronBarCode;
using System;

// Generate a QR code barcode for the specified URL
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode(
    "https://ironsoftware.com/csharp/barcode/", // URL for which QR code is generated
    BarcodeEncoding.QRCode                     // Specify QRCode as the encoding type
);

// Convert the generated barcode to an HTML tag
string htmlTag = myBarcode.ToHtmlTag();

// Print the HTML image tag representation of the barcode to the console
Console.WriteLine(htmlTag);
' Import necessary namespaces

Imports IronBarCode

Imports System



' Generate a QR code barcode for the specified URL

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode)



' Convert the generated barcode to an HTML tag

Private htmlTag As String = myBarcode.ToHtmlTag()



' Print the HTML image tag representation of the barcode to the console

Console.WriteLine(htmlTag)
$vbLabelText   $csharpLabel

From the code snippet above, we can simply attach the ToHtmlTag() method to the GeneratedBarcode in order to obtain the HTML tag of the barcode generated. This HTML tag can directly be embedded into a larger HTML file.

Export Barcode as HTML File

Users can also opt to save the GeneratedBarcode as an HTML file instead. To do this, use the SaveAsHtmlFile() method. The following code snippet demonstrates how to use this method.

:path=/static-assets/barcode/content-code-examples/how-to/ExportBarcodeAsHtmlFile.cs
using IronBarCode;

// Generate a QR code barcode with a specific URL as content.
// The BarcodeWriter.CreateBarcode method takes two parameters:
// 1. The content to be encoded as a QR code (in this case, a URL).
// 2. The encoding type, specified as BarcodeEncoding.QRCode, indicating that a QR code should be generated.
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode);

// Save the generated QR code as an HTML file.
// The SaveAsHtmlFile method is used to write the QR code to an HTML file, 
// allowing the QR code to be easily viewed or integrated into web pages.
myBarcode.SaveAsHtmlFile("myBarcode.html");
Imports IronBarCode



' Generate a QR code barcode with a specific URL as content.

' The BarcodeWriter.CreateBarcode method takes two parameters:

' 1. The content to be encoded as a QR code (in this case, a URL).

' 2. The encoding type, specified as BarcodeEncoding.QRCode, indicating that a QR code should be generated.

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode)



' Save the generated QR code as an HTML file.

' The SaveAsHtmlFile method is used to write the QR code to an HTML file, 

' allowing the QR code to be easily viewed or integrated into web pages.

myBarcode.SaveAsHtmlFile("myBarcode.html")
$vbLabelText   $csharpLabel

This method accepts a string of a file path. Looking into the HTML file generated, we can see the barcode outputted as an HTML tag within the <html>, <head>, and <body> tags that make up a complete HTML file.

Frequently Asked Questions

What is a Data URL?

A Data URL, also known as Data URI, is a type of Uniform Resource Identifier (URI) that allows data to be embedded directly in the URL string, inline in web pages as if they were external resources. This can include text, images, audio, video, and binary data.

How can I export a barcode as a Data URL?

To export a barcode as a Data URL, you can use the ToDataUrl() method on a GeneratedBarcode object from IronBarcode. This converts the barcode into a Data URL, which can then be used directly in an HTML image src attribute.

What method is used to export a barcode as an HTML tag?

To export a barcode as an HTML tag, you can use the ToHtmlTag() method provided by IronBarcode. This method renders the GeneratedBarcode object as a fully formed HTML tag.

How do I save a generated barcode as an HTML file?

You can save a generated barcode as an HTML file using the SaveAsHtmlFile() method from IronBarcode. You'll need to specify a file path where the HTML file will be saved, and the method will create the file with the barcode embedded as an HTML tag.

What are the steps to export barcodes as HTML in C#?

The steps to export barcodes as HTML in C# include downloading the IronBarcode library, creating a barcode with BarcodeWriter, and then using methods like ToDataUrl(), ToHtmlTag(), or SaveAsHtmlFile() to export the barcode in the desired HTML format.

Can the library export barcodes in formats other than HTML?

Yes, IronBarcode offers a variety of options to export barcodes in different formats, not just HTML. This flexibility allows developers to use the generated barcodes in multiple ways within their applications.

Is it necessary to save the barcode images to disk?

No, it is not necessary to save the barcode images to disk. IronBarcode provides options to export barcodes in formats that can be used directly in applications without saving them to disk, such as Data URLs or HTML tags.

What is the advantage of using this library for barcode generation in .NET?

IronBarcode is a powerful .NET library that simplifies the process of barcode generation and export. It provides versatile methods to export barcodes in various formats, which can be easily integrated into .NET applications.

Hairil Hasyimi Bin Omar
Hairil Hasyimi Bin Omar
Software Engineer
Like all great engineers, Hairil is an avid learner. He’s refining his knowledge of C#, Python, and Java, using that knowledge to add value to team members across Iron Software. Hairil joined the Iron Software team from Universiti Teknologi MARA in Malaysia, where he graduated with a Bachelor's degree in Chemical and Process Engineering.