C#でHTMLとしてバーコードを作成する方法

C#でBarCodeをHTMLとしてエクスポートする方法</#35;

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

IronBarcodeはC#の開発者が生成されたバーコードを3つのフォーマットでHTMLとしてエクスポートすることを可能にします:インライン埋め込み用のデータURL、ダイレクトインジェクション用のHTMLタグ、スタンドアロン用の完全なHTMLファイルです。

クイックスタート: 1行でHTMLタグとしてバーコードをエクスポートする

1行の流暢なコードを使用して、バーコードを生成し、完全な形式の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としても知られています)は、URL文字列に直接データを埋め込むUniform Resource Identifierです。 これにより、データが外部リソースであるかのように、ウェブページにインライン表示することができます。 データURLは、テキスト、画像、音声、動画、バイナリデータなど、あらゆる形式に対応しています。 取得したData URLを、src属性として画像タグ内のHTMLで使用してください。 ここでは、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()メソッドを使用してバーコードを作成します。 GeneratedBarcodeToDataUrl()メソッドをアタッチして、データ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
$vbLabelText   $csharpLabel

データURLのサイズ制限は何ですか?

最近のブラウザは、技術的には数メガバイトのデータURLをサポートしていますが、現実的には限界があります:

  • Internet Explorer 8:32KBに制限
  • 最新ブラウザ: 2-4MBをサポートしますが、パフォーマンスは低下します。
  • モバイルブラウザ: メモリの制約により制限が厳しくなっています。
  • メールクライアント:データURLを8-64KBに制限する

最適なパフォーマンスを実現するため、Data URL BarCodeは32KB未満に抑えてください。 大きなバーコードや複数のバーコード生成には、export as stream 機能を使用して効率的なメモリ管理を行います。

バーコードを HTML タグとしてエクスポートするにはどうすればよいですか?

ToHtmlTag()メソッドを使用して、GeneratedBarcodeをHTMLにエクスポートします。 このメソッドは、GeneratedBarcodeオブジェクトを、JavaScript、CSS、または画像に依存することなく、HTMLに直接注入するための完全な形のHTMLタグとしてレンダリングします。 次のコードは、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

GeneratedBarcodeToHtmlTag()メソッドをアタッチして、生成されたBarCodeのHTMLタグを取得します。 この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
$vbLabelText   $csharpLabel

生成された 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'")
$vbLabelText   $csharpLabel

データURL形式よりもHTMLタグを選ぶべきときは?

必要な場合は、HTMLタグ形式を選択してください:

  • クリーンで読みやすいHTML出力
  • 既存のHTMLテンプレートとの容易な統合
  • HTMLエディタおよびCMSシステムとの互換性
  • コンテンツ制作者のための直接コピーペースト機能

HTMLタグ形式は、コンポーネントに動的にバーコード画像を注入するBlazorアプリケーションで特に効果的です。

バーコードを HTML ファイルとして保存するにはどうすればよいですか?

GeneratedBarcodeSaveAsHtmlFile()メソッドを使用して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")
$vbLabelText   $csharpLabel

このメソッドは、ファイルパス文字列を受け付けます。 生成されたHTMLファイルは、完全なHTMLファイルを形成する適切な<html><head><body>タグ内のHTMLタグとしてBarCodeを含んでいます。複数のファイル形式を持つ複雑なシナリオについては、出力データ形式ガイドを参照してください。

なぜ断片ではなく完全な 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
$vbLabelText   $csharpLabel

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アプリケーションでダイナミックバーコードを生成する場合に特に有益です。

Hairil Hasyimi Bin Omar
ソフトウェアエンジニア
すべての優れたエンジニアのように、ハイリルは熱心な学習者です。彼はC#、Python、Javaの知識を磨き、その知識を活用してIron Softwareのチームメンバーに価値を追加しています。ハイリルはマレーシアのマラ工科大学からIron Softwareのチームに参加し、化学およびプロセス工学の学士号を取得しました。
準備はできましたか?
Nuget ダウンロード 2,070,733 | バージョン: 2026.2 リリース