フッターコンテンツにスキップ
IRONBARCODEの使用

C#でCode128バーコードを生成する方法

バーコードは、在庫管理から製品ラベル付け、出荷に至るまで、現代のビジネス運用において非常に重要です。 Code 128は、さまざまなバーコードコードセットの中で、多用途で広く使用されているオプションとして際立っています。 この記事では、IronBarcodeライブラリを使用してC#でCode 128バーコードジェネレーターを構築する方法を探ります。

Code 128バーコードをC#で生成する方法

  1. IronBarcodeライブラリをインストールする
  2. Code 128エンコーディングを使用してバーコードを生成する
  3. バーコードをリサイズする
  4. 背景色とバーコードの色を変更してバーコードをスタイル化する
  5. 作成されたバーコードを読み取る

Code 128バーコードの紹介

Code 128コードセットは、高密度で可変長の線形バーコードで、英数字データと特殊文字の両方をエンコードできます。 これは自己チェックであり、データの正確性を確保するためのチェックサム数字を含んでいます。 Code 128エンコーディングスキームは、3つの制御文字をサポートしています:

  1. 文字セットA: 大文字、数字、および特殊文字を含みます。
  2. 文字セットB: 大文字、小文字、数字、および追加の特殊文字を含みます。
  3. 文字セットC: 数字のペア(00から99)をエンコードします。

なぜIronBarcodeを選ぶべきか?

IronBarcodeは、バーコードの生成、デコーディング、およびカスタマイズを容易にする強力な.NETライブラリです。 With support for various Barcode Encoding like Code 128, Code 39, Code 93, Code EAN 13, EAN 8, QR codes, and others. コンテンツ、サイズ、および外観調整のための直感的なAPIを提供します。 そのデコード機能、自動チェックサム計算、および画像エクスポートは、在庫管理およびそれ以外の分野で開発者にとって貴重なツールとなります。 ライブラリのプロパティ、余白、フォント、色に関するカスタマイズオプションは、バーコード関連のタスクでの汎用性を向上させます。

C#でCode 128バーコードジェネレーターの作成

これで、C#でCode 128バーコード画像を生成するコードを書きます。 最初のステップは、プロジェクトにIronBarcodeライブラリをインストールすることです。 プロジェクトは、Windows Forms、Webフォーム、MAUI、Xamarin、ASP.NET MVC、Razor、またはBlazorプロジェクトなど、どのタイプでもかまいません。

IronBarcodeライブラリのインストール

Visual Studioのパッケージマネージャーコンソールを使用してIronBarcode NuGetパッケージをインストールするには、次の手順に従います:

  1. Visual Studioを開きます。
  2. 上部メニューで「表示」>「その他のウィンドウ」>「パッケージマネージャーコンソール」に進み、パッケージマネージャーコンソールを開きます。
  3. パッケージマネージャーコンソールで、Install-Packageコマンドを使用してIronBarcodeパッケージをインストールします。 次のコマンドを入力し、Enterキーを押します:

    Install-Package BarCode
    Install-Package BarCode
    SHELL
  4. このコマンドにより、IronBarcode NuGetパッケージの最新バージョンとその依存関係がプロジェクトにダウンロードされインストールされます。

プロジェクトでバーコードライブラリを使用するには次の名前空間を追加します。

using IronBarCode;
using IronBarCode;
Imports IronBarCode
$vbLabelText   $csharpLabel

Code 128バーコード画像を生成する

次のコードはCode 128バーコードを生成します。

// Create a barcode from the input string and specify encoding type as Code 128
var myBarcode = BarcodeWriter.CreateBarcode("12345ABC12345", BarcodeWriterEncoding.Code128);

// Save the barcode image as a JPEG file
myBarcode.SaveAsJpeg("myBarcode.Jpeg");
// Create a barcode from the input string and specify encoding type as Code 128
var myBarcode = BarcodeWriter.CreateBarcode("12345ABC12345", BarcodeWriterEncoding.Code128);

// Save the barcode image as a JPEG file
myBarcode.SaveAsJpeg("myBarcode.Jpeg");
' Create a barcode from the input string and specify encoding type as Code 128
Dim myBarcode = BarcodeWriter.CreateBarcode("12345ABC12345", BarcodeWriterEncoding.Code128)

' Save the barcode image as a JPEG file
myBarcode.SaveAsJpeg("myBarcode.Jpeg")
$vbLabelText   $csharpLabel

このコードは入力文字列からバーコードを作成し、「myBarcode.Jpeg」という名前のJPEG画像ファイルとして保存します。使用される特定のエンコーディングは、英数字文字を表現できるCode 128です。

説明

コードの最初の行は、myBarcodeという名前の新しい変数を作成します。 入力文字列「12345ABC12345」に基づいてバーコードを生成するために、BarcodeWriter.CreateBarcodeメソッドを使用します。

二番目の引数であるBarcodeWriterEncoding.Code128は、バーコードのエンコーディングタイプを指定します。 この場合、英数字データに一般的に使用されるCode 128エンコーディングを使用しています。 生成されたバーコードはmyBarcode変数に格納されます。

二行目は、生成されたバーコードをJPEG画像ファイルとして保存します。保存される画像のファイル名は「myBarcode.Jpeg」です。 保存される画像の形式はJPEG(Joint Photographic Experts Group)です。

出力

生成されたバーコードは以下のとおりです:

C♯でのCode 128バーコードの生成方法: 図1 - 前回のコードから出力されたバーコード

このコードはバーコードリーディングデバイスを使用して今すぐ読み取ることができます。

さて、バーコードのサイズを変更しましょう。

バーコードのサイズ変更

次のコードは、指定された寸法に従ってバーコードのサイズを変更します。

static void Main(string[] args)
{
    // Create a barcode from the input string and specify encoding type as Code 128
    var myBarcode = BarcodeWriter.CreateBarcode("12345ABC12345", BarcodeWriterEncoding.Code128);

    // Resize the barcode image to the specified width and height (in pixels)
    myBarcode.ResizeTo(800, 300);

    // Save the resized barcode image as a JPEG file
    myBarcode.SaveAsJpeg("myBarcode.Jpeg");
}
static void Main(string[] args)
{
    // Create a barcode from the input string and specify encoding type as Code 128
    var myBarcode = BarcodeWriter.CreateBarcode("12345ABC12345", BarcodeWriterEncoding.Code128);

    // Resize the barcode image to the specified width and height (in pixels)
    myBarcode.ResizeTo(800, 300);

    // Save the resized barcode image as a JPEG file
    myBarcode.SaveAsJpeg("myBarcode.Jpeg");
}
Shared Sub Main(ByVal args() As String)
	' Create a barcode from the input string and specify encoding type as Code 128
	Dim myBarcode = BarcodeWriter.CreateBarcode("12345ABC12345", BarcodeWriterEncoding.Code128)

	' Resize the barcode image to the specified width and height (in pixels)
	myBarcode.ResizeTo(800, 300)

	' Save the resized barcode image as a JPEG file
	myBarcode.SaveAsJpeg("myBarcode.Jpeg")
End Sub
$vbLabelText   $csharpLabel

バーコードを作成して保存するためのコードは変わりません。 バーコードのサイズを変更するために追加の行を追加するだけです。

ResizeTo()メソッドはmyBarcode変数に格納されたバーコード画像のサイズを変更します。 ResizeToメソッドはmyBarcodeオブジェクト上で呼び出されます。 ResizeToに渡される2つの引数は幅と高さです。 この場合、幅は800ピクセル、高さは300ピクセルに設定されています。

この方法で、最小幅とバーのモジュールの高さを設定できます。 リサイズ後のバーコード画像は以下の寸法になります。

バーコード画像

C♯でのCode 128バーコードの生成方法: 図2 - 前回のコードからリサイズされたバーコード

さて、バーコードをスタイル化しましょう。

Code 128バーコードのスタイル設定

さて、バーコードの背景色とバーコード色を変更してスタイルを付けましょう。

static void Main(string[] args)
{
    // Create a barcode from the input string and specify encoding type as Code 128
    var myBarcode = BarcodeWriter.CreateBarcode("12345ABC12345", BarcodeWriterEncoding.Code128);

    // Resize the barcode image to the specified width and height (in pixels)
    myBarcode.ResizeTo(800, 300);

    // Change the background color of the barcode
    myBarcode.ChangeBackgroundColor(IronSoftware.Drawing.Color.Cornsilk);

    // Change the barcode color
    myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Brown);

    // Save the styled barcode image as a JPEG file
    myBarcode.SaveAsJpeg("myBarcode.Jpeg");
}
static void Main(string[] args)
{
    // Create a barcode from the input string and specify encoding type as Code 128
    var myBarcode = BarcodeWriter.CreateBarcode("12345ABC12345", BarcodeWriterEncoding.Code128);

    // Resize the barcode image to the specified width and height (in pixels)
    myBarcode.ResizeTo(800, 300);

    // Change the background color of the barcode
    myBarcode.ChangeBackgroundColor(IronSoftware.Drawing.Color.Cornsilk);

    // Change the barcode color
    myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Brown);

    // Save the styled barcode image as a JPEG file
    myBarcode.SaveAsJpeg("myBarcode.Jpeg");
}
Shared Sub Main(ByVal args() As String)
	' Create a barcode from the input string and specify encoding type as Code 128
	Dim myBarcode = BarcodeWriter.CreateBarcode("12345ABC12345", BarcodeWriterEncoding.Code128)

	' Resize the barcode image to the specified width and height (in pixels)
	myBarcode.ResizeTo(800, 300)

	' Change the background color of the barcode
	myBarcode.ChangeBackgroundColor(IronSoftware.Drawing.Color.Cornsilk)

	' Change the barcode color
	myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Brown)

	' Save the styled barcode image as a JPEG file
	myBarcode.SaveAsJpeg("myBarcode.Jpeg")
End Sub
$vbLabelText   $csharpLabel

バーコードを生成して保存するためのコードは同じです。 背景色とバーコード色を変更するために2行を追加しました。 説明は以下のとおりです:

  • ChangeBackgroundColor: メソッドChangeBackgroundColormyBarcodeオブジェクト上で呼び出されます。 このメソッドはバーコード画像の背景色を変更します。 ChangeBackgroundColorに渡される引数はIronSoftware.Drawing.Color.Cornsilkで、指定された背景色を示します。 この場合、背景色はCornsik、淡い黄色がかった色に設定されています。

  • ChangeBarCodeColor: メソッドChangeBarCodeColormyBarcodeオブジェクト上で呼び出されます。 このメソッドはバーコードのバーの色を変更します。 ChangeBarCodeColorに渡される引数はIronSoftware.Drawing.Color.Brownで、指定されたバーコード色を示します。 この場合、バーコードの色はBrownに設定されています。

出力

私たちのスタイル化されたバーコードは以下の通りです:

C♯でのCode 128バーコードの生成方法: 図3 - 前回のコードから出力されたスタイル化されたバーコード

Code 128バーコードの読み取り

私たちはCode 128バーコードを生成する方法を学びました。 バーコードを読み取るコードを書いてみましょう:

static void Main(string[] args)
{
    // Read barcodes from the specified image file
    var resultFromBarcode = BarcodeReader.Read("myBarcode.Jpeg");

    // Loop through each barcode value read from the image
    foreach (var barcodeValue in resultFromBarcode)
    {
        // Print each barcode value to the console
        Console.WriteLine(barcodeValue);
    }
}
static void Main(string[] args)
{
    // Read barcodes from the specified image file
    var resultFromBarcode = BarcodeReader.Read("myBarcode.Jpeg");

    // Loop through each barcode value read from the image
    foreach (var barcodeValue in resultFromBarcode)
    {
        // Print each barcode value to the console
        Console.WriteLine(barcodeValue);
    }
}
Shared Sub Main(ByVal args() As String)
	' Read barcodes from the specified image file
	Dim resultFromBarcode = BarcodeReader.Read("myBarcode.Jpeg")

	' Loop through each barcode value read from the image
	For Each barcodeValue In resultFromBarcode
		' Print each barcode value to the console
		Console.WriteLine(barcodeValue)
	Next barcodeValue
End Sub
$vbLabelText   $csharpLabel

上記のコードは「myBarcode.Jpeg」画像ファイルからバーコードを読み取り、その値をコンソールに表示します。 BarcodeReaderクラスは画像からバーコードデータをデコードする役割を担っています。 コードの説明は以下のとおりです:

説明 of Code

  • 最初の行はresultFromBarcodeという名前の変数を作成します。 それは「myBarcode.Jpeg」という名前の画像ファイルからバーコードを読み取るためにBarcodeReader.Readメソッドを呼び出します。 この操作の結果はresultFromBarcode変数に格納されます。

  • 二行目は、resultFromBarcodeコレクション内の各バーコード値を繰り返すループを開始します。 foreachループによって、バーコードの値を1つずつ処理できます。

  • ループの中で、この行は各バーコード値をコンソールに出力します。 barcodeValueは、画像から読み取られたバーコードの内容を表します。

バーコードの値は次のようにコンソールに表示されます。

出力

C♯でのCode 128バーコードの生成方法: 図4 - 読み取ったバーコードのコンソール出力

結論

結論として、この記事ではIronBarcodeライブラリを使用してC#でCode 128バーコードジェネレーターを作成する方法を示しました。 By leveraging the capabilities of IronBarcode, developers can easily generate, customize, and style Code 128 barcodes for various applications, including inventory management, product labeling, and shipping. このチュートリアルに従うことで、開発者はC#プロジェクトに強力なバーコード機能を組み込み、バーコード生成およびデコーダーに関連するタスクの効率的な処理を向上させることができます。 IronBarcodeの多様性と直感的なAPIは、バーコード実装を含むアプリケーションに取り組む開発者にとって貴重なツールです。

IronBarcodeライブラリでCode 128バーコードを生成する過程で、開発者はコードセット選択キャラクターをカスタマイズする柔軟性を持ち、さまざまなASCII値を持つデータの最適なエンコーディングを保証します。 レンダリングコードは、好みの画像フォーマットにシームレスに適応し、開発者にJPEGまたは他のフォーマットでバーコードを保存する選択肢を提供します。 さらに、ストップキャラクターの追加により、生成されたバーコード内のエンコードされた情報の正確な終了が保証されます。

IronBarcodeは無料トライアルを提供しており、開発のニーズに合わせてライブラリの完全な可能性を引き出すことができます。 このアプローチにより、開発者は商用ライセンスを取得する前にIronBarcodeの機能を評価できます。

よくある質問

C#でCode 128バーコードを生成するにはどうすればいいですか?

C#でCode 128バーコードを生成するには、IronBarcodeライブラリのBarcodeWriter.CreateBarcodeメソッドを使用し、希望の入力文字列とCode 128をエンコーディングタイプとして指定します。その後、SaveAsJpegなどのメソッドを使用して生成されたバーコード画像をエクスポートできます。

Code 128は何に使用されますか?

Code 128は、アルファベットと数字、および特殊文字をコンパクトなバーコード形式でエンコードするために使用されます。それは、その高いデータ密度と汎用性のために、在庫管理、製品ラベル付け、出荷アプリケーションに理想的です。

IronBarcodeを使用してバーコードの外観をカスタマイズできますか?

はい、IronBarcodeを使用して、ChangeBackgroundColorおよびChangeBarCodeColorで色を変更し、ResizeToメソッドを使用してサイズ変更することで、バーコードの外観をカスタマイズできます。

C#でバーコードを読み取るにはどうすればいいですか?

C#でバーコードを読み取るには、IronBarcodeライブラリのBarcodeReader.Readメソッドを使用します。このメソッドは、バーコードを含む画像ファイルを処理し、さらなる処理のためにデコードされた値を返します。

バーコード生成にIronBarcodeライブラリを使用する利点は何ですか?

IronBarcodeライブラリは、ユーザーフレンドリーなAPIを提供し、さまざまなバーコードエンコードをサポートし、自動チェックサム計算を提供し、複数の形式で画像をエクスポートできるため、バーコード生成とカスタマイズのための柔軟で効率的なツールです。

IronBarcodeがエクスポートできる画像形式は何ですか?

IronBarcodeは、JPEGなどの様々な形式でバーコード画像をエクスポート可能です。

購入前にIronBarcodeの試用が可能ですか?

はい、IronBarcodeの無料試用版を活用して、機能を試すことができます。C#プロジェクトへの統合も確認できます。

Jordi Bardia
ソフトウェアエンジニア
Jordiは、最も得意な言語がPython、C#、C++であり、Iron Softwareでそのスキルを発揮していない時は、ゲームプログラミングをしています。製品テスト、製品開発、研究の責任を分担し、Jordiは継続的な製品改善において多大な価値を追加しています。この多様な経験は彼を挑戦させ続け、興味を持たせており、Iron Softwareで働くことの好きな側面の一つだと言います。Jordiはフロリダ州マイアミで育ち、フロリダ大学でコンピュータサイエンスと統計学を学びました。