
How to Generate QR Code in C# Windows Applications
本教程提供了关于如何创建二维码的深入了解,这在工业应用和零售行业中越来越受欢迎。 将使用 IronBarcode 库,这是最受欢迎和最强大的库之一,来演示如何生成二维码。
如何在C# Windows窗体应用程序中生成二维码
- 在 Microsoft Visual Studio 中创建 Windows Forms 应用程序
- 安装二维码库
- 导入命名空间以创建条形码
- 用一行代码创建二维码
- 向二维码图片添加徽标
- 将图片保存为 PDF 或 HTML
1. 在 Microsoft Visual Studio 中创建 Windows Forms 应用程序
打开 Visual Studio > 点击创建新项目 > 选择 Windows Forms 应用程序模板 > 按下一步 > 命名项目 > 按下一步 > 选择目标 .NET Framework > 点击创建按钮。
在创建项目之后,使用Visual Studio工具箱中的以下控件设计表单:Button。
一个用于加载图像和生成二维码的 Windows Forms 应用程序 UI
2. 在C#中安装QR码生成器.NET库
第一步是安装条形码库。 您可以通过以下三种方法之一来执行此操作:
2.1. 包管理器控制台
在包管理器控制台中写下以下命令。 它将为您下载并安装程序包。
包管理器控制台 UI 中的安装进度
2.2. NuGet 包管理器解决方案
您还可以使用 NuGet 包解决方案安装条形码库。 只需按照这些步骤操作:
点击工具 > NuGet程序包管理器 > 管理解决方案的NuGet程序包。
这将为您打开 NuGet 包管理器。 点击浏览,搜索 BarCode,然后安装类库。
在 NuGet 包管理器中找到 BarCode 库
2.3. 从链接下载
作为替代,IronBarCode.Dll 可以下载并添加到您的项目中,作为 .NET Barcode DLL 的引用。
3. 导入命名空间
对于本教程,为了确保有足够的引用,需要IronBarCode命名空间以及其他系统程序集。
using IronBarCode; // Provides functionality for QR and barcode generation
using System; // Contains fundamental classes and base classes that define commonly-used value and reference data types
using System.Drawing; // Provides access to GDI+ basic graphic functionality
using System.Linq; // Provides classes and interfaces that support queriesImports IronBarCode ' Provides functionality for QR and barcode generation
Imports System ' Contains fundamental classes and base classes that define commonly-used value and reference data types
Imports System.Drawing ' Provides access to GDI+ basic graphic functionality
Imports System.Linq ' Provides classes and interfaces that support queries4. 用 1 行代码创建二维码
以下示例代码允许您用一行代码生成二维码图像。 在文本框中输入您想要生成二维码的文本。 将此代码放在"生成 PNG"按钮单击事件中。 二维码图像可以保存为 PNG 格式。
// Simple QR Code generation
private void button1_Click(object sender, EventArgs e)
{
// Generate a QR code from the text provided in the TextBox
GeneratedBarcode qrCode = QRCodeWriter.CreateQrCode(textBox1.Text);
// Save the generated QR code as a PNG file
qrCode.SaveAsPng("QrCode.png");
}' Simple QR Code generation
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
' Generate a QR code from the text provided in the TextBox
Dim qrCode As GeneratedBarcode = QRCodeWriter.CreateQrCode(textBox1.Text)
' Save the generated QR code as a PNG file
qrCode.SaveAsPng("QrCode.png")
End Sub以下是二维码生成器的输出:
QR code of: https://ironsoftware.com/csharp/barcode/docs/****
5. 向二维码图像添加徽标
通过使用 CreateQrCodeWithLogo 方法从 QRCodeWriter 类中,可以将诸如标识等额外信息添加到二维码中。 示例代码展示了其简单性。
从您的计算机浏览标志,它将在PictureBox中打开。 代码如下
// Open file dialog to select an image
OpenFileDialog open = new OpenFileDialog();
// Set image file filters to ensure valid image types are opened
open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg; *.png; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK) {
// Display image in PictureBox and store file path for later use
pictureBox1.Image = new Bitmap(open.FileName);
// Store image file path in class data member
ImageFileName = open.FileName;
}' Open file dialog to select an image
Dim open As New OpenFileDialog()
' Set image file filters to ensure valid image types are opened
open.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg; *.png; *.jpeg; *.gif; *.bmp"
If open.ShowDialog() = DialogResult.OK Then
' Display image in PictureBox and store file path for later use
pictureBox1.Image = New Bitmap(open.FileName)
' Store image file path in class data member
ImageFileName = open.FileName
End If接下来,只需在文本框中输入文本,将此代码放在生成 PNG 按钮中,然后单击。
// Generate a QR code with a logo
GeneratedBarcode qrCode = QRCodeWriter.CreateQrCodeWithLogo(textBox1.Text, ImageFileName, 500);
// Save the generated QR code with logo as a PNG file
qrCode.SaveAsPng("QrCodeWithImage.png");' Generate a QR code with a logo
Dim qrCode As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo(textBox1.Text, ImageFileName, 500)
' Save the generated QR code with logo as a PNG file
qrCode.SaveAsPng("QrCodeWithImage.png")此代码将在条形码中添加 Iron 徽标。 它会自动将其调整到合适的大小,使纯代码仍然可读,并将该徽标对齐到二维码的方格网络上,使其看起来恰当。
C# 创建带徽标图像的二维码
6. 保存为 PDF 或 HTML 图像
最后,生成的二维码可以保存为 PDF 或 HTML 图像。 最后一行代码在您的默认 PDF 浏览器中打开 PDF 以方便使用。 在生成PDF按钮中添加 SaveAsPdf ,在生成HTML按钮中添加 SaveAsHtmlFile 。
// Generate a QR code with a logo
GeneratedBarcode qrCode = QRCodeWriter.CreateQrCodeWithLogo(textBox1.Text, ImageFileName, 500);
// Save the QR code as a PDF file
qrCode.SaveAsPdf("QRWithLogo.pdf");
// Also, save the QR code as an HTML file
qrCode.SaveAsHtmlFile("QRWithLogo.html");' Generate a QR code with a logo
Dim qrCode As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo(textBox1.Text, ImageFileName, 500)
' Save the QR code as a PDF file
qrCode.SaveAsPdf("QRWithLogo.pdf")
' Also, save the QR code as an HTML file
qrCode.SaveAsHtmlFile("QRWithLogo.html")摘要
IronBarcode 为开发者提供了一个友好的 API 用于读取和写入条形码和二维码的数据,以便在 C# .NET 中优化准确性,并确保在真实案例中具有低错误率。 更多关于 IronBarcode 的信息,请访问此文档网站。
此外,IronBarcode还支持从图像中读取条形码,以及提供额外的选项,可以更准确地读取条形码或对图像应用滤镜。
目前,如果您购买完整的 Iron Suite,您可以以仅付两个库的价格获得五个库。 请访问定价页面以获取更多详细信息。

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。
相关文章


