如何在 WPF 中构建二维码扫描器
Windows Presentation Foundation (WPF) 是一个 .NET Framework,用于构建采用 XAML 定义用户界面的 Windows 桌面应用程序。 IronQR 可直接集成到 WPF 中,仅需几行 C# 代码即可实现对用户选定图像文件的二维码扫描。
在本指南中,我们将构建一个 WPF 应用程序,该应用程序将打开文件对话框,加载选定的图像,并使用 IronQR 解码其中的任何嵌入式二维码。 该方案支持 PNG、JPEG、BMP、GIF、TIFF 及其他常见图像格式。
如何在 WPF 中扫描二维码
- 通过 NuGet 安装 IronQR C# 库
- 在 XAML 中向 WPF 窗口添加`按钮`和 `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 窗口布局
扫描器界面使用 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>
示例输入
请使用下方的二维码作为测试图片。 将其保存到设备中,通过文件对话框选中该文件,然后点击"选择图片并扫描二维码"。 解码后的值应显示为 https://ironsoftware.com。
示例二维码 — 编码地址:`https://ironsoftware.com`
使用 IronQR 扫描二维码
点击按钮后,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
OpenFileDialog 提供原生 Windows 文件选择功能,并筛选出常见的图像格式。 AnyBitmap.FromFile 负责对 PNG、JPEG、BMP、GIF 和 TIFF 输入进行格式解码,而 QrReader.Read 则返回一个 IEnumerable<QrResult>,其中包含每个检测到的二维码对应的一条记录。 FirstOrDefault 在未找到二维码时会安全地返回 null,从而避免在图像中没有有效二维码时抛出异常。
输出
选择二维码图片并点击扫描按钮后,解码后的值将显示在按钮下方的 TextBlock 中。
在 WPF 窗口中渲染的解码 QR 码值
下载项目
结论
IronQR 只需极少的配置即可集成到 WPF 应用程序中——仅需一次 QrReader.Read 调用即可处理桌面端的整个解码流程。若要从单张图片中扫描多个二维码,请遍历完整的结果集合,而非调用 FirstOrDefault。
这种模式同样适用于批量处理(通过循环遍历图像文件目录)或实时扫描(使用 WPF 媒体控件从网络摄像头流中捕获帧)。
有关读取二维码及可用扫描模式的更多信息,请参阅《从图像读取二维码》和《使用扫描模式读取二维码》指南。

