如何在 WPF 中建立 QR 碼掃描器
Windows Presentation Foundation (WPF) 是一個 .NET Framework,用於構建帶有 XAML 定義 UI 的 Windows 桌面應用程式。 IronQR 可直接整合至 WPF,僅需幾行 C# 程式碼,即可從使用者選定的影像檔案中掃描 QR 碼。
在本指南中,我們將建立一個 WPF 應用程式,該應用程式會開啟檔案對話方塊、載入選定的圖片,並使用 IronQR 解碼任何嵌入的 QR 碼。 此解決方案支援 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 套件管理員」>「套件管理員控制台」,並執行:
或者,您可以在 NuGet 上搜尋 IronQR 並安裝最新版本。
WPF 視窗佈局
掃描器 UI 使用 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>
範例輸入
請使用下方的 QR 碼作為測試圖片。 將其儲存至您的裝置,透過檔案對話方塊選取該檔案,然後點擊「選取圖片並掃描 QR 碼」。 解碼值應顯示為 https://ironsoftware.com**。

樣本 QR 碼 - 編碼 https://ironsoftware.com
使用 IronQR 掃描 QR 碼
當按下按鈕時,OnScanButtonClicked 會開啟文件對話框以選擇圖片。 所選文件將被加載到 AnyBitmap,傳遞給 QrReader.Read,第一個解碼值被寫入 ResultLabel。
將以下 OnScanButtonClicked 方法新增到 MainWindow.xaml.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 SubImports 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 SubOpenFileDialog 提供本機 Windows 文件選擇,篩選為常見的圖像格式。 AnyBitmap.FromFile 處理 PNG、JPEG、BMP、GIF 和 TIFF 輸入的格式解碼,而 QrReader.Read 返回包含每個檢測到的 QR 碼一個條目的 IEnumerable<QrResult>。 FirstOrDefault 當未找到 QR 碼時安全返回 null,避免在沒有有效代碼的圖片上拋出異常。
輸出
選擇 QR 碼圖片並點擊掃描按鈕後,解碼值將出現在按鈕下方的 TextBlock 中。

在 WPF 視窗中渲染的解碼 QR 碼值
下載專案
結論
IronQR 融入到 WPF 應用程式中,簡易設置即可運行——單次 QrReader.Read 呼叫便可處理整個桌面解碼流程。若要從單個圖像中掃描多個 QR 碼,請迭代整個結果集合而非多次呼叫 FirstOrDefault。
此模式同樣適用於批次處理(透過迴圈遍歷圖像檔案目錄),或即時掃描(透過 WPF 媒體元件擷取網路攝影機串流的畫面)。
要獲得更多有關閱讀 QR 碼及可用掃描模式的資訊,請參閱 從影像中讀取 QR 碼 和 使用掃描模式讀取 QR 碼 的指南。
常見問題
在使用IronQR的WPF中建立QR Code掃描器需要哪些條件?
要在WPF中使用IronQR建立QR Code掃描器,您需要安裝具有.NET桌面應用程式開發工作負載的Visual Studio 2022,以及一個目標為.NET 8或更高版本的WPF專案。此外,請透過NuGet套件管理器安裝IronQR。
如何在WPF中使用IronQR開始掃描QR Code?
首先,透過NuGet安裝IronQR程式庫,並在您的WPF視窗中使用XAML添加按鈕和TextBlock。使用OpenFileDialog開啟檔案對話框以選擇影像,並使用AnyBitmap.FromFile載入它,然後透過QrReader呼叫Read來解碼並顯示QR Code。
IronQR支援WPF中的哪些影像格式用於QR Code掃描?
IronQR支援各種常見的影像格式,如PNG、JPEG、BMP、GIF和TIFF,用於WPF應用程式中的QR Code掃描。
如何將IronQR整合到WPF應用程式中?
透過使用NuGet安裝程式庫將IronQR整合到WPF應用程式中,並設置您的應用程式UI,如按鈕以觸發影像選擇和TextBlock以顯示結果。
如果影像中沒有找到QR Code會發生什麼?
如果影像中沒有找到QR Code,IronQR的Read方法會返回null,您可以顯示像是「在選擇的影像中未找到QR Code」之類的訊息,而不是解碼結果。
IronQR能否處理單一影像中的多個QR Code?
可以,IronQR能處理單一影像中的多個QR Code。遍歷QrReader.Read返回的集合以存取所有檢測到的QR Code,而不是使用FirstOrDefault。
使用IronQR的WPF UI中QR掃描器的關鍵組件是什麼?
使用IronQR的WPF UI中QR掃描器的關鍵組件包括觸發檔案對話框的按鈕、用於選擇影像的OpenFileDialog和顯示解碼後QR Code結果的TextBlock。
如何在WPF應用程式中選擇要掃描QR Code的檔案?
使用OpenFileDialog在WPF應用程式中選擇要掃描QR Code的檔案,這允許用戶從磁碟中選擇影像檔案,並過濾以常見的影像格式。
在WPF中使用IronQR是否能夠進行即時QR Code掃描?
是的,透過從WPF媒體元素的網路攝影機影像串流中捕捉畫面並使用IronQR處理,可以在WPF中使用IronQR進行即時QR Code掃描。
如何在WPF應用程式中顯示解碼後的QR Code值?
透過將QrReader.Read的結果指派給TextBlock元件,在WPF應用程式中顯示解碼後的QR Code值,展示掃描後的文本或URL。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。