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

Generate QR Codes in C# - Complete Tutorial for .NET Developers

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

C#アプリケーションでQRコードを生成する必要がありますか? このチュートリアルでは、IronBarcodeを使ってQRコードを作成、カスタマイズ、検証する方法を詳しく説明します。シンプルな一行の実装から、ロゴ埋め込みやバイナリデータのエンコーディングといった高度な機能まであります。

在庫管理システム、イベントのチケットプラットフォーム、または非接触決済ソリューションを構築しているかどうかにかかわらず、.NETアプリケーションでプロフェッショナルなQRコード機能を実装する方法を学びます。

クイックスタート: IronBarcodeでのワンラインQRコード作成

QRコードを素早く生成したいですか?ここでは、IronBarcodeのQRCodeWriter APIを使用して、たった一行のコードでQRコードを生成する方法を紹介します。カスタマイズはオプションですが、強力です。

  1. IronBarcode をNuGetパッケージマネージャでインストール

    PM > Install-Package BarCode
  2. このコード スニペットをコピーして実行します。

    var qr = QRCodeWriter.CreateQrCode("https://ironsoftware.com/", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium); qr.SaveAsPng("MyQR.png");
  3. 実際の環境でテストするためにデプロイする

    今日プロジェクトで IronBarcode を使い始めましょう無料トライアル

    arrow pointer

How Do I Install a QR Code Library in C#?

このシンプルなコマンドを使って、NuGetパッケージマネージャーでIronBarcodeをインストールしてください。

Install-Package BarCode

NuGetでインストール

または、IronBarcode DLLを直接ダウンロードしてプロジェクトの参照として追加してください。

必要な名前空間をインポートする

IronBarcodeのQRコード生成機能にアクセスするためにこれらの名前空間を追加してください:

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-3.cs
using IronBarCode;

// You may add styling with color, logo images or branding:
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);

myQRCodeWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen);

// Save as PDF
myQRCodeWithLogo.SaveAsPdf("MyQRWithLogo.pdf");

// Also Save as HTML
myQRCodeWithLogo.SaveAsHtmlFile("MyQRWithLogo.html");
Imports IronBarCode

' You may add styling with color, logo images or branding:
Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo)

myQRCodeWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen)

' Save as PDF
myQRCodeWithLogo.SaveAsPdf("MyQRWithLogo.pdf")

' Also Save as HTML
myQRCodeWithLogo.SaveAsHtmlFile("MyQRWithLogo.html")
$vbLabelText   $csharpLabel

How Can I Create a Simple QR Code in C#?

CreateQrCodeメソッドを使用して、たった一行のコードでQRコードを生成します。

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-4.cs
using IronBarCode;
using IronSoftware.Drawing;
using System;

// Verifying QR Codes
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode MyVerifiedQR = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);

MyVerifiedQR.ChangeBarCodeColor(System.Drawing.Color.LightBlue);

if (!MyVerifiedQR.Verify())
{
    Console.WriteLine("\t LightBlue is not dark enough to be read accurately.  Lets try DarkBlue");
    MyVerifiedQR.ChangeBarCodeColor(Color.DarkBlue);
}
MyVerifiedQR.SaveAsHtmlFile("MyVerifiedQR.html");

// open the barcode html file in your default web browser
System.Diagnostics.Process.Start("MyVerifiedQR.html");
Imports Microsoft.VisualBasic
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System

' Verifying QR Codes
Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private MyVerifiedQR As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo)

MyVerifiedQR.ChangeBarCodeColor(System.Drawing.Color.LightBlue)

If Not MyVerifiedQR.Verify() Then
	Console.WriteLine(vbTab & " LightBlue is not dark enough to be read accurately.  Lets try DarkBlue")
	MyVerifiedQR.ChangeBarCodeColor(Color.DarkBlue)
End If
MyVerifiedQR.SaveAsHtmlFile("MyVerifiedQR.html")

' open the barcode html file in your default web browser
System.Diagnostics.Process.Start("MyVerifiedQR.html")
$vbLabelText   $csharpLabel

CreateQrCodeメソッドは、3つのパラメータを受け入れます。

  • テキスト内容:エンコードするデータ(URL、テキスト、その他の文字列データをサポート)
  • サイズ:正方形のQRコードのピクセル寸法(この例では500x500)
  • エラー訂正:不都合な条件での可読性を決定(低、中、クォータイル、高)

エラー訂正レベルが高いほど、QRコードが部分的に損傷または隠れている場合でも読み取り可能であることを保証しますが、データモジュールが多くなりより密度の高いパターンが生じます。

C#でIronBarcodeを使用して生成された標準のQRコード "hello world"というテキストを含む基本QRコード、500x500ピクセルで中エラー訂正で生成されています

QRコードにロゴを追加するにはどうしたらよいですか?

QRコードにロゴを埋め込むことで、ブランド認知を高めつつスキャン可能性を保ちます。 IronBarcodeは、QRコードの整合性を維持するためにロゴを自動的に配置し、サイズを調整します。

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-5.cs
using IronBarCode;
using System;
using System.Linq;

// Create Some Binary Data - This example equally well for Byte[] and System.IO.Stream
byte[] BinaryData = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/");

// WRITE QR with Binary Content
QRCodeWriter.CreateQrCode(BinaryData, 500).SaveAsImage("MyBinaryQR.png");

// READ QR with Binary Content
var MyReturnedData = BarcodeReader.Read("MyBinaryQR.png").First();
if (BinaryData.SequenceEqual(MyReturnedData.BinaryValue))
{
    Console.WriteLine("\t Binary Data Read and Written Perfectly");
}
else
{
    throw new Exception("Corrupted Data");
}
Imports Microsoft.VisualBasic
Imports IronBarCode
Imports System
Imports System.Linq

' Create Some Binary Data - This example equally well for Byte[] and System.IO.Stream
Private BinaryData() As Byte = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/")

' WRITE QR with Binary Content
QRCodeWriter.CreateQrCode(BinaryData, 500).SaveAsImage("MyBinaryQR.png")

' READ QR with Binary Content
Dim MyReturnedData = BarcodeReader.Read("MyBinaryQR.png").First()
If BinaryData.SequenceEqual(MyReturnedData.BinaryValue) Then
	Console.WriteLine(vbTab & " Binary Data Read and Written Perfectly")
Else
	Throw New Exception("Corrupted Data")
End If
$vbLabelText   $csharpLabel

CreateQrCodeWithLogoメソッドは、ロゴの配置を賢く処理します。

  • QRコードの読取り可能性を維持するためにロゴを自動的にサイズ変更
  • データ破損を避けるため静止域内に位置決め
  • QRコードの色を変更してもロゴの元の色を保持

このアプローチにより、ブランド化されたQRコードがすべてのスキャニングデバイスやアプリケーションで完全に動作することを保証します。

Visual Studioのロゴが埋め込まれたQRコード Visual StudioロゴをフィーチャーしたQRコード、IronBarcodeの自動ロゴサイズ調整と位置決めを示しています

QRコードを異なる形式にエクスポートするにはどうすればいいですか?

IronBarcodeは、さまざまな用途に対応する複数のエクスポート形式をサポートしています。 QRコードを画像、PDF、またはHTMLファイルとしてエクスポートします:

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-6.cs
using IronBarCode;
using System;
using System.Linq;

BarcodeResults result = BarcodeReader.Read("QR.png", new BarcodeReaderOptions() { ExpectBarcodeTypes = BarcodeEncoding.QRCode });
if (result != null)
{
    Console.WriteLine(result.First().Value);
}
Imports IronBarCode
Imports System
Imports System.Linq

Private result As BarcodeResults = BarcodeReader.Read("QR.png", New BarcodeReaderOptions() With {.ExpectBarcodeTypes = BarcodeEncoding.QRCode})
If result IsNot Nothing Then
	Console.WriteLine(result.First().Value)
End If
$vbLabelText   $csharpLabel

各形式は特定の目的に適しています:

カスタマイズ後のQRコードの読みやすさを検証する方法

色の変更やロゴの追加はQRコードのスキャン可能性に影響を与えることがあります。 カスタマイズされたQRコードが読み取り可能な状態を維持するためにVerify()メソッドを使用してください。

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-7.cs
using IronBarCode;
using System;
using System.Linq;

BarcodeReaderOptions options = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Faster,
    ExpectMultipleBarcodes = false,
    ExpectBarcodeTypes = BarcodeEncoding.All,
    Multithreaded = false,
    MaxParallelThreads = 0,
    CropArea = null,
    UseCode39ExtendedMode = false,
    RemoveFalsePositive = false,
    ImageFilters = null
};

BarcodeResults result = BarcodeReader.Read("QR.png", options);
if (result != null)
{
    Console.WriteLine(result.First().Value);
}
Imports IronBarCode
Imports System
Imports System.Linq

Private options As New BarcodeReaderOptions With {
	.Speed = ReadingSpeed.Faster,
	.ExpectMultipleBarcodes = False,
	.ExpectBarcodeTypes = BarcodeEncoding.All,
	.Multithreaded = False,
	.MaxParallelThreads = 0,
	.CropArea = Nothing,
	.UseCode39ExtendedMode = False,
	.RemoveFalsePositive = False,
	.ImageFilters = Nothing
}

Private result As BarcodeResults = BarcodeReader.Read("QR.png", options)
If result IsNot Nothing Then
	Console.WriteLine(result.First().Value)
End If
$vbLabelText   $csharpLabel

Verify()メソッドは、あなたのQRコードに対して包括的なスキャンテストを行います。 これにより、異なるスキャニングデバイスや照明条件に対応した互換性が確保されるため、配布前に確認が行えます。

ダークブルーの色合いとVisual Studioロゴで検証されたQRコード ダークブルーでうまく検証されたQRコード、信頼できるスキャンを発揮する正しいコントラストを示す

QRコードにバイナリデータをエンコードするにはどうすればいいですか?

QRコードは、バイナリデータを効率的に格納するのに優れています。 この能力により、暗号化されたデータ転送、ファイル共有、IoTデバイスの設定など、高度なアプリケーションが可能になります:

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-8.cs
using IronBarCode;
using System;
using System.Linq;

// Convert string to binary data
byte[] binaryData = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/");

// Create QR code from binary content
QRCodeWriter.CreateQrCode(binaryData, 500).SaveAsPng("MyBinaryQR.png");

// Read and verify binary data integrity
var myReturnedData = BarcodeReader.Read("MyBinaryQR.png").First();

// Confirm data matches original
if (binaryData.SequenceEqual(myReturnedData.BinaryValue))
{
    Console.WriteLine("Binary Data Read and Written Perfectly");
}
else
{
    throw new Exception("Data integrity check failed");
}
Imports IronBarCode
Imports System
Imports System.Linq

' Convert string to binary data
Dim binaryData As Byte() = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/")

' Create QR code from binary content
QRCodeWriter.CreateQrCode(binaryData, 500).SaveAsPng("MyBinaryQR.png")

' Read and verify binary data integrity
Dim myReturnedData = BarcodeReader.Read("MyBinaryQR.png").First()

' Confirm data matches original
If binaryData.SequenceEqual(myReturnedData.BinaryValue) Then
    Console.WriteLine("Binary Data Read and Written Perfectly")
Else
    Throw New Exception("Data integrity check failed")
End If
$vbLabelText   $csharpLabel

QRコードのバイナリエンコーディングは、いくつかの利点を提供します:

  • 効率性:コンパクトなバイナリ形式でデータを格納
  • 多様性:あらゆるデータタイプ(ファイル、暗号化コンテンツ、シリアライズされたオブジェクト)を扱う
  • 整合性:エンコードの問題なく正確なバイトシーケンスを保持

この機能により、IronBarcodeが基本的なQRコードライブラリとは一線を画し、あなたのアプリケーションにおける高度なデータ交換シナリオを可能にします。

バイナリエンコードされたデータを含むQRコード IronBarcodeの高度なエンコード機能を示すバイナリデータを格納するQRコード

How Do I Read QR Codes in C#?

IronBarcodeは、柔軟なQRコード読み取り機能を提供します。 最もシンプルなアプローチを紹介します:

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-9.cs
using IronBarCode;
using System;
using System.Linq;

// Read QR code with optimized settings
BarcodeResults result = BarcodeReader.Read("QR.png", new BarcodeReaderOptions() { 
    ExpectBarcodeTypes = BarcodeEncoding.QRCode 
});

// Extract and display the decoded value
if (result != null && result.Any())
{
    Console.WriteLine(result.First().Value);
}
else
{
    Console.WriteLine("No QR codes found in the image.");
}
Imports IronBarCode
Imports System
Imports System.Linq

' Read QR code with optimized settings
Dim result As BarcodeResults = BarcodeReader.Read("QR.png", New BarcodeReaderOptions() With {
    .ExpectBarcodeTypes = BarcodeEncoding.QRCode
})

' Extract and display the decoded value
If result IsNot Nothing AndAlso result.Any() Then
    Console.WriteLine(result.First().Value)
Else
    Console.WriteLine("No QR codes found in the image.")
End If
$vbLabelText   $csharpLabel

より複雑なシナリオでの微調整コントロールが必要な場合:

:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-10.cs
using IronBarCode;
using System;
using System.Linq;

// Configure advanced reading options
BarcodeReaderOptions options = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Faster,           // Optimize for speed
    ExpectMultipleBarcodes = false,        // Single QR code expected
    ExpectBarcodeTypes = BarcodeEncoding.QRCode, // QR codes only
    Multithreaded = true,                  // Enable parallel processing
    MaxParallelThreads = 4,                // Utilize multiple CPU cores
    RemoveFalsePositive = true,            // Filter out false detections
    ImageFilters = new ImageFilterCollection() // Apply preprocessing
    {
        new AdaptiveThresholdFilter(),    // Handle varying lighting
        new ContrastFilter(),              // Enhance contrast
        new SharpenFilter()                // Improve edge definition
    }
};

// Read with advanced configuration
BarcodeResults result = BarcodeReader.Read("QR.png", options);
Imports IronBarCode
Imports System
Imports System.Linq

' Configure advanced reading options
Dim options As New BarcodeReaderOptions With {
    .Speed = ReadingSpeed.Faster,           ' Optimize for speed
    .ExpectMultipleBarcodes = False,        ' Single QR code expected
    .ExpectBarcodeTypes = BarcodeEncoding.QRCode, ' QR codes only
    .Multithreaded = True,                  ' Enable parallel processing
    .MaxParallelThreads = 4,                ' Utilize multiple CPU cores
    .RemoveFalsePositive = True,            ' Filter out false detections
    .ImageFilters = New ImageFilterCollection() From { ' Apply preprocessing
        New AdaptiveThresholdFilter(),      ' Handle varying lighting
        New ContrastFilter(),               ' Enhance contrast
        New SharpenFilter()                 ' Improve edge definition
    }
}

' Read with advanced configuration
Dim result As BarcodeResults = BarcodeReader.Read("QR.png", options)
$vbLabelText   $csharpLabel

これらの高度な読み取りオプションを使用すると、悪条件(悪照明、画像歪み、低品質印刷)でのQRコード検出が可能です。

QRコード開発の次は?

これでIronBarcodeを使ったQRコード生成をマスターしたので、これらの高度なトピックを探索してください:

リソースをダウンロード

完全なソースコードと例にアクセスしてください:

APIドキュメント

APIリファレンスで完全な機能セットを探る:

代替案: 高度なQRアプリケーション向けIronQR

最先端のQRコード機能を必要とするプロジェクトには、IronQR-Iron SoftwareのQRコード専用ライブラリをご検討ください。機械学習による読み取り精度は99.99%で、高度な生成オプションを備えています。

.NETアプリケーションにQRコードを実装する準備ができましたか? 無料トライアルを開始またはIronBarcodeをダウンロードしてください。

よくある質問

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

C#でIronBarcodeのQRCodeWriter.CreateQrCode()メソッドを使用してQRコードを生成できます。このメソッドを使うことで、コンテンツ、サイズ、エラー訂正レベルを指定して効率的にQRコードを作成します。

QRコードはどの画像形式にエクスポートできますか?

IronBarcodeを使用すると、QRコードをPNG、JPEG、PDF、HTMLなどの様々な形式にエクスポートできます。この目的のために、SaveAsPng()SaveAsJpeg()SaveAsPdf()SaveAsHtmlFile()などのメソッドが用意されています。

QRコードに会社のロゴをどのように追加できますか?

IronBarcodeはCreateQrCodeWithLogo()メソッドを提供しており、これにQRCodeLogoオブジェクトを渡すことで、ロゴ画像を含めることができます。ライブラリはロゴがQRコードの読み取り性を損なわないように正しくサイズや位置を調整します。

QRコードのエラー訂正とは何ですか?どのレベルを選べばよいですか?

QRコードのエラー訂正は、一部が損傷していてもスキャン可能であることを保証します。IronBarcodeは4つのレベルを提供しています:低(7%)、中(15%)、四分(25%)、高(30%)。ほとんどの用途には中レベルが適しており、厳しい環境には高レベルが理想的です。

カスタマイズされたQRコードの読み取り性をどのように確認しますか?

GeneratedBarcodeオブジェクトでVerify()メソッドを使用することで、色の変更やロゴ追加といった変更後もカスタマイズされたQRコードがスキャンできることを確認できます。

QRコードにバイナリデータをエンコードできますか?

はい、IronBarcodeのCreateQrCode()メソッドはバイト列のエンコードをサポートしており、ファイルや暗号化されたコンテンツをQRコード内に保存できます。

C#で画像からQRコードをどのように読み取りますか?

C#で画像からQRコードを読み取るには、IronBarcodeのBarcodeReader.Read()メソッドを利用します。パフォーマンスを最適化するために、BarcodeReaderOptionsBarcodeEncoding.QRCodeを指定してください。

QRコードの最大データ容量はどれくらいですか?

IronBarcodeで生成されたQRコードは、選択したエラー訂正レベルに応じて最大で2,953バイト、4,296の英数字、または7,089の数字を保持できます。

QRコードの色を変更しても読み取り可能に保つにはどうすればよいですか?

IronBarcodeのChangeBarCodeColor()メソッドを使用してQRコードの色を変更できます。色の変更がQRコードの読み取り性に影響を及ぼさないことを確認するために、常にVerify()メソッドを使用してください。

特殊なQRコードライブラリにはどのような機能がありますか?

Iron Softwareの特殊なライブラリであるIronQRは、99.99%の精度で動作する機械学習を搭載したQRコード読み取り機能や、複雑なアプリケーションに対応した頑強な生成機能などの高度な機能を含んでいます。

Jacob Mellor、Ironチームの最高技術責任者(CTO)
最高技術責任者(CTO)

ジェイコブ・メラーはIron Softwareの最高技術責任者(CTO)であり、C# PDFテクノロジーを開拓する先見的なエンジニアです。Iron Softwareのコアコードベースを支えるオリジナル開発者として、彼は創業以来、会社の製品アーキテクチャを形成し、CEOのCameron Rimingtonとともに、会社をNASA、Tesla、および世界的な政府機関にサービスを提供する50人以上の会社に変えました。1999年にロンドンで最初のソフトウェアビジネスを開業し、2005年に最初 for .NETコンポーネントを作成した後、Microsoftのエコシステム全体で複雑な問題を解決することを専門としました。

彼の主要なIronPDFとIron Suite .NETライブラリは、世界中で3000万以上のNuGetインストールを達成し、彼の基礎となるコードは世界中で使用されている開発者ツールに力を与え続けています。25年の商業経験と41年のコーディングの専門知識を持つJacobは、次世代の技術リーダーを指導しながら、エンタープライズグレードのC#、Java、Python PDFテクノロジーにおけるイノベーションの推進に注力しています。

準備はできましたか?
Nuget ダウンロード 2,317,217 | バージョン: 2026.7 リリースされたばかり
Still Scrolling Icon

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

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