Iron Barcodeを使用して.NET 5用にC#でQRコードとバーコードを生成する方法

Barcodes & QRs in C# & VB.NET Applications

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

C#およびその他のすべて for .NET言語でのBARCODEの読み取りと書き込みは、当社のIronBarcodeライブラリを使用すれば簡単に行えます。

IronBarcode をインストールする

最初のステップは IronBarcode のインストールです。NuGet からダウンロードするか、DLL をダウンロードしてインストールできます。

IronBarcode NuGet パッケージをインストールするには、Visual Studio の NuGet パッケージ マネージャーを使用できます:

Install-Package BarCode

あるいは、.NET CLI を使用してインストールすることもできます:

dotnet add package IronBarCode

BarCodeまたはQRコードの読み取り

IronBarcodeを使えば、BARCODEの読み取りはたった1行のコードで実現できます。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-1.cs
using IronBarCode;

BarcodeResults results = BarcodeReader.Read("QuickStart.jpg");
if (results != null)
{
    foreach (BarcodeResult result in results)
    {
        Console.WriteLine(result.Text);
    }
}
Imports IronBarCode

Private results As BarcodeResults = BarcodeReader.Read("QuickStart.jpg")
If results IsNot Nothing Then
	For Each result As BarcodeResult In results
		Console.WriteLine(result.Text)
	Next result
End If
$vbLabelText   $csharpLabel

この1行のコードで、入力ドキュメント内のあらゆる種類のBARCODEを卓越したパフォーマンスで検出・スキャンできます。必要な処理がすべて1ステップで完了します!このメソッドは、JPEG、PNG、BMPなどの幅広い画像形式に加え、PDFやGIF、TIFFなどのマルチフレーム形式にも対応しています。 パフォーマンスを向上させるため、カスタマイズ可能な設定オプションが用意されています。

読み込み速度を向上させるには、パフォーマンス向上のために設定された Speed を使用して BarcodeReaderOptions オブジェクトを作成できます。 デフォルトは Balanced ですが、特定のチェックをスキップするには Faster オプションを使用できます。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-2.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    ExpectMultipleBarcodes = false,
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
    CropArea = new System.Drawing.Rectangle(100, 200, 300, 400),
};

BarcodeResults result = BarcodeReader.Read("QuickStart.jpg", myOptionsExample);
if (result != null)
{
    Console.WriteLine(result.First().Text);
}
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ExpectMultipleBarcodes = False,
	.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128,
	.CropArea = New System.Drawing.Rectangle(100, 200, 300, 400)
}

Private result As BarcodeResults = BarcodeReader.Read("QuickStart.jpg", myOptionsExample)
If result IsNot Nothing Then
	Console.WriteLine(result.First().Text)
End If
$vbLabelText   $csharpLabel

また、読みやすさを最適化するために、ScanModeOnlyBasicScan に設定することも可能です。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-3.cs
using IronBarCode;

BarcodeResults results = BarcodeReader.Read("MultipleBarcodes.png");

// Loop through the results
foreach (BarcodeResult result in results)
{
    string value = result.Value;
    Bitmap img = result.BarcodeImage;
    BarcodeEncoding barcodeType = result.BarcodeType;
    byte[] binary = result.BinaryValue;
    Console.WriteLine(result.Value);
}
Imports IronBarCode

Private results As BarcodeResults = BarcodeReader.Read("MultipleBarcodes.png")

' Loop through the results
For Each result As BarcodeResult In results
	Dim value As String = result.Value
	Dim img As Bitmap = result.BarcodeImage
	Dim barcodeType As BarcodeEncoding = result.BarcodeType
	Dim binary() As Byte = result.BinaryValue
	Console.WriteLine(result.Value)
Next result
$vbLabelText   $csharpLabel

その他の設定には、スキャン対象のBARCODE形式の指定が含まれます。これにより、不要なスキャンを減らし、処理を高速化することができます。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-4.cs
using IronBarCode;

BarcodeResults pagedResults = BarcodeReader.Read("MultipleBarcodes.pdf");

// Loop through the results
foreach (BarcodeResult result in pagedResults)
{
    int pageNumber = result.PageNumber;
    string value = result.Value;
    Bitmap img = result.BarcodeImage;
    BarcodeEncoding barcodeType = result.BarcodeType;
    byte[] binary = result.BinaryValue;
    Console.WriteLine(result.Value);
}

// or from a multi-page  TIFF scan with image correction:
BarcodeResults multiFrameResults = BarcodeReader.Read(inputImage: "Multiframe.tiff", new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Detailed,
    ExpectMultipleBarcodes = true,
    ExpectBarcodeTypes = BarcodeEncoding.Code128,
    Multithreaded = false,
    RemoveFalsePositive = false,
    ImageFilters = null
});
Imports IronBarCode

Private pagedResults As BarcodeResults = BarcodeReader.Read("MultipleBarcodes.pdf")

' Loop through the results
For Each result As BarcodeResult In pagedResults
	Dim pageNumber As Integer = result.PageNumber
	Dim value As String = result.Value
	Dim img As Bitmap = result.BarcodeImage
	Dim barcodeType As BarcodeEncoding = result.BarcodeType
	Dim binary() As Byte = result.BinaryValue
	Console.WriteLine(result.Value)
Next result

' or from a multi-page  TIFF scan with image correction:
Dim multiFrameResults As BarcodeResults = BarcodeReader.Read(inputImage:= "Multiframe.tiff", New BarcodeReaderOptions With {
	.Speed = ReadingSpeed.Detailed,
	.ExpectMultipleBarcodes = True,
	.ExpectBarcodeTypes = BarcodeEncoding.Code128,
	.Multithreaded = False,
	.RemoveFalsePositive = False,
	.ImageFilters = Nothing
})
$vbLabelText   $csharpLabel

BarCodeの記述

IronBarcode を使用して BARCODE を作成するには、BarcodeWriter クラスを使用します。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-5.cs
using IronBarCode;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.SaveAsImage("myBarcode.png");
Imports IronBarCode

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.SaveAsImage("myBarcode.png")
$vbLabelText   $csharpLabel

BarCodeのスタイル設定

IronBarcodeは、BARCODEの視覚的表現を操作するためのいくつかのオプションを提供しています。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-7.cs
using IronBarCode;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.AddAnnotationTextAboveBarcode("Product URL:");
myBarcode.AddBarcodeValueTextBelowBarcode();
myBarcode.SetMargins(100);
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Purple);

// All major image formats supported as well as PDF and HTML
myBarcode.SaveAsPng("myBarcode.png");
Imports IronBarCode

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.AddAnnotationTextAboveBarcode("Product URL:")
myBarcode.AddBarcodeValueTextBelowBarcode()
myBarcode.SetMargins(100)
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Purple)

' All major image formats supported as well as PDF and HTML
myBarcode.SaveAsPng("myBarcode.png")
$vbLabelText   $csharpLabel

BarCodeをHTMLとしてエクスポートする

IronBarcodeは、BARCODEをHTMLドキュメントとして、またはHTMLコンテンツの一部としてエクスポートできます。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-8.cs
using IronBarCode;

QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPdf("MyQR.pdf");
Imports IronBarCode

QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPdf("MyQR.pdf")
$vbLabelText   $csharpLabel

QRコードの生成

QRコードについては、誤り訂正などのQRコード固有の機能に対する追加設定を提供する QRCodeWriter クラスを使用してください。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-9.cs
using IronBarCode;
using IronSoftware.Drawing;

QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", qrCodeLogo);
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf");
Imports IronBarCode
Imports IronSoftware.Drawing

Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", qrCodeLogo)
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf")
$vbLabelText   $csharpLabel

対応BARCODE形式

IronBarcodeは、読み取りと書き込みの両方において、一般的に使用される多種多様なBarCode形式に対応しています:

  • QRコードマイクロQRコード、および長方形マイクロQRコード(rMQR)
  • AztecData MatrixMaxiCodePDF417などのその他の2次元BARCODE。
  • Databarなどの積み重ね型線形BarCode。
  • UPC-AUPC-EEAN-8EAN-13CodabarITFMSIPlesseyなどの従来の1次元BARCODE形式。

IronBarcodeを選ぶ理由

IronBarcodeは、.NET向けにバーコードの読み取りおよび書き込みを行うための、親しみやすく使いやすいAPIを提供しており、実運用における正確性、精度、および速度を最適化しています。

例えば、BarcodeWriterクラスは、UPCAおよびUPCE BARCODEの"チェックサム"を自動的に検証・修正し、数値形式の制約を処理します。 IronBarcodeは、開発者が自身のデータに最適なBarCode形式を選択できるよう支援します。

このライブラリは堅牢で、BarCode検出の成功率を最大化するために、自動回転や画像ノイズ除去などの画像前処理技術を備えています。

今後の進め方

IronBarcodeを最大限に活用していただくため、本ドキュメントセクション内のチュートリアルをお読みになるか、GitHubの当プロジェクトページをご覧になることをお勧めします。

カーティス・チャウ
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

準備はできましたか?
Nuget ダウンロード 2,240,258 | バージョン: 2026.5 just released
Still Scrolling Icon

まだスクロールしていますか?

すぐに証拠が欲しいですか? PM > Install-Package BarCode
サンプルを実行する 文字列が BarCode になるのを見る。