如何在 WPF 中建立 QR 碼掃描器

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

Windows Presentation Foundation (WPF) 是一個 .NET Framework,用於建立採用 XAML 定義使用者介面的 Windows 桌面應用程式。 IronQR 可直接整合至 WPF,僅需幾行 C# 程式碼,即可從使用者選定的影像檔案中掃描 QR 碼。

在本指南中,我們將建立一個 WPF 應用程式,該應用程式會開啟檔案對話方塊、載入選定的圖片,並使用 IronQR 解碼任何嵌入的 QR 碼。 此解決方案支援 PNG、JPEG、BMP、GIF、TIFF 及其他常見的圖像格式。

先決條件

  1. 已安裝 .NET 桌面開發工作負載的 Visual Studio 2022
  2. 針對 .NET 8 或更高版本的 WPF 專案

安裝 IronQR

請透過 Visual Studio 中的 NuGet 套件管理員主控台安裝 IronQR程式庫。 請前往"工具">"NuGet 套件管理員">"套件管理員控制台",並執行:

Install-Package IronQR

或者,您也可以在 NuGet 上搜尋 IronQR 並安裝最新版本。

WPF 視窗佈局

掃描器介面使用 Button 觸發檔案對話方塊,並使用 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 會開啟檔案對話方塊以選擇圖片。 選定的檔案會載入至 AnyBitmap,傳遞給 QrReader.Read,並將第一個解碼後的值寫入 ResultLabel

請將以下 OnScanButtonClicked 方法新增至 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 檔案選取功能,並篩選常見的圖像格式。 AnyBitmap.FromFile 負責解碼 PNG、JPEG、BMP、GIF 和 TIFF 格式的輸入資料,而 QrReader.Read 則會傳回一個 IEnumerable<QrResult>,其中包含每個偵測到的 QR 碼對應的一筆記錄。 FirstOrDefault 在未找到 QR 碼時會安全地返回 null,避免在圖片中沒有有效 QR 碼時觸發例外狀況。

輸出

選取 QR 碼圖片並點擊掃描按鈕後,解碼後的值會顯示在按鈕下方的 TextBlock 中。

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

在 WPF 視窗中渲染的解碼 QR 碼值

下載專案

點擊此處下載完整的 WpfQrScanner 專案。

結論

IronQR 只需極少設定即可整合至 WPF 應用程式中 —— 單一 QrReader.Read 呼叫即可處理桌面端的整個解碼流程。若要從單一圖像掃描多個 QR 碼,請遍歷完整的結果集合,而非呼叫 FirstOrDefault

此模式同樣適用於批次處理(透過迴圈遍歷圖像檔案目錄),或即時掃描(透過 WPF 媒體元件擷取網路攝影機串流的畫面)。

有關讀取 QR 碼及可用掃描模式的更多資訊,請參閱《從圖片讀取 QR 碼》和《使用掃描模式讀取 QR 碼》指南。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 63,625 | 版本: 2026.4 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronQR
執行範例 觀看您的 URL 變成 QR code。