如何将条形码导出为 HTML
一个好的API输出必须足够灵活,以便用户可以在其程序或应用程序中进一步使用,而不必保存在磁盘上。 这就是为什么 IronBarcode 为用户提供了多种选项来将GeneratedBarcode
导出为各种类型,其中之一是将生成的条形码导出为 HTML。
说到将GeneratedBarcode
导出为HTML,可以选择导出为数据URL、HTML标签或HTML文件。 在本文中,我们将讨论如何导出为每种格式。
如何用 C# 将条形码导出为 HTML
- 下载 C# 库以导出条形码
- 导出条形码为数据 URL
- 将 BarCode 导出为 HTML 标签
- 将 BarCode 导出为 HTML 文件
开始使用 IronBarcode
立即在您的项目中开始使用IronBarcode,并享受免费试用。
将条形码导出为数据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)
从上面的代码片段开始,我们使用BarcodeWriter
类的CreateBarcode()
方法创建了一个条形码,以条形码的值和条形码编码作为该方法的参数。 为了获取条形码的数据 URL,我们将ToDataUrl()
方法附加到GeneratedBarcode
。
将 BarCode 导出为 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)
从上面的代码片段中,我们可以简单地将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")
此方法接受文件路径字符串。 查看生成的HTML文件,我们可以看到条形码作为HTML标签输出在构成完整HTML文件的、
和标签中。