IronQR 教程 C# QR代码生成器应用 C# 二维码生成器 Application Curtis Chau 已更新:2026年1月31日 下载 IronQR NuGet 下载 免费试用 LLM副本 LLM副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在 Grok 中打开 向 Grok 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 This article was translated from English: Does it need improvement? Translated View the article in English IronQR是 Iron Software 全新推出的 .NET 二维码库。它利用尖端的机器学习技术,能够以 99.99% 的准确率从任何角度读取二维码。轻松生成和自定义二维码!立即开始使用 IronQR ! 欢迎使用我们的指南,了解如何使用C# 创建QR码! QR码和.NET条形码DLL已成为快速高效分享信息的流行方式。 无论您是在开发应用程序,管理网站,还是只是寻找一种整洁的方式分享链接,这些代码都能非常有用。 在本指南中,我们将演示如何使用IronQR高效地生成二维码,确保您可以生成符合您需求的二维码。 这个库使任何使用C#的人都可以轻松创建QR码,而无需深入复杂的逻辑。 我们将引导您完成每一步,确保您拥有开始所需的一切。 无论您是想在应用程序中添加QR码生成器功能还是只想了解它是如何完成的,您都来对地方了。 让我们开始吧。 如何在 C# 中创建二维码生成器 在Visual Studio中创建一个Windows窗体应用程序 使用 NuGet 安装 QR 库 设计表单的前端元素 编写二维码生成的逻辑 运行应用程序并开始创建二维码 Install 二维码生成器 Library in C 今天在您的项目中使用 IronQR,免费试用。 第一步: 免费开始 在开始之前,我们需要安装 IronQR NuGet 包。 Install-Package IronQR IronQR:C# QR 库 IronQR是一个C# QR码库,用于将QR码功能集成到.NET应用程序中。 IronQR支持多种.NET版本和项目类型,包括C#、VB.NET、F#、.NET Core、.NET Standard、.NET Framework等,确保与各种开发环境兼容,如Windows、Linux、macOS、iOS和Android。 IronQR以其高级功能而著称,包括读取QR码和生成QR码、支持多种图像格式以及诸如重新调整大小、样式和在QR码上添加徽标等定制选项。 IronQR的一些关键功能 IronQR不仅限于基本的QR码生成,还提供多种功能,旨在适应各种与QR码相关的任务。 让我们来了解一下这些功能,并查看它们的示例代码,您可以将这些代码集成到任何类型的 .NET 应用程序模板(如控制台应用程序)中。 读取QR码 IronQR在解码QR码方面表现出色,为用户提供了一种访问嵌入在QR码中的信息的简单方法。 您可以快速准确地从QR码中提取数据,从简单的URL到复杂的嵌入信息皆可。 :path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-code-generator-application-1.cs using IronQr; using IronSoftware.Drawing; using System; using System.Collections.Generic; IronQr.License.LicenseKey = "License-Key"; // Load the image file that contains the QR Code var inputImage = AnyBitmap.FromFile("QRCode.png"); // Prepare the image for QR code detection QrImageInput qrInput = new QrImageInput(inputImage); // Initialize the QR Code reader QrReader qrReader = new QrReader(); // Execute QR Code reading on the provided image IEnumerable<QrResult> qrResults = qrReader.Read(qrInput); // Assuming you have the QR results in qrResults as before foreach (var result in qrResults) { Console.WriteLine(result.Value); // Print the QR code content to the console } $vbLabelText $csharpLabel 翻译过程首先要纳入必要的命名空间:IronQR 和 IronSoftware.Drawing,并特别提及 IronSoftware.Drawing 命名空间中的 Color,以处理图像处理。 在开始读取二维码之前,必须使用您的许可证密钥激活软件,方法是将其分配给 IronQr.License.LicenseKey。 然后,该代码继续使用 AnyBitmap.FromFile("QRCode.png") 从文件中加载二维码图像。 加载图像后,下一步就是准备它以进行QR码检测。 此准备工作是通过创建一个 QrImageInput 对象来完成的,该对象用作图像的容器。 此功能的核心在于 QrReader 类,该类被实例化并用于执行二维码读取操作。 读者分析准备好的图片,搜索其中包含的任何二维码。 该操作的结果是一组 QrResult 对象,每个对象代表图像中检测到的二维码。 为了访问和使用二维码中编码的数据,该代码使用循环遍历结果集合。每个对象都包含二维码值等属性,这些属性可以被访问和显示。 自定义QR读取模式选项 IronQR 提供不同的 QR 码阅读模式,可满足各种需求。 混合扫描模式:兼顾速度和准确性,适用于不清晰或部分隐藏的二维码。 机器学习 (ML) 扫描模式:使用先进技术读取损坏或难以读取的 QR 代码,是难以检测场景的理想选择。 Basic Scan Mode(基本扫描模式):最简单、最快的方式,二维码清晰明了。 :path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-code-generator-application-2.cs using IronQr; using IronQr.Enum; using IronSoftware.Drawing; using System.Collections.Generic; IronQr.License.LicenseKey = "License-Key"; // Load the image file that contains the QR Code var inputImage = AnyBitmap.FromFile("QRCode.png"); QrImageInput mixedScanInput = new QrImageInput(inputImage, QrScanMode.OnlyDetectionModel); IEnumerable<QrResult> mixedScanResults = new QrReader().Read(mixedScanInput); QrImageInput mlScanInput = new QrImageInput(inputImage, QrScanMode.OnlyDetectionModel); IEnumerable<QrResult> mlScanResults = new QrReader().Read(mlScanInput); QrImageInput basicScanInput = new QrImageInput(inputImage, QrScanMode.OnlyBasicScan); IEnumerable<QrResult> basicScanResults = new QrReader().Read(basicScanInput); $vbLabelText $csharpLabel 读取高级QR码 IronQR 先进的 QR 码阅读功能为 QR 码扫描和解码提供了全面的方法。 这套功能超出了基本的阅读范围,提供了更深层次的互动和数据提取。 :path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-code-generator-application-3.cs using IronQr; using IronSoftware.Drawing; using System; using System.Collections.Generic; IronQr.License.LicenseKey = "License-Key"; var imageToScan = AnyBitmap.FromFile("QRCode.png"); QrImageInput qrInput = new QrImageInput(imageToScan); QrReader qrScanner = new QrReader(); IEnumerable<QrResult> scanResults = qrScanner.Read(qrInput); foreach (QrResult qrResult in scanResults) { Console.WriteLine(qrResult.Value); Console.WriteLine(qrResult.Url); foreach (IronSoftware.Drawing.PointF coordinate in qrResult.Points) { Console.WriteLine($"{coordinate.X}, {coordinate.Y}"); } } $vbLabelText $csharpLabel 要在 C# 应用程序中使用 IronQR 库创建 QR 代码生成器,请仔细按照以下步骤操作。 本指南将带您从设置Windows窗体应用程序、安装IronQR库、编写生成QR码的代码到理解输出。 步骤1:在Visual Studio中创建Windows应用程序 1.首先在计算机上启动 Visual Studio。 2.单击 "创建新项目 "按钮。 3.选择 Windows 表单应用程序 作为项目类型。 确保选择C#语言。  4.输入项目名称并选择保存位置。 然后在下一屏选择.NET Framework。 然后点击创建。  步骤2:安装IronQR库 现在是时候在项目中安装IronQR库了。 您可以通过不同的方法安装IronQR库。 使用NuGet包管理器安装 1.在解决方案资源管理器中右键单击您的项目,然后选择 管理 NuGet 包。 在搜索框中输入 IronQR,然后按Enter 键。 3.在列表中找到 IronQR,然后单击其旁边的 安装。 使用NuGet包管理器控制台安装 1.转到 工具 > NuGet 包管理器 > 包管理器控制台。 输入 Install-Package IronQR 并按 Enter 键。 步骤3:设计前端 3.1 标题头 启动QR码生成器应用程序后,用户立即会看到一个引人注目的标题,名为"QR Generator IronQR",以一种粗体和权威的字体书写。 3.2 输入部分 QR码的文本输入 用户可以在二维码中输入想要编码的数据。 徽标选择 "选择徽标"区域允许进行更高级的自定义设置。 用户可以上传一个徽标,该徽标将嵌入到二维码中。 颜色配置 颜色选择按钮允许用户自定义二维码的调色板。 3.3 样式参数 在颜色定制工具旁,用户会找到"维度"输入。这个数值设置是关键,因为它决定了QR码的整体尺寸,确保其完美地适应预定的显示上下文,无论是名片、传单还是数字屏幕。 允许用户指定二维码的整体大小。 边距设置 允许用户指定二维码周围的空白区域。 提供生成 QR 代码的实时预览。 生成QR 触发 QR 代码创建过程。 打开保存二维码的保存对话框。 清除之前的所有输入和选择。 第4步:编写后端逻辑 4.1 设置和初始化 包含必要的命名空间:IronQr 和 IronSoftware.Drawing。 :path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-code-generator-application-4.cs using IronQr; using IronSoftware.Drawing; using Color = IronSoftware.Drawing.Color; $vbLabelText $csharpLabel :path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-code-generator-application-5.cs public QR_Generator() { InitializeComponent(); SetLicenseKey(); EnsureDirectoryExists(qrCodesDirectory); } $vbLabelText $csharpLabel 为了确保IronQR无限制运行,必须应用有效的许可证密钥。 应用 IronQR 库的有效许可证密钥: private static void SetLicenseKey() { IronQr.License.LicenseKey = "YOUR_LICENSE_KEY"; } private static void SetLicenseKey() { IronQr.License.LicenseKey = "YOUR_LICENSE_KEY"; } $vbLabelText $csharpLabel 请将 "YOUR_LICENSE_KEY" 替换为您的实际许可证密钥。 该应用程序使用EnsureDirectoryExists方法来检查存储QR码的指定目录是否存在。 检查或创建必要的目录。 :path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-code-generator-application-7.cs private static void EnsureDirectoryExists(string path) { if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } } $vbLabelText $csharpLabel QR 代码目录的路径在 QR_Generator 类的构造函数中定义为 qrCodesDirectory,它结合了应用程序的启动路径和 "QR 代码 "文件夹名称: :path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-code-generator-application-8.cs string qrCodesDirectory = System.IO.Path.Combine(Application.StartupPath, "QR Codes"); $vbLabelText $csharpLabel 该应用程序在用户界面中提供了两个按钮,每个按钮都绑有一个用于选择颜色的方法:btn_color_Click用于QR码颜色,btn_background_Click用于背景颜色。 提供彩色对话框组件和实用功能。 :path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-code-generator-application-9.cs private string ColorToHex(System.Drawing.Color color) { return $"#{color.R:X2}{color.G:X2}{color.B:X2}"; } $vbLabelText $csharpLabel UpdateColor 方法获取选定的颜色,并使用十六进制字符串将其转换为 IronSoftware.Drawing.Color 格式,并根据选择更新二维码的前景色或背景色。 它还更新UI以反映新的颜色选择: :path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-code-generator-application-10.cs private void UpdateColor(ref Color targetColor, Control display, bool isBackground) { if (select_color.ShowDialog() == DialogResult.OK) { var hexColor = ColorToHex(select_color.Color); targetColor = new Color(hexColor); display.BackColor = select_color.Color; } } $vbLabelText $csharpLabel 4.5 添加徽标 允许用户选择徽标。 :path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-code-generator-application-11.cs private void btn_logo_Click(object sender, EventArgs e) { if (select_logo.ShowDialog() == DialogResult.OK) { try { logoBmp = new AnyBitmap(select_logo.FileName); selected_logo.Image = Image.FromFile(select_logo.FileName); } catch (Exception ex) { ShowError("An error occurred while loading the logo", ex.Message); } } } $vbLabelText $csharpLabel 4.6 QR码生成 包含根据用户输入生成二维码的逻辑。 :path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-code-generator-application-12.cs private void btn_generate_Click(object sender, EventArgs e) { GenerateQRCode(); } $vbLabelText $csharpLabel QrOptions 对象定义了纠错级别,增强了二维码对损坏或遮蔽的抵抗力。 CreateStyleOptions 方法生成一个 QrStyleOptions 对象,其中包含用户的自定义设置,如颜色、尺寸和徽标。 这里是详细的方法: :path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-code-generator-application-14.cs private QrStyleOptions CreateStyleOptions() { return new QrStyleOptions { BackgroundColor = bgColor, Color = color, Dimensions = txt_dimension.Value > 0 ? Convert.ToInt32(txt_dimension.Value) : throw new ArgumentException("Please select valid dimensions!"), Margins = Convert.ToInt32(txt_margin.Value), Logo = logoBmp != null ? new QrLogo { Bitmap = logoBmp, Width = 50, Height = 50, CornerRadius = 5 } : null }; } $vbLabelText $csharpLabel 4.7 保存二维码 保存生成的 QR 代码。 :path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-code-generator-application-15.cs private void btn_save_Click(object sender, EventArgs e) { SaveQRCode(); } private void SaveQRCode() { if (pictureBox.Image == null) { MessageBox.Show("There is no QR code to save.", "Error"); return; } saveFileDialog.Filter = "PNG Files|*.png|JPEG Files|*.jpg"; saveFileDialog.Title = "Save QR Code"; saveFileDialog.FileName = "QRCode"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { try { pictureBox.Image.Save(saveFileDialog.FileName, DetermineImageFormat(saveFileDialog.FileName)); MessageBox.Show("QR Code has been saved!", "Success"); } catch (Exception ex) { ShowError("An error occurred while saving the QR code", ex.Message); } } } $vbLabelText $csharpLabel :path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-code-generator-application-16.cs private System.Drawing.Imaging.ImageFormat DetermineImageFormat(string filePath) { return System.IO.Path.GetExtension(filePath).ToLower() == ".jpg" ? System.Drawing.Imaging.ImageFormat.Jpeg : System.Drawing.Imaging.ImageFormat.Png; } $vbLabelText $csharpLabel 4.8 重置应用程序 清除用户输入并重置表单状态。 :path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-code-generator-application-17.cs private void btn_reset_Click(object sender, EventArgs e) { ResetFields(); } private void ResetFields() { txt_QR.Text = string.Empty; txt_dimension.Value = 200; txt_margin.Value = 0; bgColor = Color.White; color = Color.Black; txt_selected_color.BackColor = System.Drawing.Color.White; txt_selected_bgcolor.BackColor = System.Drawing.Color.Black; logoBmp = null; selected_logo.Image = null; pictureBox.Image = null; } $vbLabelText $csharpLabel 4.9 错误处理 向用户显示错误信息。 :path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-code-generator-application-18.cs private static void ShowError(string title, string message) { MessageBox.Show($"{title}: {message}", "Error"); } $vbLabelText $csharpLabel 4.10 完整代码示例 结合上述所有功能的完整代码可在链接到您项目的示例文件中找到。 :path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-code-generator-application-19.cs using IronQr; using IronSoftware.Drawing; using Color = IronSoftware.Drawing.Color; namespace IronQR_QR_Generator_WinForms { public partial class QR_Generator : Form { string qrCodesDirectory = System.IO.Path.Combine(Application.StartupPath, "QR Codes"); Color bgColor = Color.White; Color color = Color.Black; AnyBitmap? logoBmp = null; public QR_Generator() { InitializeComponent(); SetLicenseKey(); EnsureDirectoryExists(qrCodesDirectory); } private static void SetLicenseKey() { IronQr.License.LicenseKey = "License-Key"; } private static void EnsureDirectoryExists(string path) { if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } } private void btn_color_Click(object sender, EventArgs e) { UpdateColor(ref color, txt_selected_color, false); } private void btn_background_Click(object sender, EventArgs e) { UpdateColor(ref bgColor, txt_selected_bgcolor, true); } private string ColorToHex(System.Drawing.Color color) { return $"#{color.R:X2}{color.G:X2}{color.B:X2}"; } private void UpdateColor(ref Color targetColor, Control display, bool isBackground) { if (select_color.ShowDialog() == DialogResult.OK) { var hexColor = ColorToHex(select_color.Color); targetColor = new Color(hexColor); display.BackColor = select_color.Color; } } private void btn_logo_Click(object sender, EventArgs e) { if (select_logo.ShowDialog() == DialogResult.OK) { try { logoBmp = new AnyBitmap(select_logo.FileName); selected_logo.Image = Image.FromFile(select_logo.FileName); } catch (Exception ex) { ShowError("An error occurred while loading the logo", ex.Message); } } } private void btn_generate_Click(object sender, EventArgs e) { GenerateQRCode(); } private void GenerateQRCode() { try { var options = new QrOptions(QrErrorCorrectionLevel.High); var myQr = QrWriter.Write(txt_QR.Text, options); var style = CreateStyleOptions(); var qrImage = myQr.Save(style); var fileName = $"{DateTime.Now:yyyyMMddHHmmssfff}_QR.png"; var fullPath = System.IO.Path.Combine(qrCodesDirectory, fileName); qrImage.SaveAs(fullPath); pictureBox.Image = Image.FromFile(fullPath); } catch (Exception ex) { ShowError("An error occurred during QR code generation or saving", ex.Message); } } private QrStyleOptions CreateStyleOptions() { return new QrStyleOptions { BackgroundColor = bgColor, Color = color, Dimensions = txt_dimension.Value > 0 ? Convert.ToInt32(txt_dimension.Value) : throw new ArgumentException("Please select valid dimensions!"), Margins = Convert.ToInt32(txt_margin.Value), Logo = logoBmp != null ? new QrLogo { Bitmap = logoBmp, Width = 50, Height = 50, CornerRadius = 5 } : null }; } private void btn_save_Click(object sender, EventArgs e) { SaveQRCode(); } private void SaveQRCode() { if (pictureBox.Image == null) { MessageBox.Show("There is no QR code to save.", "Error"); return; } saveFileDialog.Filter = "PNG Files|*.png|JPEG Files|*.jpg"; saveFileDialog.Title = "Save QR Code"; saveFileDialog.FileName = "QRCode"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { try { pictureBox.Image.Save(saveFileDialog.FileName, DetermineImageFormat(saveFileDialog.FileName)); MessageBox.Show("QR Code has been saved!", "Success"); } catch (Exception ex) { ShowError("An error occurred while saving the QR code", ex.Message); } } } private System.Drawing.Imaging.ImageFormat DetermineImageFormat(string filePath) { return System.IO.Path.GetExtension(filePath).ToLower() == ".jpg" ? System.Drawing.Imaging.ImageFormat.Jpeg : System.Drawing.Imaging.ImageFormat.Png; } private void btn_reset_Click(object sender, EventArgs e) { ResetFields(); } private void ResetFields() { txt_QR.Text = string.Empty; txt_dimension.Value = 200; txt_margin.Value = 0; bgColor = Color.White; color = Color.Black; txt_selected_color.BackColor = bgColor; txt_selected_bgcolor.BackColor = color; logoBmp = null; selected_logo.Image = null; pictureBox.Image = null; } private static void ShowError(string title, string message) { MessageBox.Show($"{title}: {message}", "Error"); } } } $vbLabelText $csharpLabel 步骤 5:运行应用程序 在执行应用程序时,主窗口将显示为有组织的输入、样式、输出和操作部分。 按照用户界面输入数据、自定义 QR 代码,并根据需要生成和保存 QR 代码。 结论 总之,本指南引导您完成在C#应用程序中使用IronQR库生成二维码的过程。 通过分解从在Visual Studio中设置项目、集成IronQR库、设计用户友好的界面到编写后端逻辑的步骤,我们展示了为您的应用程序添加二维码功能的便捷性。 对于那些有兴趣进一步探索 IronQR 功能的人来说,值得注意的是 IronQR 提供免费试用,让您开始使用。 如果您决定将IronQR集成到您的项目中,许可证起价为 $799,为专业级二维码生成提供经济高效的解决方案。 常见问题解答 我如何用C#创建一个二维码生成器应用? 要在C#中创建二维码生成器应用程序,您可以使用IronQR库。首先在Visual Studio中设置Windows窗体应用,使用NuGet安装IronQR,并设计您的应用前端。利用IronQR的功能实现二维码生成逻辑,例如颜色选择和徽标嵌入。 使用.NET二维码库有什么好处? .NET二维码库如IronQR提供了高级功能,例如高精度读取二维码,生成二维码的定制选项,以及支持多种.NET环境。它还允许调整二维码大小和样式。 我如何在C#中处理生成二维码时的错误? 在C#中,您可以通过使用try-catch块实现适当的错误处理机制来处理生成二维码时的错误。IronQR能帮助平稳管理错误,确保在二维码创建过程中出现的问题得到有效解决。 我可以使用二维码库将徽标嵌入到二维码中吗? 是的,您可以使用IronQR库将徽标嵌入到二维码中。这项功能允许您通过在设计中加入自定义徽标来增强二维码的品牌形象。 我如何在C#应用程序中保存生成的二维码? 您可以使用IronQR的功能指定存储目录来保存C#应用程序中生成的二维码。这使您能够在应用程序内有效管理和存储生成的二维码。 配置二维码库的许可证密钥需要哪些步骤? 要为IronQR配置许可证密钥,您需要在应用程序中加入许可代码。这通常涉及添加IronQR提供的特定代码行,以通过您购买的许可证激活该库。 我如何在我的C#应用程序中使用特定颜色样式化二维码? IronQR允许您通过其颜色自定义功能样式化二维码。您可以使用集成到应用程序中的颜色选择对话框选择二维码前景色和背景色。 在Visual Studio中安装二维码库的过程是什么? 要在Visual Studio中安装像IronQR这样的二维码库,请使用NuGet包管理器。搜索'IronQR'并点击'安装'以将其添加到您的项目中。或者,使用包管理器控制台键入命令'Install-Package IronQR'。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 准备开始了吗? Nuget 下载 62,157 | 版本: 2026.3 刚刚发布 免费试用 免费 NuGet 下载 总下载量:62,157 查看许可证 还在滚动吗? 想快速获得证据? PM > Install-Package IronQR 运行示例 观看您的 URL 变成 QR 代码。 免费 NuGet 下载 总下载量:62,157 查看许可证