Barcodes & QRs in C# & VB.NET Applications
C#および他のすべて for .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
この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
読み取りプロセスを最適化するために、ScanMode を OnlyBasicScan に設定することもできます。
: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
他の設定では、スキャンするバーコード形式を指定でき、不要なスキャンを減らすことで処理を高速化することができます。
: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
})
バーコードの書き込み
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")
バーコードのスタイリング
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")
バーコードの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")
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")
サポートされるバーコード形式
IronBarcodeは、読み書きの両方で多くの一般的に使用されるバーコード形式をサポートしています:
- QR、Micro QR、長方形のMicro QR (rMQR)コード。
- Aztec、Data Matrix、MaxiCode、PDF417などの他の二次元バーコード。
- Databarなどのスタック型一次元バーコード。
- UPC-A、UPC-E、EAN-8、EAN-13、Codabar、ITF、MSI、Plesseyなどの通常の一次元バーコード形式。
IronBarcodeを選ぶ理由
IronBarcodeは、.NETのためのバーコードの読み書きのための開発者にとって使いやすく、実際のユースケースでの精度、精密さ、および速度を最適化するAPIを提供します。
たとえば、BarcodeWriter クラスは、UPCA および UPCE バーコードの"チェックサム"を自動的に検証および修正し、数値形式の制約を処理します。 IronBarcodeは、開発者がデータに最適なバーコード形式を選択するのを手助けします。
このライブラリは堅牢で、画像の事前処理技術(自動回転や画像ノイズ除去など)を使用して、バーコード検出成功率を最大化します。
今後のステップ
IronBarcodeを最大限に活用するために、このドキュメンテーションセクション内のチュートリアルを読むことをお勧めします。また、GitHubで私たちを訪れてください。

