バーコードをC#でHTMLとしてエクスポートする方法
IronBarcodeは、C#開発者が生成されたバーコードをHTMLでエクスポートするための3つの形式を提供します: インライン埋め込み用のデータURL、直接注入用のHTMLタグ、またはスタンドアロンで使用する完全なHTMLファイル。これにより、外部依存なしに柔軟な統合オプションを提供します。
クイックスタート: バーコードを HTML タグとして 1 行でエクスポート1行のメソッドチェーンでバーコードを生成し、完全なHTML画像タグとして直接エクスポートします。外部画像ファイルやアセットへの依存関係を管理せずに、すぐに使い始めることができます。
-
1IronBarcode を 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#ライブラリをダウンロード
- バーコードをData URLとしてエクスポートする
- バーコードをHTMLタグとしてエクスポートする
- バーコードをHTMLファイルとしてエクスポートする
バーコードをデータURLとしてエクスポートする方法は?
バーコードをデータURLとしてエクスポートする前に、データURLとは何かを理解してください。 データURL(データURIとしても知られています)は、URL文字列に直接データを埋め込むUniform Resource Identifierです。 これにより、データが外部リソースであるかのように、ウェブページにインライン表示することができます。 データURLは、テキスト、画像、音声、動画、バイナリデータなど、あらゆる形式に対応しています。 取得したデータURLをHTML内で画像タグの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)バーコードを作成するには、CreateBarcode() メソッドを BarcodeWriter クラスから使用し、バーコードの値とエンコーディングを引数として渡します。 データURLを取得するには、ToDataUrl() メソッドを GeneratedBarcode にアタッチします。 このアプローチは、IronBarcodeでサポートされているすべてのバーコード形式に対応しています。
ウェブアプリケーションでデータURLを使用する重要性とは?
データ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' 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に制限します
最適なパフォーマンスを実現するため、データURLとして埋め込むバーコードは32KB未満に抑えてください。 より大きなバーコードや複数のバーコード生成のためには、効率的なメモリ管理を可能にするストリームとしてエクスポート機能を使用してください。
バーコードをHTMLタグとしてエクスポートする方法は?
GeneratedBarcode を HTML にエクスポートするには、ToHtmlTag() メソッドを使用します。 このメソッドは、JavaScript、CSS、画像の依存関係なしに、GeneratedBarcodeオブジェクトを完全なHTMLタグとしてレンダリングし、直接HTMLに挿入することができます。 次のコードは、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)生成されたバーコードの HTML タグを取得するには、ToHtmlTag() メソッドを GeneratedBarcode にアタッチします。 この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'")データURL形式よりもHTMLタグ形式を選ぶべき場合は?
次の要件がある場合は、HTMLタグ形式を選択してください。
- クリーンで読みやすいHTML出力
- 既存のHTMLテンプレートとの容易な統合
- HTMLエディタおよびCMSシステムとの互換性
- コンテンツ制作者のための直接コピーペースト機能
HTMLタグ形式は、Blazorアプリケーションでのコンポーネントへの動的なバーコード画像の注入に特に適しています。
バーコードをHTMLファイルとして保存する方法は?
GeneratedBarcode を HTML ファイルとして保存するには、SaveAsHtmlFile() メソッドを使用します。 次のコードは、この方法を示しています:
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ファイルは、特定の使用ケースに明確な利点を提供します:
- スタンドアロンのドキュメント: 印刷可能なバーコードシートを生成
- メール添付ファイル: 自己完結型のバーコードファイルを送信
- アーカイブ目的: 適切な構造でバーコードを保存
- テストおよびデバッグ: バーコードを独立して表示
- バッチ処理: 配布のための複数のファイルを生成
以下は、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コードのような特殊な形式を扱う際には、様々なビジネスニーズに対応したQRコードを作成しカスタマイズするための包括的なガイダンスを提供するC#でQRコードを生成するチュートリアルをご利用ください。
よくある質問
IronBarcode を使用して、C# バーコードを HTML としてエクスポートする方法は?
IronBarcode では、C# バーコードを HTML としてエクスポートする際に、データURLによるインライン使用、HTMLタグによる直接埋め込み、またはスタンドアロンのHTMLファイルとしてエクスポートする3つの形式を提供します。これにより、外部依存なしで多用途な統合が可能です。
バーコードエクスポートにデータURLを使用する利点は何ですか?
バーコードエクスポートにデータURLを使用すると、HTTP リクエストが削減され、ページのパフォーマンスが向上します。そのため、単一ページアプリケーション、メールテンプレート、オフラインアプリ、ファイル管理が煩雑な動的バーコード生成に最適です。
WebブラウザーでデータURLを使用する際のサイズ制限は何ですか?
現代のブラウザーはいくつかのメガバイトをサポートしていますが、実際のデータURLのサイズ制限には、インターネットエクスプローラー8での32KB、現代のブラウザでの2-4MB、メールクライアントでの8-64KBが含まれ、最適なパフォーマンスのためには32KBが推奨されます。
データ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ファイルの生成は、印刷用タグのための小売POSシステム、ラベル付けのための倉庫管理、報告書のための文書管理、品質管理、およびスキャン可能なコードを備えたチケットのためのイベント管理に有益です。