C#でバーコード画像を生成

チャクニット・ビン
チャクニット・ビン
2018年11月5日
更新済み 2024年12月10日
共有:
This article was translated from English: Does it need improvement?
Translated
View the article in English

このチュートリアルでは、IronBarcode ライブラリを使用して C# .NET でバーコードを生成する方法を例とともに見ていきます。

バーコードをC#やVB.NETで作成するのがいかに簡単か、またバーコードのスタイルを設定し、それを画像としてエクスポートする方法を見ていきます。

IronBarcodeを始める

今日から無料トライアルでIronBarcodeをあなたのプロジェクトで使い始めましょう。

最初のステップ:
green arrow pointer



インストール

まず最初に行うべきことは、Iron Barcodeライブラリをインストールし、.NETフレームワークにバーコード機能を追加することです。 これは、NuGetパッケージを使用するか、.NET Barcode DLLをダウンロードすることで実行できます。

Install-Package BarCode
信頼性のあるC# バーコードライブラリがなければ、.NETでバーコードやQRコードを作成することは難しいです。ここで役立つのがIron Barcodeです

簡単なバーコードをレンダリングする

以下の例では、IronBarcodeを使用してわずか数行のコードで数値またはテキストの内容を含むバーコードを作成できることがわかります。

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-barcode-image-generator-1.cs
using IronBarCode;

// Generate a Simple BarCode image and save as PNG
GeneratedBarcode myBarcode = IronBarCode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128);
myBarcode.SaveAsPng("myBarcode.png");

// This line opens the image in your default image viewer
System.Diagnostics.Process.Start("myBarcode.png");
Imports IronBarCode

' Generate a Simple BarCode image and save as PNG
Private myBarcode As GeneratedBarcode = IronBarCode.BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128)
myBarcode.SaveAsPng("myBarcode.png")

' This line opens the image in your default image viewer
System.Diagnostics.Process.Start("myBarcode.png")
$vbLabelText   $csharpLabel

最初に、IronBarCode.BarcodeWriterEncoding列挙型から使用するバーコードの形式とその値を指定してバーコードを作成します。 その後、画像として保存するか、System.Drawing.Image または Bitmap オブジェクトとして保存することを選択できます。 これが必要なすべてのコードです!

C#の例でバーコード画像を作成する

最終行のコードは、例としてバーコードPNGを開くだけで、これにより実際に目で確認することができます。

高度なバーコード作成##

以前の例は効果的でしたが、現実世界ではさらに多くのことを行いたい場合があります。

以下の例では、バーコードに注釈を追加し、フォントを設定し、その値を下に表示し、余白を追加し、バーコードの色を変更し、最後にそれを保存することが、すべてC#で非常に簡単に行えます。

必要に応じて、画像の代わりにHTMLまたはPDF形式でエクスポートすることもできます。これは私たちのアプリケーションにとってより適切な場合もあります。

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-barcode-image-generator-2.cs
using IronBarCode;
using IronSoftware.Drawing;

// Styling a QR code and adding annotation text
GeneratedBarcode myBarCode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode);
myBarCode.AddAnnotationTextAboveBarcode("Product URL:");
myBarCode.AddBarcodeValueTextBelowBarcode();
myBarCode.SetMargins(100);
myBarCode.ChangeBarCodeColor(Color.Purple);

// Save as HTML
myBarCode.SaveAsHtmlFile("MyBarCode.html");
Imports IronBarCode
Imports IronSoftware.Drawing

' Styling a QR code and adding annotation text
Private myBarCode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode)
myBarCode.AddAnnotationTextAboveBarcode("Product URL:")
myBarCode.AddBarcodeValueTextBelowBarcode()
myBarCode.SetMargins(100)
myBarCode.ChangeBarCodeColor(Color.Purple)

' Save as HTML
myBarCode.SaveAsHtmlFile("MyBarCode.html")
$vbLabelText   $csharpLabel
C# を使用して注釈付きおよびスタイル付きのバーコード画像を作成する

コードは自己解説的であるべきですが、そうでない場合は、GeneratedBarcode クラスのドキュメントを API リファレンス で読むことをお勧めします。

フルーエンシー

最後の例では、単一のコード行でバーコードを作成、スタイル設定、エクスポートできることがわかります。

IronBarcodeは、System.Linqに類似したオプションのFluent APIを実装しています。 メソッド呼び出しを連鎖させることにより、まずバーコードを作成し、その後マージンを設定し、最後にBitmap形式でエクスポートするという操作を1行で行います。

これは非常に便利で、コードの可読性を向上させることができます。

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-barcode-image-generator-3.cs
using IronBarCode;
using IronSoftware.Drawing;

// Fluent API for Barcode Image generation.
string value = "https://ironsoftware.com/csharp/barcode";
AnyBitmap barcodeBitmap = BarcodeWriter.CreateBarcode(value, BarcodeEncoding.PDF417).ResizeTo(300, 200).SetMargins(100).ToBitmap();
System.Drawing.Bitmap barcodeLegacyBitmap = (System.Drawing.Bitmap)barcodeBitmap;
Imports IronBarCode
Imports IronSoftware.Drawing

' Fluent API for Barcode Image generation.
Private value As String = "https://ironsoftware.com/csharp/barcode"
Private barcodeBitmap As AnyBitmap = BarcodeWriter.CreateBarcode(value, BarcodeEncoding.PDF417).ResizeTo(300, 200).SetMargins(100).ToBitmap()
Private barcodeLegacyBitmap As System.Drawing.Bitmap = CType(barcodeBitmap, System.Drawing.Bitmap)
$vbLabelText   $csharpLabel

結果は、このようなPDF417バーコードのSystem.Drawing.Imageです:

IronBarcodeを使用したC#でのシンプルでスムーズなバーコード生成

詳細について学ぶ

このコードサンプルとC#でバーコードから画像を読み取る方法の詳細を知るには、GitHubで閲覧するか、Visual Studioプロジェクトとしてダウンロードするか、このセクション内の他の例、特にQRコードを作成する方法に関するチュートリアルをご覧ください。

C#のソースコードのダウンロード

この「バーコード画像生成」チュートリアルのソースは、Visual Studio 2017用のC#バーコード生成コードプロジェクトとして利用できます。

さらなるドキュメント

APIリファレンス内のBarcodeReaderクラスも非常に有用なものとして見つかるかもしれません。 ソフトウェアをC# Barcode Scannerとして使用する方法に関する情報もあります。

さらに、IronBarcode の他の側面に光を当てるかもしれない他のチュートリアルもあります。これには、QR コードバーコード画像の .NET による読み取り が含まれます。

チャクニット・ビン
ソフトウェアエンジニア
ChaknithはIronXLとIronBarcodeで作業しています。彼はC#と.NETに深い専門知識を持ち、ソフトウェアの改善と顧客サポートを支援しています。ユーザーとの対話から得た彼の洞察は、より良い製品、文書、および全体的な体験に貢献しています。