WPFでQRコードスキャナーを構築する方法

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

Windows Presentation Foundation (WPF)は、XAMLで定義されたUIを持つWindowsデスクトップアプリケーションを構築するため for .NETフレームワークです。 IronQRはWPFに直接統合されており、ユーザーが選択した画像ファイルからQRコードを簡単な数行のC#コードでスキャンすることを可能にします。

このガイドでは、ファイルダイアログを開き、選択した画像を読み込み、IronQRを使用して埋め込まれたQRコードをデコードするWPFアプリケーションを構築します。 このアプローチは、PNG、JPEG、BMP、GIF、TIFF、およびその他の一般的な画像形式をサポートしています。

前提条件

  1. .NET デスクトップ開発ワークロードがインストールされたVisual Studio 2022
  2. .NET 8またはそれ以降をターゲットとするWPFプロジェクト

IronQRのインストール

Visual StudioのNuGetパッケージマネージャーコンソールを使用してIronQRライブラリをインストールします。 ツール > NuGet パッケージ マネージャ > パッケージ マネージャ コンソールに移動し、次を実行します:

Install-Package IronQR

もしくは、NuGetIronQRを検索し、最新バージョンをインストールします。

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>
XML

サンプル入力

以下のQRコードをテスト画像として使用します。 デバイスに保存し、ファイルダイアログで選択して画像を選んでQRコードをスキャンをクリックします。 デコードされた値はhttps://ironsoftware.comとして表示されます。

Sample QR code encoding https://ironsoftware.com for testing the WPF QR scanner

サンプル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
$vbLabelText   $csharpLabel

OpenFileDialogは、一般的な画像形式にフィルタリングされたネイティブのWindowsファイル選択を提供します。 IEnumerable<QrResult>を返します。 QRコードが見つからない場合、nullを安全に返し、有効なコードのない画像で例外を回避します。

出力

QRコード画像を選択し、スキャンボタンをクリックした後、デコードされた値がボタンの下のTextBlockに表示されます。

WPF QR Code Scanner using IronQR — decoded result displayed in the window

デコードされたQRコードの値はWPFウィンドウに表示されます。

プロジェクトをダウンロードする

完全なWpfQrScannerプロジェクトをダウンロードするにはここをクリックしてください。

結論

IronQRは、最小限のセットアップでWPFアプリケーションに統合され、デスクトップ上でデコードパイプライン全体を処理します。単一画像から複数のQRコードをスキャンするには、FirstOrDefaultを呼び出す代わりに完全な結果コレクションを繰り返します。

この同じパターンは、イメージファイルのディレクトリをループするバッチ処理や、WPFメディア要素を使用してWebカメラフィードからフレームをキャプチャするリアルタイムスキャンに拡張されます。

QRコードの読み取りと利用可能なスキャンモードの詳細については、画像からQRコードを読み取るガイドとスキャンモードを使用してQRコードを読み取るガイドを参照してください。

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

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

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

準備はできましたか?
Nuget ダウンロード 63,625 | バージョン: 2026.4 リリース
Still Scrolling Icon

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

すぐに証拠が欲しいですか? PM > Install-Package IronQR
サンプルを実行する URL が QR コードになるのを見る。