How to Generate Barcode Images in C# .NET Applications
.NETアプリケーションで、ProfessionalなBarCode画像を素早く生成したいですか? このチュートリアルでは、IronBarcode を使用してバーコードを作成、カスタマイズ、エクスポートする方法を詳しく解説します。単純な 1 行の実装から、バーコードの外観を完全に制御できる高度なスタイリング手法まで、幅広く紹介します。
クイックスタート:BarCode画像を即座に作成・保存
IronBarcode を使用すれば、たった 1 回の簡単な呼び出しで BARCODE 画像を生成・エクスポートできます。 テキストに CreateBarcode メソッドを使用し、書式とサイズを選択してから SaveAsPng を呼び出してください。複雑な設定は不要です。
最小限のワークフロー(5ステップ)
- NuGet パッケージ マネージャーを使用して IronBarcode をインストールする
- 1行のコードでシンプルなBARCODEを生成する
- BARCODEにカスタムスタイルと注釈を適用する
- BarCodeを画像、PDF、またはHTMLとしてエクスポート
- Fluent API を使用した効率的な BarCode 生成
How Do I Install a Barcode Generator Library in C#?
NuGet パッケージ マネージャーを使用すれば、IronBarcode のインストールはわずか数秒で完了します。 パッケージマネージャーコンソールから直接インストールするか、DLLを手動でダウンロードすることができます。
Install-Package BarCode
IronBarcodeは、.NET開発者向けに包括的なBarCode生成機能を提供します
How Can I Generate a Simple Barcode Using C#?
最初のBARCODEを作成するには、たった2行のコードで済みます。 以下の例は、標準的な Code128 BARCODEを生成し、画像ファイルとして保存する方法を示しています。
using IronBarCode;
// Create a barcode with your desired content and encoding type
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Co/de128);
// Save the barcode as a PNG image file
myBarcode.SaveAsPng("myBarcode.png");
// Optional: Open the generated image in your default viewer
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("myBarcode.png") { UseShellExecute = true });
using IronBarCode;
// Create a barcode with your desired content and encoding type
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Co/de128);
// Save the barcode as a PNG image file
myBarcode.SaveAsPng("myBarcode.png");
// Optional: Open the generated image in your default viewer
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("myBarcode.png") { UseShellExecute = true });
Imports IronBarCode
Imports System.Diagnostics
' Create a barcode with your desired content and encoding type
Dim myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128)
' Save the barcode as a PNG image file
myBarcode.SaveAsPng("myBarcode.png")
' Optional: Open the generated image in your default viewer
Process.Start(New ProcessStartInfo("myBarcode.png") With {.UseShellExecute = True})
BarcodeWriter.CreateBarcode() メソッドは、BarCode生成のエントリポイントです。 この関数は、エンコードしたいデータと、BarcodeWriterEncoding列挙型から指定するBARCODE形式の2つのパラメータを受け取ります。 IronBarcodeは、DataMatrix、および QRCode コードを含む、すべての主要なバーコード形式をサポートしています。
生成された GeneratedBarcode オブジェクトは、複数のエクスポートオプションを提供します。 さまざまな画像形式(PNG、JPEG、GIF、TIFF)で保存したり、PDFとしてエクスポートしたり、さらにはアプリケーション内でのさらなる処理のためにSystem.Drawing.Bitmapとして取得することも可能です。
*A Code128 barcode generated with IronBarcode displaying a URL*
生成されたBARCODEの外観をカスタマイズすることはできますか?
IronBarcodeは、基本的なBarCode生成をはるかに超えた、幅広いカスタマイズオプションを提供します。 注釈の追加、色の調整、余白の設定など、BARCODEの外観に関するあらゆる側面を制御できます。
using IronBarCode;
using IronSoftware.Drawing;
// Create a QR code with advanced styling options
GeneratedBarcode myBarCode = BarcodeWriter.CreateBarcode(
"https://ironsoftware.com/csharp/barcode",
BarcodeWriterEncoding.QRCode
);
// Add descriptive text above the barcode
myBarCode.AddAnnotationTextAboveBarcode("Product URL:");
// Display the encoded value below the barcode
myBarCode.AddBarcodeValueTextBelowBarcode();
// Set consistent margins around the barcode
myBarCode.SetMargins(100);
// Customize the barcode color (purple in this example)
myBarCode.ChangeBarCodeColor(Color.Purple);
// Export as an HTML file for web integration
myBarCode.SaveAsHtmlFile("MyBarCode.html");
using IronBarCode;
using IronSoftware.Drawing;
// Create a QR code with advanced styling options
GeneratedBarcode myBarCode = BarcodeWriter.CreateBarcode(
"https://ironsoftware.com/csharp/barcode",
BarcodeWriterEncoding.QRCode
);
// Add descriptive text above the barcode
myBarCode.AddAnnotationTextAboveBarcode("Product URL:");
// Display the encoded value below the barcode
myBarCode.AddBarcodeValueTextBelowBarcode();
// Set consistent margins around the barcode
myBarCode.SetMargins(100);
// Customize the barcode color (purple in this example)
myBarCode.ChangeBarCodeColor(Color.Purple);
// Export as an HTML file for web integration
myBarCode.SaveAsHtmlFile("MyBarCode.html");
Imports IronBarCode
Imports IronSoftware.Drawing
' Create a QR code with advanced styling options
Private myBarCode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode)
' Add descriptive text above the barcode
myBarCode.AddAnnotationTextAboveBarcode("Product URL:")
' Display the encoded value below the barcode
myBarCode.AddBarcodeValueTextBelowBarcode()
' Set consistent margins around the barcode
myBarCode.SetMargins(100)
' Customize the barcode color (purple in this example)
myBarCode.ChangeBarCodeColor(Color.Purple)
' Export as an HTML file for web integration
myBarCode.SaveAsHtmlFile("MyBarCode.html")
GeneratedBarcode クラスは、カスタマイズのための豊富なメソッドセットを提供します:
- 注釈: BARCODEの周囲にカスタムラベルや指示を追加するには、
AddAnnotationTextAboveBarcode()およびAddAnnotationTextBelowBarcode()を使用してください - 値の表示:
AddBarcodeValueTextBelowBarcode()メソッドは、エンコードされたデータを人間が読みやすい形式で自動的に表示します - スペース: 適切なスキャンと視覚的な見やすさを確保するため、
SetMargins()を使用して空白を制御してください - 色:
ChangeBarCodeColor()およびChangeBackgroundColor()を使用して前景色と背景色を変更します - エクスポートオプション:画像ファイル、PDF、または独立したHTMLドキュメントとして保存
*カスタムカラーと注釈テキストを適用したQRコード*詳細なカスタマイズオプションについては、利用可能なすべてのスタイル設定メソッドとプロパティを網羅した GeneratedBarcode クラスのドキュメントを参照してください。
1行のコードでBARCODEを作成してエクスポートするにはどうすればよいですか?
IronBarcodeは、メソッドチェーンを可能にするFluent APIデザインパターンを実装しており、より簡潔で読みやすいコードを実現します。 このアプローチは、BARCODEに複数の変換を適用する場合に特に有用です。
using IronBarCode;
using IronSoftware.Drawing;
// Generate, style, and convert a barcode in a single statement
string value = "https://ironsoftware.com/csharp/barcode";
// Create PDF417 barcode with chained operations
AnyBitmap barcodeBitmap = BarcodeWriter
.CreateBarcode(value, BarcodeWriterEncoding.PDF417) // Create PDF417 barcode
.ResizeTo(300, 200) // Set specific dimensions
.SetMargins(10) // Add 10px margins
.ToBitmap(); // Convert to bitmap
// Convert to System.Drawing.Bitmap for legacy compatibility
System.Drawing.Bitmap legacyBitmap = barcodeBitmap;
using IronBarCode;
using IronSoftware.Drawing;
// Generate, style, and convert a barcode in a single statement
string value = "https://ironsoftware.com/csharp/barcode";
// Create PDF417 barcode with chained operations
AnyBitmap barcodeBitmap = BarcodeWriter
.CreateBarcode(value, BarcodeWriterEncoding.PDF417) // Create PDF417 barcode
.ResizeTo(300, 200) // Set specific dimensions
.SetMargins(10) // Add 10px margins
.ToBitmap(); // Convert to bitmap
// Convert to System.Drawing.Bitmap for legacy compatibility
System.Drawing.Bitmap legacyBitmap = barcodeBitmap;
Imports IronBarCode
Imports IronSoftware.Drawing
' Generate, style, and convert a barcode in a single statement
Private value As String = "https://ironsoftware.com/csharp/barcode"
' Create PDF417 barcode with chained operations
Private barcodeBitmap As AnyBitmap = BarcodeWriter.CreateBarcode(value, BarcodeWriterEncoding.PDF417).ResizeTo(300, 200).SetMargins(10).ToBitmap() ' Convert to bitmap
' Convert to System.Drawing.Bitmap for legacy compatibility
Private legacyBitmap As System.Drawing.Bitmap = barcodeBitmap
Fluent API パターンには、いくつかの利点があります:
- 読みやすさ: 自然な文章のように読めるよう、操作を論理的な順序で連結してください
- 効率性: 変数宣言や中間処理を削減
- 柔軟性: コードの構造を変更することなく、操作を簡単に追加または削除できます
一般的な流暢な操作には以下が含まれます:
ResizeTo(): BarCodeの正確な寸法を制御するSetMargins(): スペースを統一して追加ChangeBarCodeColor(): 表示形式を変更AddAnnotationTextAboveBarcode(): 説明文を含めるToBitmap(),SaveAsPng(),SaveAsPdf(): さまざまな形式でのエクスポート
*A PDF417 barcode generated using fluent method chaining*
IronBarcodeではどのようなBarCode形式がサポートされていますか?
IronBarcodeは、BarcodeWriterEncoding列挙型を通じて、包括的なBARCODE形式の生成をサポートしています。 対応フォーマットは以下の通りです:
1次元BarCode: Code128, Code39, Code93, Codabar, ITF, MSI, Plessey, UPCA, UPCE, EAN8, EAN13
2D BARCODE:MaxiCode
特殊なフォーマット:DataBarExpanded、および各種GS1規格
各フォーマットには、固有の特性とユースケースがあります。 例えば、EAN13は小売製品向けの標準的なツールです。 アプリケーションに適したBarCode形式の選び方について詳しくはこちらをご覧ください。
生成したBARCODEが読み取れるかどうかを確認するにはどうすればよいですか?
BarCodeの実装においては、品質保証が極めて重要です。 IronBarcodeには、生成されたBarCodeが確実に読み取れるようにするための検証機能が組み込まれています:
// Generate and verify a barcode
GeneratedBarcode myBarcode = BarcodeWriter
.CreateBarcode("TEST123", BarcodeWriterEncoding.Co/de128)
.ResizeTo(200, 100)
.ChangeBarCodeColor(Color.DarkBlue);
// Verify the barcode is still readable after modifications
bool isReadable = myBarcode.Verify();
Console.WriteLine($"Barcode verification: {(isReadable ? "PASS" : "FAIL")}");
// Generate and verify a barcode
GeneratedBarcode myBarcode = BarcodeWriter
.CreateBarcode("TEST123", BarcodeWriterEncoding.Co/de128)
.ResizeTo(200, 100)
.ChangeBarCodeColor(Color.DarkBlue);
// Verify the barcode is still readable after modifications
bool isReadable = myBarcode.Verify();
Console.WriteLine($"Barcode verification: {(isReadable ? "PASS" : "FAIL")}");
Imports System.Drawing
' Generate and verify a barcode
Dim myBarcode As GeneratedBarcode = BarcodeWriter _
.CreateBarcode("TEST123", BarcodeWriterEncoding.Code128) _
.ResizeTo(200, 100) _
.ChangeBarCodeColor(Color.DarkBlue)
' Verify the barcode is still readable after modifications
Dim isReadable As Boolean = myBarcode.Verify()
Console.WriteLine($"Barcode verification: {(If(isReadable, "PASS", "FAIL"))}")
Verify() メソッドは、サイズ変更や色変更などの変換を適用した後も、BARCODEが機械で読み取り可能であるかどうかを確認します。 これは、非標準の色や非常に小さなサイズを使用する場合に特に重要です。
BARCODE生成のその他の例はどこで見られますか?
BARCODE生成機能を拡張するには、以下の追加リソースをご確認ください:
ソースコードとサンプル
このチュートリアルの完全なソースコードをダウンロード:
高度なトピック
- QRロゴ入りコードを生成 - QRコードにブランドロゴを追加
- BarCodeスタイリングガイド - 高度なカスタマイズ技術を習得する
- 画像からのBarCode読み取り - BarCodeスキャンでサイクルを完了
- BarCodeの一括生成 - 複数のBarCodeを効率的に生成
APIドキュメント
BarcodeWriterクラスリファレンス - メソッドの完全なドキュメントGeneratedBarcodeクラスリファレンス - すべてのカスタマイズオプションBarcodeWriterEncodingEnum - 対応BarCode形式
アプリケーションでProfessionalなBarCodeを生成する準備はできていますか?
IronBarcodeは、BarCode生成を簡単に行えるようにすると同時に、Professionalな用途に必要な柔軟性を提供します。 単純な製品コードから、カスタムスタイルを適用した複雑な2D BARCODEまで、IronBarcodeなら最小限のコードで全てを処理できます。
今すぐIronBarcodeをダウンロードして、数分でBARCODEの作成を始めましょう。 適切なライセンスの選択にお困りですか? ライセンスオプションをご確認いただくか、無料トライアルキーをリクエストして、本番環境でIronBarcodeをお試しください。
よくある質問
C#でバーコード画像をどのように作成できますか?
C#でバーコード画像を作成するには、IronBarcodeのBarcodeWriter.CreateBarcode()メソッドを使用できます。これにより、データとバーコード形式を指定し、SaveAsPng()などのメソッドを使用してPNGやJPEGの形式で画像を保存できます。
IronBarcodeを.NETプロジェクトにインストールする手順は何ですか?
Visual StudioでNuGetパッケージマネージャーを使用して.NETプロジェクトにIronBarcodeをインストールできます。または、IronBarcodeのウェブサイトからDLLをダウンロードしてプロジェクトリファレンスに追加することもできます。
C#でバーコードをPDFとしてエクスポートするにはどうすれば良いですか?
IronBarcodeを使用すると、GeneratedBarcodeクラスのSaveAsPdf()メソッドを使用してバーコードをPDFとしてエクスポートできます。これはPDF形式でバーコードを保存する簡単な方法を提供します。
C#でバーコードに利用可能なカスタマイズオプションは何ですか?
IronBarcodeは、ChangeBarCodeColor()でバーコードの色を変更したり、AddAnnotationTextAboveBarcode()でテキスト注釈を追加したり、SetMargins()でマージンを設定したりと、幅広いカスタマイズオプションを提供します。
1行のコードでバーコードを迅速に作成しスタイルを設定するにはどうすれば良いですか?
IronBarcodeのフルーエントAPIを使用して、メソッドチェーンを使用した1行のコードでバーコードを作成しスタイルを設定できます: BarcodeWriter.CreateBarcode(data, encoding).ResizeTo(300, 200).SetMargins(10).SaveAsPng(path)。
変更後にバーコードがスキャン可能であることをどうやって確保しますか?
スタイリングやサイズ変更後にバーコードのスキャン可能性を確認するには、Verify()メソッドをGeneratedBarcodeオブジェクトで使用して、その機械読取り可能性をチェックします。
C#でロゴ付きのQRコードを生成することはできますか?
はい、IronBarcodeは、ロゴの挿入とエラーレベルの向上を可能にするQRCodeWriterクラスを使用して埋め込みロゴ付きのQRコード生成をサポートしています。
C#で複数のバーコードを効率的に生成するプロセスはどうなっていますか?
IronBarcodeを使用してC#で複数のバーコードを効率的に生成できます。これはバッチ処理をサポートしており、ループや並列処理を使用して大量のバーコード生成を処理することが可能です。
C#でバーコードをエクスポートするために使用できるファイル形式は何ですか?
IronBarcodeは、PNG、JPEG、GIF、TIFF、BMP、PDF、HTMLなどの様々な形式でのバーコードエクスポートをサポートし、さまざまなアプリケーションニーズに対応する柔軟性を提供します。
C#でバーコードの下に人間が読めるテキストを追加するにはどうすればよいですか?
C#でバーコードの下に人間が読めるテキストを追加するには、AddBarcodeValueTextBelowBarcode()メソッドを使用して、エンコードされた値をバーコード画像の下にテキスト形式で自動的に表示します。

