如何在C#中將條碼匯出為HTML
IronBarcode 可讓 C# 開發者以三種格式將產生的條碼匯出為 HTML:用於內嵌的資料 URL、可直接插入的 HTML 標籤,或可獨立使用的完整 HTML 檔案,提供多種整合方式,無需外部相依項目。
快速開始:將條碼匯出為HTML標籤只需一行使用一行鏈式呼叫的程式碼,產生條碼並直接匯出為完整的 HTML 影像標籤。快速上手,無需管理外部影像檔案或相依資源。
-
1使用 IronBarcode 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
var htmlTag = BarcodeWriter.CreateBarcode("1234567890", BarcodeWriterEncoding.Code128).ToHtmlTag();Dim htmlTag = BarcodeWriter.CreateBarcode("1234567890", BarcodeWriterEncoding.Code128).ToHtmlTag()Dim htmlTag = BarcodeWriter.CreateBarcode("1234567890", BarcodeWriterEncoding.Code128).ToHtmlTag() -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronBarcode,並使用 免費試用
最簡工作流程(4 個步驟)
- 下載將條碼匯出的C#程式庫
- 將條碼匯出為資料 URL
- 將條碼匯出為 HTML 標籤
- 將條碼匯出為 HTML 檔案
如何將條碼匯出為資料URL?
在將條碼匯出為資料 URL 之前,請先瞭解何謂資料 URL。 Data URL(也稱為 Data URI)是直接在 URL 字串中嵌入資料的統一資源識別碼。 這允許在網頁中內嵌顯示,就像資料是外部資源一樣。 資料 URL 支援任何格式:文字、影像、音訊、視訊和二進位資料。 在HTML中使用取得的資料URL,作為影像標籤的src屬性。 以下是如何將GeneratedBarcode轉換為資料URL:
using IronBarCode;
using System;
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode);
var dataUrl = myBarcode.ToDataUrl();
Console.WriteLine(dataUrl);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)Imports IronBarCode
Imports System
Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode)
Private dataUrl = myBarcode.ToDataUrl()
Console.WriteLine(dataUrl)使用BarcodeWriter類別中的CreateBarcode()方法建立條碼,將條碼值和編碼作為參數。 將ToDataUrl()方法附加到GeneratedBarcode以獲取資料URL。 此方法適用於IronBarcode中支援的所有條碼格式。
為什麼使用資料URL對於Web應用程式很重要?
資料 URL 可減少 HTTP 請求並提升頁面載入效能,為網頁應用程式提供顯著優勢。當您將條碼嵌入為資料 URL 時,影像資料會成為 HTML 檔案本身的一部分,無需另外請求影像檔案。以下情境可從中受益:
- 單頁應用程式 (SPA) 需要最少的伺服器往返次數
- 電子郵件範本,外部圖片可能會被阻擋
- 離線應用程式在沒有網路連線的情況下運作
- 動態條碼產生,其中建立實體檔案的效率很低
對於生產部署,請參閱我們的部署到Azure或AWS部署指南,以進行基於雲的條碼生成。
什麼時候應使用資料URL而不是影像檔案?
當條碼較小(32KB 以下)且需要立即內嵌渲染時,請使用資料 URL。 選擇儲存於伺服器或 CDN 的傳統影像檔案時:
// 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
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 If' 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 If資料URL有哪些大小限制?
儘管現代瀏覽器在技術上支援數 MB 的資料 URL,但仍存在實際限制:
- Internet Explorer 8:限於32KB
- 現代瀏覽器:支援2-4MB,但效能會降低
- 行動瀏覽器:受記憶體限制,大小限制更嚴格
- 電子郵件客戶端:限制資料URL為8-64KB
將資料 URL BarCode 保持在 32KB 以下,以獲得最佳效能。 對於較大的條碼或多個條碼生成,請使用我們的匯出為串流功能以有效管理記憶體。
如何將條碼匯出為HTML標籤?
使用ToHtmlTag()方法將GeneratedBarcode匯出為HTML。 此方法將GeneratedBarcode物件呈現為完整的HTML標籤,可直接注入HTML,無需JavaScript、CSS或影像依賴。 以下程式碼示範 HTML 標籤匯出:
using IronBarCode;
using System;
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode);
var htmlTag = myBarcode.ToHtmlTag();
Console.WriteLine(htmlTag);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)Imports IronBarCode
Imports System
Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode)
Private htmlTag = myBarcode.ToHtmlTag()
Console.WriteLine(htmlTag)將ToHtmlTag()方法附加到GeneratedBarcode以獲取生成條碼的HTML標籤。 將此HTML標籤直接嵌入到更大的HTML 檔案中。如需進階樣式選項,請參閱我們的自訂條碼樣式指南。
為什麼HTML標籤匯出比外部影像引用更好?
相較於參照外部影像,匯出 HTML 標籤具有以下主要優勢:
- 無損壞的影像連結:條碼資料直接嵌入標籤中
- 更快的渲染:不需要額外的HTTP請求
- 簡化的部署:無需單獨的影像資產管理
- 更好的安全性:無檔案路徑或伺服器結構曝露
- 動態生成:適合即時條碼建立
以下是一個實用的網路應用程式整合範例:
// 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>");
}// 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>")
NextImports 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>")
Next如何自訂生成的HTML標籤屬性?
雖然ToHtmlTag()會產生標準 img 標籤,您仍可加入額外屬性或自訂外層 HTML 元素來擴充它。如需進階自訂,請結合 IronBarcode 與我們的樣式功能:
// 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'");// 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'")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'")何時應選擇HTML標籤而非資料URL格式?
當您有以下需求時,請選擇 HTML 標籤格式:
- 乾淨、可讀的 HTML 輸出
- 容易與現有的 HTML 範本整合
- 與 HTML 編輯器和 CMS 系統相容
- 為內容創造者提供直接複製貼上功能
HTML標籤格式在Blazor應用程式中特別適用,您可以動態地將條碼影像注入到元件中。
如何將條碼儲存為HTML 檔案?
使用SaveAsHtmlFile()方法將GeneratedBarcode保存為HTML 檔案。 以下程式碼示範此方法:
using IronBarCode;
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode);
myBarcode.SaveAsHtmlFile("myBarcode.html");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")Imports IronBarCode
Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode)
myBarcode.SaveAsHtmlFile("myBarcode.html")此方法接受檔案路徑字串。 生成的HTML 檔案在適當的<html>、<head>和<body>標籤內包含條碼,形成完整的HTML 檔案。針對多文件格式的複雜場景,請參閱我們的輸出資料格式指南。
為什麼需要生成完整的HTML 檔案而不是片段?
完整的 HTML 檔案在特定使用情況下具有明顯的優勢:
- 獨立的文件:生成可列印的條碼頁
- 電子郵件附件:發送自包含的條碼文件
- 存檔目的:以適當結構存儲條碼
- 測試和偵錯:獨立查看條碼
- 批次處理:生成多個文件以進行分發
以下是產生一批 HTML 檔案的範例:
// 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 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 Sub' 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 SubHTML 檔案匯出的常見用例是什麼?
HTML 檔案匯出在這些情況下很有價值:
- 零售 POS 系統:產生可列印的價格標籤
- 倉庫管理:建立貨架的條碼標籤
- 文件管理:在報告中嵌入條碼
- 品質控制:生成可追溯的批次代碼
- 活動管理:建立帶有可掃瞄代碼的門票
對於高容量條碼生成,實施非同步和多執行緒以提高效能。 在處理像QR Code這樣的特殊格式時,我們的C# QR Code Generator教學提供了建立和自訂QR Code以適應各種業務需求的全面指導。
常見問題
如何使用IronBarcode將C#條碼匯出為HTML?
IronBarcode允許將C#條碼匯出為三種HTML格式:用於內嵌的Data URL、供直接注入的HTML標籤或作為獨立用的完整HTML文件,提供多樣化整合而不需要外部依賴。
使用Data URL匯出條碼有什麼好處?
使用Data URL匯出條碼減少了HTTP請求並改善了頁面效能,非常適合單頁應用程式、電子郵件範本、離線應用程式以及文件管理繁瑣的動態條碼生成。
在網頁瀏覽器中使用Data URL有什麼尺寸限制?
雖然現代瀏覽器支持幾兆的容量,但實際Data URL的大小限制包括Internet Explorer 8的32KB、現代瀏覽器的2-4MB,以及電子郵件客戶端的8-64KB,建議使用32KB以獲得最佳效能。
何時應選擇HTML標籤匯出,而非Data URL格式?
在需要乾淨、可讀的HTML、輕鬆整合範本、與HTML編輯器及CMS系統的相容性,以及在內容管理情境中直接複製貼上功能時,選擇HTML標籤匯出。
如何使用IronBarcode將條碼保存為HTML文件?
使用IronBarcode的SaveAsHtmlFile()方法提供檔案路徑保存條碼為獨立的HTML文件,包括所有必要的HTML標籤以完成結構和展示。
為什麼HTML標籤匯出比外部影像引用有優勢?
HTML標籤匯出避免了影像鏈接中斷,通過消除額外的HTTP請求加速渲染,簡化了不需影像資產管理的部署,加強安全性,並適用於即時條碼生成。
將條碼匯出為完整HTML文件的常見用例有哪些?
完整HTML文件匯出用於獨立文件、以電子郵件附件形式發送條碼檔案、保持結構完整性的歸檔、測試、偵錯和分發的批次處理。
IronBarcode如何促進條碼HTML標籤的快速整合?
IronBarcode 提供一種快速啟動的方法,能夠用一行流暢的代碼生成並匯出條碼作為完整的HTML圖像標籤,無需管理外部檔案或資產。
如何自訂IronBarcode生成的HTML標籤屬性?
雖然ToHtmlTag()提供標準HTML標籤,您可以通過額外的HTML包裝和IronBarcode的樣式功能增強這些標籤,提供高級自訂選項。
哪些情境適合生成完整的條碼HTML文件?
生成完整HTML文件適合於零售點銷售系統的可打印標籤、倉庫管理的標籤、文件管理的報告、品質控制和事件管理中可掃描條碼的票券。