如何在 WPF 中构建二维码扫描器
Windows Presentation Foundation (WPF) 是一个用于构建具有XAML定义的用户界面的Windows桌面应用程序的.NET框架。 IronQR 可直接集成到 WPF 中,仅需几行 C# 代码即可实现对用户选定图像文件的二维码扫描。
在本指南中,我们将构建一个 WPF 应用程序,该应用程序将打开文件对话框,加载选定的图像,并使用 IronQR 解码其中的任何嵌入式二维码。 该方案支持 PNG、JPEG、BMP、GIF、TIFF 及其他常见图像格式。
如何在 WPF 中扫描二维码
- 通过 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>
示例输入
请使用下方的二维码作为测试图片。 将其保存到设备中,通过文件对话框选中该文件,然后点击“选择图片并扫描二维码”。 解码后的值应显示为**https://ironsoftware.com**。

示例 QR 码 - 编码 https://ironsoftware.com
使用 IronQR 扫描二维码
当点击按钮时,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 码 指南。
常见问题
在WPF中使用IronQR构建二维码扫描器需要什么条件?
要在WPF中使用IronQR构建二维码扫描器,需要安装具有.NET桌面开发工作负载的Visual Studio 2022,以及一个目标.NET 8或更高版本的WPF项目。此外,通过NuGet包管理器安装IronQR。
如何在WPF中开始使用IronQR扫描二维码?
首先,通过NuGet安装IronQR库,并在XAML中向您的WPF窗口添加一个Button和一个TextBlock。使用OpenFileDialog打开文件对话框以选择图像,并使用AnyBitmap.FromFile加载该图像,然后通过QrReader调用Read来解码和显示二维码。
IronQR支持WPF中的二维码扫描的哪些图像格式?
IronQR支持各种常见的图像格式,如PNG、JPEG、BMP、GIF和TIFF,用于WPF应用程序中的二维码扫描。
如何将IronQR集成到WPF应用程序中?
通过NuGet安装IronQR库来将其集成到WPF应用程序中,并设置您的应用程序UI,包含例如用于触发图像选择的Button和用于显示结果的TextBlock等组件。
如果在图像中找不到二维码会发生什么?
如果在图像中找不到二维码,IronQR的Read方法将返回null,您可以显示例如‘在选定的图像中未找到二维码’的信息,而不是解码结果。
IronQR能处理单个图像中的多个二维码吗?
可以,IronQR可以处理单个图像中的多个二维码。遍历QrReader.Read返回的集合以访问所有检测到的二维码,而不是使用FirstOrDefault。
使用IronQR的WPF UI中二维码扫描器的关键组件是什么?
使用IronQR的WPF UI中二维码扫描器的关键组件包括用于触发文件对话框的Button、用于选择图像的OpenFileDialog,以及用于显示解码后二维码结果的TextBlock。
如何在WPF应用程序中选择用于二维码扫描的文件?
在WPF应用程序中,使用OpenFileDialog选择用于二维码扫描的文件,允许用户从磁盘中选择图像文件,并过滤为常见的图像格式。
在WPF中使用IronQR可以进行实时二维码扫描吗?
可以,在WPF中使用IronQR进行实时二维码扫描,可以通过WPF媒体元素捕获来自网络摄像头的视频帧,并用IronQR进行处理。
如何在WPF应用程序中显示解码后的二维码值?
在WPF应用程序中显示解码后的二维码值,通过将QrReader.Read的结果分配给TextBlock组件,展示扫描的文本或URL。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。