如何在 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。
在 MainWindow.xaml.cs 中添加以下 OnScanButtonClicked 方法:
: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>,其中包含每个检测到的 QR 码对应的一条记录。 FirstOrDefault 在未找到QR码时会安全地返回 null,从而避免在图像中没有有效QR码时抛出异常。
输出
选择QR图片并点击扫描按钮后,解码后的值将显示在按钮下方的 TextBlock 中。
在 WPF 窗口中渲染的解码 QR 码值
下载项目
结论
IronQR 只需极少的配置即可集成到 WPF 应用程序中——仅需一次 QrReader.Read 调用即可处理桌面端的整个解码流程。若要从单张图像中扫描多个 QR 码,请遍历完整的结果集合,而非调用 FirstOrDefault。
这种模式同样适用于批量处理(通过循环遍历图像文件目录)或实时扫描(使用 WPF 媒体控件从网络摄像头流中捕获帧)。
有关读取二维码及可用扫描模式的更多信息,请参阅《从图像读取二维码》和《使用扫描模式读取二维码》指南。
常见问题解答
WPF QR码扫描教程的内容是什么?
WPF QR码扫描教程提供了使用IronQR构建QR码扫描器的逐步指南。它解释了如何在Windows桌面应用程序中使用OpenFileDialog进行图像选择以及使用QrReader.Read进行QR码解码。
IronQR如何促进WPF应用程序中的QR码扫描?
IronQR简化了WPF应用程序中的QR码扫描,提供了如QrReader.Read之类的工具,可以有效地从通过OpenFileDialog选择的图像中解码QR码。
在WPF中构建QR码扫描器所需的主要组件是什么?
使用IronQR构建WPF QR码扫描器的主要组件包括用于选择图像的OpenFileDialog和用于QR码解码的QrReader.Read方法。
IronQR可以从不同的图像格式中解码QR码吗?
是的,IronQR能够从各种图像格式中解码QR码,使其在WPF应用程序中具有多用性,因为图像可能来自不同的文件类型。
是否可以将IronQR与现有的WPF应用程序集成?
绝对可以,IronQR可以与现有的WPF应用程序集成,让开发者可以增加QR码扫描功能而无需在当前系统中进行大规模改动。
是什么让IronQR适合WPF开发?
IronQR适合WPF开发的原因在于其易于使用、强大的QR码解码能力以及无缝集成到Windows桌面应用程序中的特点。
OpenFileDialog如何改善WPF QR码扫描器中的用户体验?
OpenFileDialog通过提供一个简单的界面来选择图像文件,从而增强了用户体验,随后这些文件可以由IronQR处理以解码QR码。
QrReader.Read在QR码扫描过程中起什么作用?
QrReader.Read在QR码扫描过程中至关重要,因为它利用IronQR的高级解码算法从所选图像中解码QR码。
在WPF应用程序中使用IronQR是否有任何前提条件?
要在WPF应用程序中使用IronQR,开发人员应具备WPF开发的基本知识和对C#的熟悉程度。IronQR的简单API使其在各个技能级别的开发人员中都能容易地使用。
IronQR如何确保QR码解码的准确性?
IronQR通过其尖端的算法确保QR码解码的准确性,这些算法设计用以高效地处理各种QR码的复杂性和图像质量。

