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#および他のすべての.NET言語でバーコードを読み書きすることは、IronBarcodeソフトウェアライブラリを使用することで簡単です。

IronBarcodeのインストール

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

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

Install-Package BarCode

代わりに、dotnet CLIを使用してインストールすることもできます。

dotnet add package IronBarCode

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

IronBarcodeを使用すると、バーコードを読み取るのは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行のコードで、入力ドキュメントからすべての種類のバーコードを特定してスキャンする機能を持ち、抜群の性能を発揮します。すべてが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

他の設定では、スキャンするバーコード形式を指定でき、不要なスキャンを減らすことで処理を高速化することができます。

: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

バーコードの書き込み

IronBarcodeを使用してバーコードを書くには、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

バーコードのスタイリング

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

: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

バーコードのHTMLとしてのエクスポート

IronBarcodeは、バーコードを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コードの場合、QRCodeWriterクラスを使用します。これはエラー訂正のようなQR固有の機能のための追加の設定を提供します。

: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

サポートされるバーコード形式

IronBarcodeは、読み書きの両方で多くの一般的に使用されるバーコード形式をサポートしています:

  • QRMicro QR長方形のMicro QR (rMQR)コード。
  • AztecData MatrixMaxiCodePDF417などの他の二次元バーコード。
  • Databarなどのスタック型一次元バーコード。
  • UPC-AUPC-EEAN-8EAN-13CodabarITFMSIPlesseyなどの通常の一次元バーコード形式。

IronBarcodeを選ぶ理由

IronBarcodeは、.NETのためのバーコードの読み書きのための開発者にとって使いやすく、実際のユースケースでの精度、精密さ、および速度を最適化するAPIを提供します。

たとえば、BarcodeWriterクラスは、UPCAおよびUPCEバーコードの「チェックサム」を自動的に検証および修正し、数値フォーマットの制約を処理します。 IronBarcodeは、開発者がデータに最適なバーコード形式を選択するのを手助けします。

このライブラリは堅牢で、画像の事前処理技術(自動回転や画像ノイズ除去など)を使用して、バーコード検出成功率を最大化します。

今後のステップ

IronBarcodeを最大限に活用するために、このドキュメンテーションセクション内のチュートリアルを読むことをお勧めします。また、GitHubで私たちを訪れてください。

Curtis Chau
テクニカルライター

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

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

準備はいいですか?
Nuget ダウンロード 1,935,276 | バージョン: 2025.11 ただ今リリースされました