バーコードジェネレーター.NETチュートリアル
バーコード使用の急速な普及を考えると、開発者は自分の好みのプログラミング言語でバーコードを生成できる必要があります。 このチュートリアルでは、.NETでバーコードを生成する方法を示します。
バーコードジェネレーター.NETチュートリアル
- Visual Studio でのプロジェクト作成
- C# バーコードジェネレータライブラリのインストール
- WindowsフォームアプリケーションのUIデザイン
- コア機能のコードを書く
- .NETバーコードジェネレーターを実行する
チュートリアルを始めましょう。
プロジェクトを作成する
このチュートリアルでは、Visual Studioの最新バージョンとWindowsフォームアプリケーションテンプレートを使用します。 お好みのアプリケーションを使用し、既存のプロジェクトとバージョンを使用することができます。
Visual Studioを開く > 新しいプロジェクトを作成をクリック > Windowsフォームアプリケーションテンプレートを選択 > 次へを押す > プロジェクトに名前を付ける > 次へを押す => 対象 for .NET Frameworkを選択 => 作成ボタンをクリック。
新しいWindowsフォームアプリケーションを作成
バーコードライブラリのインストール
バーコードジェネレーターライブラリをインストールすることには多くの利点があります。 C#で書かれたIronBarcodeは、1行のコードでバーコードとQRコードを作成する機能を提供します。 また、QRコードやバーコードを希望のファイル形式で保存することもサポートしています。 さらに、.NETでバーコードを生成するための無料サービスとランタイムサポートを提供しています。
IronBarcode NuGetパッケージのインストールから始めましょう。 以下の3つの方法のうちのいずれかでインストールできます。
パッケージマネージャコンソール
パッケージマネージャコンソールで次のコマンドを書きます。 これにより、パッケージがダウンロードされインストールされます。
パッケージマネージャコンソールインストールステップ
NuGetパッケージソリューション
NuGetパッケージソリューションを使用してバーコードパッケージをインストールすることもできます。 次の手順に従うだけです。
ツールをクリック > NuGetパッケージマネージャ > ソリューションのNuGetパッケージ管理。
これにより、NuGetパッケージマネージャが開きます。 ブラウズをクリックし、"IronBarcode"を検索し、ライブラリをインストールします。
NuGetパッケージマネージャUI
リンクからのダウンロード
代替として、IronBarCode.Dllをダウンロードし、.NETバーコードDLLから参照としてプロジェクトに追加することができます。
Windowsフォームをデザインする
.NETバーコードジェネレーターのUIには2つのラベル、1つのリッチテキストボックス、生成されたバーコード画像を表示する1つのピクチャーボックスがあります。 以下の画像は、デモンストレーションを目的としたシンプルなデザインを示しています。
Windowsフォームアプリケーションをデザイン
バーコード生成コードを書く
"Generate"ボタンをダブルクリックします。 次のコードが表示されます。
private void button1_Click(object sender, EventArgs e)
{
// This function will be triggered when the "Generate" button is clicked
}Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
' This function will be triggered when the "Generate" button is clicked
End SubPrivate Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
' This function will be triggered when the "Generate" button is clicked
End Sub次の名前空間をコードファイルの先頭に追加します。
using IronBarCode; // Import the IronBarCode library to handle barcode operations
using System.Drawing; // Import for image manipulation
using System.Windows.Forms; // Import for Windows Forms functionalityusing IronBarCode; // Import the IronBarCode library to handle barcode operations
using System.Drawing; // Import for image manipulation
using System.Windows.Forms; // Import for Windows Forms functionalityImports IronBarCode ' Import the IronBarCode library to handle barcode operations
Imports System.Drawing ' Import for image manipulation
Imports System.Windows.Forms ' Import for Windows Forms functionalityImports IronBarCode ' Import the IronBarCode library to handle barcode operations
Imports System.Drawing ' Import for image manipulation
Imports System.Windows.Forms ' Import for Windows Forms functionality次のコードをbutton1_Click()関数内に記述します:
// Generate a barcode with the specified value and encoding
GeneratedBarcode MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode(BarcodeValue.Text, BarcodeWriterEncoding.Code128);
// Save the generated barcode as a PNG file
MyBarCode.SaveAsPng("MyBarCode.png");
// Display the generated barcode image in the PictureBox
BarcodeImage.Image = new Bitmap("MyBarCode.png");// Generate a barcode with the specified value and encoding
GeneratedBarcode MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode(BarcodeValue.Text, BarcodeWriterEncoding.Code128);
// Save the generated barcode as a PNG file
MyBarCode.SaveAsPng("MyBarCode.png");
// Display the generated barcode image in the PictureBox
BarcodeImage.Image = new Bitmap("MyBarCode.png");' Generate a barcode with the specified value and encoding
Dim MyBarCode As GeneratedBarcode = IronBarCode.BarcodeWriter.CreateBarcode(BarcodeValue.Text, BarcodeWriterEncoding.Code128)
' Save the generated barcode as a PNG file
MyBarCode.SaveAsPng("MyBarCode.png")
' Display the generated barcode image in the PictureBox
BarcodeImage.Image = New Bitmap("MyBarCode.png")' Generate a barcode with the specified value and encoding
Dim MyBarCode As GeneratedBarcode = IronBarCode.BarcodeWriter.CreateBarcode(BarcodeValue.Text, BarcodeWriterEncoding.Code128)
' Save the generated barcode as a PNG file
MyBarCode.SaveAsPng("MyBarCode.png")
' Display the generated barcode image in the PictureBox
BarcodeImage.Image = New Bitmap("MyBarCode.png")コードを行ごとに理解しましょう。
-
GeneratedBarcodeは生成されたバーコードを表すデータ型です。 -
BarcodeWriterクラスの関数です。 -
BarcodeValue.Textはユーザーが入力したテキストを取得し、バーコードにエンコードされます。 -
BarcodeWriterEncoding.Code128はバーコードを生成するためのエンコーディング方式を指定します。 これをQRコードを生成するためのBarcodeWriterEncoding.QRCodeなど他のエンコーディングタイプに変更できます。 -
SaveAsPng("MyBarCode.png")はバーコード画像をPNGファイルとして保存します。 -
BarcodeImageは、フォーム上でバーコード画像をユーザーに表示するためのPictureBoxコントロールです。
.NETバーコードジェネレーターを実行する
アプリケーションを実行するにはCtrl + F5を押してください。
バーコードジェネレーターアプリケーションを実行
バーコードにエンコードしたい値を以下のようにテキストボックス内に書き込みます。
URLを貼り付けてバーコードを生成
次に、"Generate"ボタンをクリックします。 以下のようにバーコードが生成されます。
Windowsフォームアプリケーションにおいて生成されたバーコード
バーコードの値を表示する
次に、1行のコードでバーコードの値を表示できます。
// Add the encoded barcode value as text below the barcode image
MyBarCode.AddBarcodeValueTextBelowBarcode();// Add the encoded barcode value as text below the barcode image
MyBarCode.AddBarcodeValueTextBelowBarcode();' Add the encoded barcode value as text below the barcode image
MyBarCode.AddBarcodeValueTextBelowBarcode()' Add the encoded barcode value as text below the barcode image
MyBarCode.AddBarcodeValueTextBelowBarcode()出力
文字列値からバーコードを生成
まとめ
IronBarcode は、リアルワールドのソフトウェアでの精度を最適化し、低エラー率を保証するために、.NET 用のバーコードを読み書きするためのフレンドリーなAPIを特徴としています。 IronBarcodeに関する詳細については公式ドキュメントページをご覧ください。
現在、完全なIron Suiteを購入すると、2つの価格で5つのライブラリが手に入ります。 詳細については。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。