How to Export Barcodes as HTML in C#
IronBarcode enables C# developers to export generated barcodes as HTML in three formats: Data URL for inline embedding, HTML tag for direct injection, or complete HTML file for standalone use - providing versatile integration options without external dependencies.
Quickstart: Export a Barcode as an HTML Tag with One LineGenerate a barcode and export it directly as a fully-formed HTML image tag using a single fluent line of code. Get started fast without managing external image files or asset dependencies.
-
1Install IronBarcode with NuGet Package Manager
-
2Copy and run this code snippet.
var htmlTag = BarcodeWriter.CreateBarcode("1234567890", BarcodeWriterEncoding.Code128).ToHtmlTag();C# -
3Deploy to test on your live environment
Start using IronBarcode in your project today with a free trial
Minimal Workflow (4 steps)
- Download C# library to export barcodes
- Export barcodes as Data URL
- Export barcodes as HTML tag
- Export barcodes as HTML file
How Do I Export a Barcode as a Data URL?
Before exporting a barcode as a Data URL, understand what a Data URL is. A Data URL (also known as Data URI) is a Uniform Resource Identifier that embeds data directly in the URL string. This allows inline display in web pages as if the data were external resources. Data URLs support any format: text, images, audio, video, and binary data. Use the obtained Data URL in HTML inside an image tag as a src attribute. Here's how to convert a GeneratedBarcode into a Data URL:
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)Create a barcode using the CreateBarcode() method from the BarcodeWriter class with the barcode value and encoding as arguments. Get the Data URL by attaching the ToDataUrl() method to the GeneratedBarcode. This approach works with all supported barcode formats in IronBarcode.
Why Does Using Data URL Matter for Web Applications?
Data URLs provide significant advantages for web applications by reducing HTTP requests and improving page load performance. When you embed a barcode as a Data URL, the image data becomes part of the HTML document itself, eliminating separate image file requests. This benefits:
- Single-page applications (SPAs) requiring minimal server round-trips
- Email templates where external images might be blocked
- Offline-capable applications functioning without network connectivity
- Dynamic barcode generation where creating physical files is inefficient
For production deployment, see our guides on deploying to Azure or AWS deployment for cloud-based barcode generation.
When Should I Use Data URL Instead of Image Files?
Use Data URLs when barcodes are small (under 32KB) and require immediate inline rendering. Choose traditional image files stored on servers or CDNs when:
// Example: Choosing between Data URL and file export based on size
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("LARGE-DATA-STRING-HERE", BarcodeEncoding.PDF417);
// Check estimated size before choosing export method
if (barcode.BinaryStream.Length < 32768) // 32KB threshold
{
// Use Data URL for smaller barcodes
string dataUrl = barcode.ToDataUrl();
// Embed directly in HTML
}
else
{
// Save as file for larger barcodes
barcode.SaveAsImage("large-barcode.png");
// Reference as external resource
}' Example: Choosing between Data URL and file export based on size
Dim barcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("LARGE-DATA-STRING-HERE", BarcodeEncoding.PDF417)
' Check estimated size before choosing export method
If barcode.BinaryStream.Length < 32768 Then ' 32KB threshold
' Use Data URL for smaller barcodes
Dim dataUrl As String = barcode.ToDataUrl()
' Embed directly in HTML
Else
' Save as file for larger barcodes
barcode.SaveAsImage("large-barcode.png")
' Reference as external resource
End IfWhat Are the Size Limitations of Data URLs?
While modern browsers technically support Data URLs of several megabytes, practical limitations exist:
- Internet Explorer 8: Limited to 32KB
- Modern browsers: Support 2-4MB, but performance degrades
- Mobile browsers: Stricter limits due to memory constraints
- Email clients: Restrict Data URLs to 8-64KB
Keep Data URL barcodes under 32KB for optimal performance. For larger barcodes or multiple barcode generation, use our export as stream functionality for efficient memory management.
How Do I Export a Barcode as an HTML Tag?
Export a GeneratedBarcode to HTML using the ToHtmlTag() method. This method renders the GeneratedBarcode object as a fully formed HTML tag for direct injection into HTML without JavaScript, CSS, or image dependencies. The following code demonstrates HTML tag export:
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)Attach the ToHtmlTag() method to the GeneratedBarcode to obtain the HTML tag of the generated barcode. Embed this HTML tag directly into a larger HTML file. For advanced styling options, see our guide on customizing barcode styles.
Why Is HTML Tag Export Better Than External Image References?
HTML tag export provides key advantages over external image references:
- No broken image links: Barcode data embeds directly in the tag
- Faster rendering: No additional HTTP requests needed
- Simplified deployment: No separate image asset management
- Better security: No file path or server structure exposure
- Dynamic generation: Perfect for real-time barcode creation
Here's a practical web application integration example:
// Generate multiple barcodes for a product catalog
var products = new[] { "PROD-001", "PROD-002", "PROD-003" };
var htmlBuilder = new StringBuilder();
foreach (var productCode in products)
{
var barcode = BarcodeWriter.CreateBarcode(productCode, BarcodeEncoding.Code128)
.ResizeTo(200, 50)
.SetMargins(10);
htmlBuilder.AppendLine($"<div class='product-barcode'>");
htmlBuilder.AppendLine($" <p>Product: {productCode}</p>");
htmlBuilder.AppendLine($" {barcode.ToHtmlTag()}");
htmlBuilder.AppendLine($"</div>");
}Imports System.Text
' Generate multiple barcodes for a product catalog
Dim products = New String() {"PROD-001", "PROD-002", "PROD-003"}
Dim htmlBuilder = New StringBuilder()
For Each productCode In products
Dim barcode = BarcodeWriter.CreateBarcode(productCode, BarcodeEncoding.Code128) _
.ResizeTo(200, 50) _
.SetMargins(10)
htmlBuilder.AppendLine("<div class='product-barcode'>")
htmlBuilder.AppendLine($" <p>Product: {productCode}</p>")
htmlBuilder.AppendLine($" {barcode.ToHtmlTag()}")
htmlBuilder.AppendLine("</div>")
NextHow Can I Customize the Generated HTML Tag Attributes?
While ToHtmlTag() generates a standard img tag, you can enhance it with additional attributes or custom HTML wrapping. For advanced customization, combine IronBarcode with our styling capabilities:
// Create a customized barcode with specific styling
var customBarcode = BarcodeWriter.CreateBarcode("CUSTOM-123", BarcodeEncoding.Code128)
.AddAnnotationTextAboveBarcode("Product ID")
.SetMargins(15)
.ChangeBackgroundColor(System.Drawing.Color.LightGray);
// Get the HTML tag and add custom attributes
string htmlTag = customBarcode.ToHtmlTag();
string customizedTag = htmlTag.Replace("<img", "<img class='barcode' id='product-123'");Imports System.Drawing
' Create a customized barcode with specific styling
Dim customBarcode = BarcodeWriter.CreateBarcode("CUSTOM-123", BarcodeEncoding.Code128) _
.AddAnnotationTextAboveBarcode("Product ID") _
.SetMargins(15) _
.ChangeBackgroundColor(Color.LightGray)
' Get the HTML tag and add custom attributes
Dim htmlTag As String = customBarcode.ToHtmlTag()
Dim customizedTag As String = htmlTag.Replace("<img", "<img class='barcode' id='product-123'")When Should I Choose HTML Tag Over Data URL Format?
Choose HTML tag format when you need:
- Clean, readable HTML output
- Easy integration with existing HTML templates
- Compatibility with HTML editors and CMS systems
- Direct copy-paste functionality for content creators
HTML tag format works particularly well with Blazor applications where you dynamically inject barcode images into components.
How Do I Save a Barcode as an HTML File?
Save a GeneratedBarcode as an HTML file using the SaveAsHtmlFile() method. The following code demonstrates this method:
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")This method accepts a file path string. The generated HTML file contains the barcode as an HTML tag within proper <html>, <head>, and <body> tags forming a complete HTML file. For complex scenarios with multiple file formats, see our output data formats guide.
Why Generate Complete HTML Files Instead of Fragments?
Complete HTML files offer distinct advantages for specific use cases:
- Standalone documentation: Generate printable barcode sheets
- Email attachments: Send self-contained barcode files
- Archive purposes: Store barcodes with proper structure
- Testing and debugging: View barcodes independently
- Batch processing: Generate multiple files for distribution
Here's an example generating a batch of HTML files:
// Generate HTML files for inventory items
public void GenerateInventoryBarcodes(List<InventoryItem> items)
{
foreach (var item in items)
{
var barcode = BarcodeWriter.CreateBarcode(item.SKU, BarcodeEncoding.Code128)
.AddAnnotationTextBelowBarcode($"{item.Name} - ${item.Price:F2}")
.ResizeTo(300, 100);
// Save with descriptive filename
string filename = $"barcode_{item.SKU}_{DateTime.Now:yyyyMMdd}.html";
barcode.SaveAsHtmlFile(filename);
}
}' Generate HTML files for inventory items
Public Sub GenerateInventoryBarcodes(items As List(Of InventoryItem))
For Each item In items
Dim barcode = BarcodeWriter.CreateBarcode(item.SKU, BarcodeEncoding.Code128) _
.AddAnnotationTextBelowBarcode($"{item.Name} - ${item.Price:F2}") _
.ResizeTo(300, 100)
' Save with descriptive filename
Dim filename As String = $"barcode_{item.SKU}_{DateTime.Now:yyyyMMdd}.html"
barcode.SaveAsHtmlFile(filename)
Next
End SubWhat Are Common Use Cases for HTML File Export?
HTML file export proves valuable in these scenarios:
- Retail point-of-sale systems: Generate printable price tags
- Warehouse management: Create barcode labels for shelving
- Document management: Embed barcodes in reports
- Quality control: Generate traceable batch codes
- Event management: Create tickets with scannable codes
For high-volume barcode generation, implement async and multithreading to improve performance. When working with specialized formats like QR codes, our C# QR Code Generator tutorial provides comprehensive guidance on creating and customizing QR codes for various business needs.
Frequently Asked Questions
How can you export C# barcodes as HTML using IronBarcode?
IronBarcode allows exporting C# barcodes as HTML in three formats: Data URL for inline use, HTML tag for direct injection, or a complete HTML file for standalone purposes, offering versatile integration without external dependencies.
What are the benefits of using Data URLs for barcode export?
Using Data URLs for barcode export reduces HTTP requests and improves page performance, making it ideal for single-page applications, email templates, offline apps, and dynamic barcode generation where file management is cumbersome.
What are the size limitations of using Data URLs in web browsers?
Though modern browsers support several megabytes, practical data URL size limits include 32KB for Internet Explorer 8, 2-4MB for modern browsers, and 8-64KB for email clients, with 32KB recommended for optimal performance.
When should you choose HTML tag export over Data URL format?
Choose HTML tag export for clean, readable HTML, ease of integration with templates, compatibility with HTML editors and CMS systems, and direct copy-paste functionality in content management scenarios.
How can a barcode be saved as an HTML file using IronBarcode?
Use IronBarcode’s `SaveAsHtmlFile()` method by providing a file path to save a barcode as a standalone HTML file, which includes all necessary HTML tags for complete structure and presentation.
Why is HTML tag export advantageous over external image references?
HTML tag export avoids broken image links, speeds up rendering by eliminating additional HTTP requests, simplifies deployment without image asset management, enhances security, and suits real-time barcode generation.
What are common use cases for exporting barcodes as complete HTML files?
Complete HTML file export is used for standalone documentation, sending barcode files as email attachments, archiving with structural integrity, testing, debugging, and batch processing for distribution.
How does IronBarcode facilitate fast integration of barcode HTML tags?
IronBarcode offers a quickstart method to generate and export barcodes as a fully-formed HTML image tag with a single line of fluent code, eliminating the need to manage external files or assets.
How can you customize the generated HTML tag attributes in IronBarcode?
While `ToHtmlTag()` provides standard HTML tags, you can enhance these with custom attributes using additional HTML wrapping and IronBarcode’s styling capabilities for advanced customization options.
What scenarios benefit from generating complete HTML files for barcodes?
Generating complete HTML files benefits retail point-of-sale systems for printable tags, warehouse management for labeling, document management for reports, quality control, and event management for tickets with scannable codes.
