How to Export Barcodes as HTML

An output from a good API must be versatile enough for users to use it further in their program or application, and not necessarily be saved on the disk. That is why IronBarcode offers many options for users to export the GeneratedBarcode into various types, and one of them is exporting the barcodes generated as HTML.

Speaking of exporting the GeneratedBarcode into HTML as either a Data URL, HTML Tag, or HTML File, in this article, we'll discuss how we can export to each format.

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
using IronBarCode;
using System;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode);
var dataUrl = myBarcode.ToDataUrl();
Console.WriteLine(dataUrl);
Imports IronBarCode
Imports System

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode)
Private dataUrl = myBarcode.ToDataUrl()
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
using IronBarCode;
using System;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode);
var htmlTag = myBarcode.ToHtmlTag();
Console.WriteLine(htmlTag);
Imports IronBarCode
Imports System

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode)
Private htmlTag = myBarcode.ToHtmlTag()
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;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode);
myBarcode.SaveAsHtmlFile("myBarcode.html");
Imports IronBarCode

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode)
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

How can I export a barcode as a Data URL in C#?

You can convert a barcode into a Data URL in C# using the IronBarcode library by calling the ToDataUrl() method on a GeneratedBarcode object. This allows the barcode to be embedded inline in web pages as an image source.

What is a Data URL and how is it used in HTML?

A Data URL is a type of URI that embeds data directly in URL strings. In HTML, it can be used within the src attribute of an image tag to display images directly without separate image files.

How do I export a barcode as an HTML tag in C#?

To export a barcode as an HTML tag in C#, use the IronBarcode library's ToHtmlTag() method on a GeneratedBarcode object. This method renders the barcode as a standalone HTML tag ready for embedding.

Can I save a barcode as an HTML file in C#?

Yes, you can save a barcode as an HTML file in C# using the IronBarcode library by utilizing the SaveAsHtmlFile() method. This method saves the barcode as an HTML tag within a complete HTML file structure.

What are the benefits of exporting barcodes as HTML?

Exporting barcodes as HTML provides versatility in displaying barcodes directly on web pages without additional image files. HTML formats such as Data URLs and HTML tags enhance integration with web applications.

Is it possible to use barcodes in applications without saving them to disk?

Yes, using IronBarcode, you can export barcodes as Data URLs or HTML tags, allowing their direct use in applications without the need to save them to disk first.

How does IronBarcode enhance .NET barcode generation?

IronBarcode simplifies .NET barcode generation by offering flexible export options like HTML tags and Data URLs, making it easy to integrate barcodes into various applications and formats.

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 ...Read More