How to Generate QR Code in C# Windows Applications
本教程提供了关于如何创建二维码的深入了解,这在工业应用和零售行业中越来越受欢迎。 将使用 IronBarcode 库,这是最受欢迎和最强大的库之一,来演示如何生成二维码。
如何在 C# Windows Forms 应用程序中生成二维码
- 在 Microsoft Visual Studio 中创建 Windows Forms 应用程序
- 安装二维码库
- 导入命名空间以创建条形码
- 用一行代码创建二维码
- 向二维码图片添加徽标
- 将图片保存为 PDF 或 HTML
1. 在 Microsoft Visual Studio 中创建 Windows Forms 应用程序
打开 Visual Studio > 点击创建新项目 > 选择 Windows Forms 应用程序模板 > 按下一步 > 命名项目 > 按下一步 > 选择目标 .NET Framework > 点击创建按钮。
创建项目后,使用 Visual Studio 工具箱中的以下控件设计表单:PictureBox、Label、TextBox 和 Button。
一个用于加载图像和生成二维码的 Windows Forms 应用程序 UI
2. 在 C# 中安装 QR 码生成器 .NET 库
第一步是安装条形码库。 您可以通过以下三种方法之一来执行此操作:
2.1. 包管理器控制台
在包管理器控制台中写下以下命令。 它将为您下载并安装程序包。
Install-Package BarCode
包管理器控制台 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 queriesusing 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 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 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");
}以下是二维码生成器的输出:
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
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;
}接下来,只需在文本框中输入文本,将此代码放在生成 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
GeneratedBarcode qrCode = 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
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");摘要
IronBarcode 为开发者提供了一个友好的 API 用于读取和写入条形码和二维码的数据,以便在 C# .NET 中优化准确性,并确保在真实案例中具有低错误率。 更多关于 IronBarcode 的信息,请访问此文档网站。
此外,IronBarcode还支持从图像中读取条形码,以及提供额外的选项,可以更准确地读取条形码或对图像应用滤镜。
目前,如果您购买完整的 Iron Suite,您可以以仅付两个库的价格获得五个库。 请访问定价页面以获取更多详细信息。
常见问题解答
如何在 C# Windows 应用程序中生成 QR 代码?
您可以使用 IronBarcode 库在 C# Windows 应用程序中生成 QR 代码,方法是利用 QRCodeWriter.CreateQrCode 方法。这允许您从文本输入中生成 QR 代码并将其保存为 PNG 文件。
使用 IronBarcode 进行 QR 代码生成的好处是什么?
IronBarcode 提供了一个用户友好的 API,用于 QR 代码生成,具有高精度和低错误率。它还支持添加徽标到 QR 代码以及将 QR 代码保存为 PDF 或 HTML 文件等附加功能。
如何在 Microsoft Visual Studio 中设置用于生成 QR 代码的 Windows 窗体应用程序?
要在 Microsoft Visual Studio 中设置一个 Windows 窗体应用程序,请打开 Visual Studio,选择“创建新项目”,选择“Windows 窗体应用程序模板”,命名您的项目,选择目标 .NET 框架,然后点击“创建”。
在 C# 项目中安装 QR 代码库的过程是什么?
IronBarcode 库可以通过包管理器控制台、NuGet 包管理器解决方案,或通过直接下载 IronBarCode.DLL 来安装到 C# 项目中。
我能否使用 IronBarcode 为 QR 代码添加徽标?
是的,您可以使用 IronBarcode 库的 CreateQrCodeWithLogo 方法从 QRCodeWriter 类中选择计算机中的图像来为 QR 代码添加徽标。
IronBarcode 是否可以将 QR 代码转换为 PDF 或 HTML?
是的,IronBarcode 允许您使用 SaveAsPdf 将 QR 代码转换为 PDF,或使用 SaveAsHtmlFile 将其转换为 HTML 文件。
使用 IronBarcode 生成 QR 代码需要哪些命名空间?
要使用 IronBarcode 生成 QR 代码,您需要包含 'IronBarCode' 命名空间以及系统命名空间,例如 System、System.Drawing 和 System.Linq。
IronBarcode 提供了哪些附加条形码功能?
IronBarcode 支持从图像读取各种条形码格式,提供增强的精度选项以及改进条形码识别的滤波器应用。
在哪里可以找到有关使用 IronBarcode 的更详细的文档?
您可以访问 IronBarcode 文档网站,获取有关使用该库进行 QR 代码生成和其他条形码相关任务的更详细信息和指导。






