如何在 C# 中將 BarCode 建立為 HTML

如何在 C# 中將條碼匯出為 HTML

This article was translated from English: Does it need improvement?
Translated
View the article in English

一個好的 API 的輸出必須足夠靈活,以便用戶可以在自己的程式或應用程式中進一步使用它,而不一定要保存到磁碟上。 因此,IronBarcode 為使用者提供了許多GeneratedBarcode匯出為各種類型的選項,其中之一就是將產生的條碼匯出為 HTML

本文將討論如何將GeneratedBarcode匯出為 HTML 格式(資料 URLHTML 標籤HTML 檔案)。

快速入門:用一行程式碼將條碼匯出為 HTML 標籤

使用 IronBarcode,您只需一行流暢的程式碼即可產生條碼並將其直接匯出為完整的 HTML 圖像標籤。 它的設計旨在讓您快速上手,無需管理外部圖像檔案或資源依賴項。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronBarcode

    PM > Install-Package BarCode

  2. 複製並運行這段程式碼。

    var htmlTag = BarcodeWriter.CreateBarcode("1234567890", BarcodeWriterEncoding.Code128).ToHtmlTag();
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronBarcode,免費試用!
    arrow pointer

匯出條碼為資料 URL

在討論如何將條碼匯出為資料 URL 之前,我們需要先了解資料 URL 究竟是什麼。 資料 URL ,也稱為資料 URI ,是一種統一資源識別碼 (URI),它允許將資料直接嵌入 URL 字串中,就像它們是外部資源一樣內聯嵌入到網頁中。 它可以是任何格式,包括文字、圖像、音訊、視訊和二進位資料。 取得到的資料 URL 可以稍後在 HTML 中用作圖片標籤內的src屬性。 以下是一段程式碼片段,示範如何將GeneratedBarcode轉換為資料 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

從上面的程式碼片段可以看出,我們首先使用BarcodeWriter類別的CreateBarcode()方法來建立條碼,並將條碼值和條碼編碼作為該方法的參數。 為了取得條碼的資料 URL ,我們將ToDataUrl()方法附加到GeneratedBarcode上。

將條碼匯出為 HTML 標籤

GeneratedBarcode匯出為 HTML 的另一種方法是使用ToHtmlTag()方法將其匯出為 HTML 標籤。 此方法將GeneratedBarcode物件渲染為完全成型的 HTML 標籤,可以直接將其註入到 HTML 中,而無需在任何 JavaScript、CSS 或影像依賴項中引用它。 以下程式碼片段示範如何匯出為 HTML 標籤。

: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

從上面的程式碼片段中,我們可以簡單地將ToHtmlTag()方法附加到GeneratedBarcode ,以便取得產生的條碼的 HTML 標籤。 這個HTML標籤可以直接嵌入到更大的HTML檔案中。

將條碼匯出為 HTML 文件

使用者也可以選擇GeneratedBarcode儲存為 HTML 檔案。 為此,請使用SaveAsHtmlFile()方法。 以下程式碼片段示範如何使用此方法。

: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

此方法接受一個檔案路徑字串。 查看產生的 HTML 文件,我們可以看到條碼以 HTML 標籤的形式輸出。<html> , 和構成完整 HTML 文件的標籤。

常見問題解答

如何在 C# 中將 BarCode 匯出為資料 URL?

您可以在 C# 中使用 IronBarcode 程式庫,透過在 GeneratedBarcode 物件上呼叫 ToDataUrl() 方法,將條碼轉換成 Data URL。這樣,條碼就可以作為圖片來源嵌入到網頁中。

什麼是資料 URL,在 HTML 中如何使用?

資料 URL 是一種 URI,可直接將資料嵌入 URL 字串中。在 HTML 中,它可在圖片標籤的 src 屬性中使用,以直接顯示圖片,而無需獨立的圖片檔案。

如何在 C# 中將 BarCode 匯出為 HTML 標籤?

要在 C# 中將條碼匯出為 HTML 標籤,請在 GeneratedBarcode 物件上使用 IronBarcode 函式庫的 ToHtmlTag() 方法。此方法可將條碼渲染為獨立的 HTML 標籤,以便嵌入。

我可以在 C# 中將 BarCode 儲存為 HTML 檔案嗎?

是的,您可以使用 SaveAsHtmlFile() 方法在 C# 中使用 IronBarcode 库将条形码保存为 HTML 文件。此方法在一個完整的 HTML 檔案結構中,將條碼儲存為 HTML 標籤。

將 BarCode 匯出為 HTML 有哪些好處?

將 BarCode 匯出為 HTML 可提供多功能性,可直接在網頁上顯示條碼,而無需額外的影像檔案。HTML 格式(如資料 URL 和 HTML 標籤)可增強與 Web 應用程式的整合。

是否可以在應用程式中使用 BarCode 而不將其儲存至磁碟?

是的,使用 IronBarcode,您可以將條碼匯出為資料 URL 或 HTML 標籤,讓它們直接用於應用程式中,而不需要先儲存到磁碟。

IronBarcode 如何增強 .NET 條碼生成?

IronBarcode 通過提供靈活的輸出選項(如 HTML 標籤和資料 URL)簡化了 .NET 條碼的生成,使條碼能輕易整合到各種應用程式和格式中。

Hairil Hasyimi Bin Omar
軟體工程師
和所有优秀的工程师一样,Hairil 是个努力学习者。他正在细化自己的 C# 、Python 和 Java 知识,将这些知识应用于 Iron Software 各个团队成员以增加价值。Hairil 自马来西亚 Universiti Teknologi MARA 加入 Iron Software 团队,并以化学与工艺工程学士学位毕业。
準備好開始了嗎?
Nuget 下載 1,979,979 | Version: 2025.11 剛發表