WPFでQRコードスキャナーを構築する方法
Windows Presentation Foundation (WPF)は、XAMLで定義されたUIを持つWindowsデスクトップアプリケーションを構築するため for .NETフレームワークです。 IronQRはWPFに直接統合されており、ユーザーが選択した画像ファイルからQRコードを簡単な数行のC#コードでスキャンすることを可能にします。
このガイドでは、ファイルダイアログを開き、選択した画像を読み込み、IronQRを使用して埋め込まれたQRコードをデコードするWPFアプリケーションを構築します。 このアプローチは、PNG、JPEG、BMP、GIF、TIFF、およびその他の一般的な画像形式をサポートしています。
WPFでQRコードをスキャンする方法
- NuGetを使用してIronQR C#ライブラリをインストールします
- XAMLでWPFウィンドウに
ButtonとTextBlockを追加します OpenFileDialogでファイルダイアログを開き、ディスクから画像を選択しますAnyBitmap.FromFileで画像を読み込み、QrImageInputにラップしますReadを呼び出し、デコードされた値をTextBlockに表示します
前提条件
- .NET デスクトップ開発ワークロードがインストールされたVisual Studio 2022
- .NET 8またはそれ以降をターゲットとするWPFプロジェクト
IronQRのインストール
Visual StudioのNuGetパッケージマネージャーコンソールを使用してIronQRライブラリをインストールします。 ツール > NuGet パッケージ マネージャ > パッケージ マネージャ コンソールに移動し、次を実行します:
Install-Package IronQR
もしくは、NuGetでIronQRを検索し、最新バージョンをインストールします。
WPFウィンドウレイアウト
スキャナーUIは、ファイルダイアログをトリガーするTextBlockを使用します。 次のマークアップをMainWindow.xamlに追加します:
<StackPanel Margin="28" VerticalAlignment="Center">
<TextBlock Text="WPF QR Code Scanner" FontSize="20" FontWeight="Bold" Margin="0,0,0,16"/>
<Button x:Name="ScanButton"
Content="Select Image and Scan QR Code"
Click="OnScanButtonClicked"
Padding="12,8"
Margin="0,0,0,16"
HorizontalAlignment="Left"/>
<TextBlock x:Name="ResultLabel"
Text="Select an image to scan."
TextWrapping="Wrap"
FontSize="14"/>
</StackPanel>
<StackPanel Margin="28" VerticalAlignment="Center">
<TextBlock Text="WPF QR Code Scanner" FontSize="20" FontWeight="Bold" Margin="0,0,0,16"/>
<Button x:Name="ScanButton"
Content="Select Image and Scan QR Code"
Click="OnScanButtonClicked"
Padding="12,8"
Margin="0,0,0,16"
HorizontalAlignment="Left"/>
<TextBlock x:Name="ResultLabel"
Text="Select an image to scan."
TextWrapping="Wrap"
FontSize="14"/>
</StackPanel>
サンプル入力
以下のQRコードをテスト画像として使用します。 デバイスに保存し、ファイルダイアログで選択して画像を選んでQRコードをスキャンをクリックします。 デコードされた値はhttps://ironsoftware.comとして表示されます。
サンプルQRコード — `https://ironsoftware.com`をエンコード
IronQRによるQRコードスキャン
ボタンがクリックされると、OnScanButtonClickedが画像を選択するためのファイルダイアログを開きます。 選択されたファイルはResultLabelに書き込まれます。
次のMainWindow.xaml.csに追加します:
:path=/static-assets/qr/content-code-examples/get-started/wpf-qr-code-scanner.cs
using IronQr;
using IronSoftware.Drawing;
using Microsoft.Win32;
using System.Windows;
private void OnScanButtonClicked(object sender, RoutedEventArgs e)
{
// Open a file dialog to select a QR code image
var dialog = new OpenFileDialog
{
Title = "Select a QR code image",
Filter = "Image Files|*.png;*.jpg;*.jpeg;*.bmp;*.gif;*.tiff"
};
if (dialog.ShowDialog() != true) return;
var imageSource = dialog.FileName;
// Load the image into IronQR
var inputBmp = AnyBitmap.FromFile(imageSource);
// Load the asset into QrImageInput
QrImageInput imageInput = new QrImageInput(inputBmp);
// Create a QR Reader object
QrReader reader = new QrReader();
// Read the input and get all embedded QR Codes
IEnumerable<QrResult> results = reader.Read(imageInput);
// Display the first result
var firstResult = results.FirstOrDefault();
ResultLabel.Text = firstResult != null
? "Scanned Text: " + firstResult.Value
: "No QR code found in the selected image.";
}
Imports IronQr
Imports IronSoftware.Drawing
Imports Microsoft.Win32
Imports System.Windows
Private Sub OnScanButtonClicked(sender As Object, e As RoutedEventArgs)
' Open a file dialog to select a QR code image
Dim dialog As New OpenFileDialog With {
.Title = "Select a QR code image",
.Filter = "Image Files|*.png;*.jpg;*.jpeg;*.bmp;*.gif;*.tiff"
}
If dialog.ShowDialog() <> True Then Return
Dim imageSource As String = dialog.FileName
' Load the image into IronQR
Dim inputBmp As AnyBitmap = AnyBitmap.FromFile(imageSource)
' Load the asset into QrImageInput
Dim imageInput As New QrImageInput(inputBmp)
' Create a QR Reader object
Dim reader As New QrReader()
' Read the input and get all embedded QR Codes
Dim results As IEnumerable(Of QrResult) = reader.Read(imageInput)
' Display the first result
Dim firstResult As QrResult = results.FirstOrDefault()
ResultLabel.Text = If(firstResult IsNot Nothing, "Scanned Text: " & firstResult.Value, "No QR code found in the selected image.")
End Sub
OpenFileDialogは、一般的な画像形式にフィルタリングされたネイティブのWindowsファイル選択を提供します。 IEnumerable<QrResult>を返します。 QRコードが見つからない場合、nullを安全に返し、有効なコードのない画像で例外を回避します。
出力
QRコード画像を選択し、スキャンボタンをクリックした後、デコードされた値がボタンの下のTextBlockに表示されます。
デコードされたQRコードの値はWPFウィンドウに表示されます。
プロジェクトをダウンロードする
結論
IronQRは、最小限のセットアップでWPFアプリケーションに統合され、デスクトップ上でデコードパイプライン全体を処理します。単一画像から複数のQRコードをスキャンするには、FirstOrDefaultを呼び出す代わりに完全な結果コレクションを繰り返します。
この同じパターンは、イメージファイルのディレクトリをループするバッチ処理や、WPFメディア要素を使用してWebカメラフィードからフレームをキャプチャするリアルタイムスキャンに拡張されます。
QRコードの読み取りと利用可能なスキャンモードの詳細については、画像からQRコードを読み取るガイドとスキャンモードを使用してQRコードを読み取るガイドを参照してください。

