C#でバーコードをHTMLとしてエクスポートする方法
IronBarcodeはC#の開発者が生成されたバーコードを3つのフォーマットでHTMLとしてエクスポートすることを可能にします:インライン埋め込み用のデータURL、ダイレクトインジェクション用のHTMLタグ、スタンドアロン用の完全なHTMLファイルです。
クイックスタート: 1 行の HTML タグとしてバーコードをエクスポートする
1行の流暢なコードを使用して、バーコードを生成し、完全な形式のHTML画像タグとして直接エクスポートします。 外部イメージファイルやアセットの依存関係を管理することなく、すぐに使い始めることができます。
最小限のワークフロー(5ステップ)
- バーコードをエクスポートするためのC#ライブラリをダウンロードする
- バーコードをData URLとしてエクスポートする
- バーコードをHTMLタグとしてエクスポートする
- バーコードをHTMLファイルとしてエクスポートする
バーコードをデータ URL としてエクスポートするにはどうすればよいですか?
バーコードをデータURLとしてエクスポートする前に、データURLとは何かを理解してください。 データURL(データURIとしても知られています)は、URL文字列に直接データを埋め込むUniform Resource Identifierです。 これにより、データが外部リソースであるかのように、ウェブページにインライン表示することができます。 データ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() メソッドを使用してバーコードを作成します。 ToDataUrl() メソッドを GeneratedBarcode に添付してデータ URLを取得します。 このアプローチはIronBarcodeのすべてのサポートされているバーコードフォーマットで動作します。
なぜデータ URL を使用することが Web アプリケーションにとって重要なのですか?
データURLは、HTTPリクエストを減らし、ページの読み込みパフォーマンスを向上させることで、ウェブアプリケーションに大きな利点をもたらします。 バーコードをデータURLとして埋め込むと、画像データはHTMLドキュメント自体の一部となり、個別の画像ファイル要求がなくなります。 この利点
- 最小限のサーバーラウンドトリップを必要とするシングルページアプリケーション(SPA)。
- 外部画像がブロックされる可能性のあるメールテンプレート。
- オフライン対応アプリケーションは、ネットワーク接続なしで機能します。
- 物理ファイルの作成が非効率的な場合の動的バーコード生成。
本番展開については、Azureへの展開またはクラウドベースのバーコード生成のためのAWS展開に関するガイドを参照してください。
画像ファイルの代わりにデータ URL を使用するのはどのような場合ですか?
バーコードのサイズが小さく(32KB以下)、すぐにインラインレンダリングが必要な場合は、Data 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
データURLのサイズ制限は何ですか?
最近のブラウザは、技術的には数メガバイトのデータURLをサポートしていますが、現実的には限界があります:
- Internet Explorer 8:32KBに制限
- 最新ブラウザ: 2-4MBをサポートしますが、パフォーマンスは低下します。
- モバイルブラウザ: メモリの制約により制限が厳しくなっています。
- メールクライアント:データURLを8-64KBに制限する
最適なパフォーマンスを実現するため、Data URL BarCodeは32KB未満に抑えてください。 大きなバーコードや複数のバーコード生成には、export as stream 機能を使用して効率的なメモリ管理を行います。
バーコードを HTML タグとしてエクスポートするにはどうすればよいですか?
ToHtmlTag() メソッドを使用して、GeneratedBarcode を HTML にエクスポートします。 このメソッドは、 JavaScript、CSS、または画像の依存関係なしに HTML に直接挿入するための完全な形式の HTML タグとして GeneratedBarcode オブジェクトをレンダリングします。 次のコードは、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)
生成されたバーコードの HTML タグを取得するには、ToHtmlTag() メソッドを GeneratedBarcode に添付します。 このHTMLタグを大きなHTMLファイルに直接埋め込みます。高度なスタイリングオプションについては、バーコードスタイルのカスタマイズのガイドを参照してください。
外部画像参照よりも HTML タグ エクスポートのほうがよいのはなぜですか?
HTMLタグのエクスポートは、外部画像参照よりも重要な利点があります:
1.リンク切れのない画像: BarCodeデータはタグに直接埋め込まれます。 2.より高速なレンダリング:追加のHTTPリクエストは必要ありません。 3.簡素化されたデプロイ:個別のイメージ資産管理は不要です。 4.より優れたセキュリティ: ファイルパスやサーバー構造の漏洩がありません。 5.動的生成:リアルタイムのバーコード作成に最適です。
以下は、実用的なウェブアプリケーション統合の例です:
// 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>")
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'");
' Create a customized barcode with specific styling
Dim 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
Dim htmlTag As String = customBarcode.ToHtmlTag()
Dim customizedTag As String = htmlTag.Replace("<img", "<img class='barcode' id='product-123'")
データURL形式よりもHTMLタグを選ぶべきときは?
必要な場合は、HTMLタグ形式を選択してください:
- クリーンで読みやすいHTML出力
- 既存のHTMLテンプレートとの容易な統合
- HTMLエディタおよびCMSシステムとの互換性
- コンテンツ制作者のための直接コピーペースト機能
HTMLタグ形式は、コンポーネントに動的にバーコード画像を注入するBlazorアプリケーションで特に効果的です。
バーコードを HTML ファイルとして保存するにはどうすればよいですか?
SaveAsHtmlFile() メソッドを使用して、GeneratedBarcode を HTML ファイルとして保存します。 次のコードは、この方法を示しています:
: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ファイルには、バーコードが適切な<body>タグで囲まれたHTMLタグとして含まれ、完全なHTMLファイルを形成します。複数のファイル形式を使用する複雑なシナリオについては、出力データ形式ガイドをご覧ください。
なぜ断片ではなく完全な HTML ファイルを生成するのですか?
完全なHTMLファイルは、特定の使用ケースに明確な利点を提供します:
- スタンダロン文書:印刷可能な BarCode シートを生成する
- 電子メールの添付ファイル:自己完結型のバーコードファイルを送信する。
- アーカイブの目的: バーコードを適切な構造で保存する。
- テストとデバッグ:BarCode を単独で表示する。
- バッチ処理:配布用に複数のファイルを生成する
以下は、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
HTMLファイルエクスポートの一般的な使用例とは
HTMLファイルのエクスポートは、このような場面で威力を発揮します:
1.小売POSシステム:印刷可能な値札を生成する 2.倉庫管理:棚のバーコードラベルを作成する 3.ドキュメント管理: レポートに BarCode を埋め込む 4.品質管理:追跡可能なバッチコードを生成する 5.イベント管理:スキャン可能なコードでチケットを作成する
大量の BarCode 生成には、async とマルチスレッドを実装してパフォーマンスを向上させてください。 QR コードのような特殊な形式を扱う場合、C# QR Code Generator チュートリアルでは、さまざまなビジネス ニーズに合わせた QR コードの作成とカスタマイズに関する包括的なガイダンスを提供しています。
よくある質問
C#でバーコードをデータURLとしてエクスポートするには?
IronBarcodeでは、GeneratedBarcodeオブジェクトのToDataUrl()メソッドを使用することで、バーコードをデータURLとしてエクスポートすることができます。BarCodeWriter.CreateBarcode()を使用して希望の値とエンコーディングでバーコードを作成し、ToDataUrl()を呼び出してHTMLに直接埋め込むことができるデータURL文字列を取得します。
BarCode で使用できる 3 つの HTML エクスポート形式は何ですか?
IronBarcodeは3つのHTMLエクスポート形式を提供します:外部ファイルなしでインラインに埋め込むためのデータURL、ウェブページに直接注入するためのHTMLタグ、スタンドアロンで使用するための完全なHTMLファイルです。各フォーマットは、Webアプリケーションの異なる統合ニーズに対応します。
1行のコードでバーコード用のHTML画像タグを生成できますか?
はい、IronBarcodeでは流暢なコード一行で完全なHTMLイメージタグを生成することができます。BarCodeWriter.CreateBarcode()をバーコードの値とエンコーディングで使用し、ToHtmlTag()メソッドをチェインして、挿入可能な完全なHTMLイメージタグを取得します。
バーコードに従来の画像ファイルではなく、Data URLを使用するのはどのような場合ですか?
バーコードのサイズが小さく(32KB以下)、すぐにインラインレンダリングが必要な場合は、Data URLを使用してください。シングルページのアプリケーション、メールテンプレート、オフライン対応アプリケーション、ダイナミックバーコード生成シナリオに最適です。IronBarcodeのToDataUrl()メソッドはこの変換をシームレスにします。
HTMLエクスポートでサポートされているバーコード形式は何ですか?
IronBarcodeはCode 128、QRコード、その他多くのバーコードフォーマットをHTMLエクスポート用にサポートしています。ToDataUrl()、ToHtmlTag()、HTMLファイルエクスポートメソッドはライブラリでサポートされているすべてのバーコードフォーマットで動作します。
データURLを使用すると、Webアプリケーションのパフォーマンスはどのように向上しますか?
データURLは、HTMLドキュメントに直接バーコード画像データを埋め込むことで、画像ファイルに対する個別のHTTPリクエストを排除し、パフォーマンスを向上させます。これにより、サーバーのラウンドトリップが減り、ページのロード時間が改善されます。これは、IronBarcodeを使用してWebアプリケーションでダイナミックバーコードを生成する場合に特に有益です。

