IronBarcode 教程 C# 二维码生成器 使用 C# 生成二维码 - .NET 开发人员完整教程 Jacob Mellor 已更新:八月 20, 2025 下载 IronBarcode NuGet 下载 DLL 下载 免费试用 法学硕士副本 法学硕士副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在双子座打开 向 Gemini 询问此页面 打开困惑 向 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# 应用程序中生成二维码吗? 本教程将向您展示如何使用 IronBarcode 创建、自定义和验证二维码——从简单的单行实现到高级功能,例如徽标嵌入和二进制数据编码。 无论您是构建库存系统、活动票务平台还是非接触式支付解决方案,您都将学习如何在 .NET 应用程序中实现专业级二维码功能。 快速入门:使用 IronBarcode 创建一行二维码 想快速生成二维码吗?以下是如何使用 IronBarcode 的QRCodeWriter API,只需一行代码即可生成二维码——自定义是可选的,但功能强大。 立即开始使用 NuGet 创建 PDF 文件: 使用 NuGet 包管理器安装 IronBarcode PM > Install-Package BarCode 复制并运行这段代码。 var qr = QRCodeWriter.CreateQrCode("https://ironsoftware.com/", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium); qr.SaveAsPng("MyQR.png"); 部署到您的生产环境中进行测试 立即开始在您的项目中使用 IronBarcode,免费试用! 免费试用30天 最小工作流程(5 个步骤) 通过 NuGet 安装 IronBarcode Create a QR code with one line: `QRCodeWriter.CreateQrCode()` Embed logos using `CreateQrCodeWithLogo()` Verify readability with `GeneratedBarcode.Verify()` 对二进制数据进行编码,以用于高级应用程序 如何在C#中安装二维码库? 今天在您的项目中使用 IronBarcode,免费试用。 第一步: 免费开始 使用 NuGet 包管理器,通过以下简单命令安装 IronBarcode: Install-Package BarCode 通过 NuGet 安装 或者,直接下载 IronBarcode DLL并将其作为引用添加到您的项目中。 导入所需的命名空间 添加以下命名空间即可访问 IronBarcode 的二维码生成功能: using IronBarCode; using System; using System.Drawing; using System.Linq; using IronBarCode; using System; using System.Drawing; using System.Linq; Imports IronBarCode Imports System Imports System.Drawing Imports System.Linq $vbLabelText $csharpLabel 如何在C#中创建简单的二维码? 使用 IronBarcode 的CreateQrCode方法,只需一行代码即可生成二维码: using IronBarCode; // Generate a QR code with text content var qrCode = QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium); qrCode.SaveAsPng("MyQR.png"); using IronBarCode; // Generate a QR code with text content var qrCode = QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium); qrCode.SaveAsPng("MyQR.png"); Imports IronBarCode ' Generate a QR code with text content Private qrCode = QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium) qrCode.SaveAsPng("MyQR.png") $vbLabelText $csharpLabel CreateQrCode方法接受三个参数: -文本内容:要编码的数据(支持 URL、文本或任何字符串数据) -尺寸:方形二维码的像素尺寸(本例中为 500x500) -纠错:确定在次优条件下的可读性(低、中、四分位数或高) 更高的纠错级别使二维码即使在部分损坏或被遮挡的情况下也能保持可读性,但这会导致更密集的图案和更多的数据模块。 !使用 IronBarcode 在 C# 中生成标准二维码 一个包含"hello world"文本的基本二维码,尺寸为500x500像素,采用中等纠错级别生成。 如何在二维码中添加Logo? 在二维码中嵌入徽标可以增强品牌识别度,同时保持可扫描性。 IronBarcode 会自动调整徽标的位置和大小,以保持二维码的完整性: using IronBarCode; using IronSoftware.Drawing; // Load logo image QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png"); // Create QR code with embedded logo GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo); // Customize appearance myQRCodeWithLogo.ResizeTo(500, 500).SetMargins(10).ChangeBarCodeColor(Color.DarkGreen); // Save the branded QR code myQRCodeWithLogo.SaveAsPng("myQRWithLogo.png"); using IronBarCode; using IronSoftware.Drawing; // Load logo image QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png"); // Create QR code with embedded logo GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo); // Customize appearance myQRCodeWithLogo.ResizeTo(500, 500).SetMargins(10).ChangeBarCodeColor(Color.DarkGreen); // Save the branded QR code myQRCodeWithLogo.SaveAsPng("myQRWithLogo.png"); Imports IronBarCode Imports IronSoftware.Drawing ' Load logo image Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png") ' Create QR code with embedded logo Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo) ' Customize appearance myQRCodeWithLogo.ResizeTo(500, 500).SetMargins(10).ChangeBarCodeColor(Color.DarkGreen) ' Save the branded QR code myQRCodeWithLogo.SaveAsPng("myQRWithLogo.png") $vbLabelText $csharpLabel CreateQrCodeWithLogo方法通过以下方式智能处理徽标放置: 自动调整徽标大小,以保持二维码的可读性 将其放置在静默区内,以避免数据损坏 更改二维码颜色时,保持徽标的原始颜色不变 这种方法可以确保您的品牌二维码在所有扫描设备和应用程序上都能完全正常工作。 带有嵌入式 Visual Studio 徽标的二维码 包含 Visual Studio 徽标的二维码,展示了 IronBarcode 的自动徽标尺寸调整和位置功能。 如何将二维码导出为不同格式? IronBarcode支持多种导出格式,以满足不同的使用场景。 将您的二维码导出为图片、PDF 或 HTML 文件: using IronBarCode; using System.Drawing; // Create QR code with logo QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png"); GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo); // Apply custom styling myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen); // Export to multiple formats myQRCodeWithLogo.SaveAsPdf("MyQRWithLogo.pdf"); // PDF document myQRCodeWithLogo.SaveAsHtmlFile("MyQRWithLogo.html"); // Standalone HTML myQRCodeWithLogo.SaveAsPng("MyQRWithLogo.png"); // PNG image myQRCodeWithLogo.SaveAsJpeg("MyQRWithLogo.jpg"); // JPEG image using IronBarCode; using System.Drawing; // Create QR code with logo QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png"); GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo); // Apply custom styling myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen); // Export to multiple formats myQRCodeWithLogo.SaveAsPdf("MyQRWithLogo.pdf"); // PDF document myQRCodeWithLogo.SaveAsHtmlFile("MyQRWithLogo.html"); // Standalone HTML myQRCodeWithLogo.SaveAsPng("MyQRWithLogo.png"); // PNG image myQRCodeWithLogo.SaveAsJpeg("MyQRWithLogo.jpg"); // JPEG image Imports IronBarCode Imports System.Drawing ' Create QR code with logo Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png") Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo) ' Apply custom styling myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen) ' Export to multiple formats myQRCodeWithLogo.SaveAsPdf("MyQRWithLogo.pdf") ' PDF document myQRCodeWithLogo.SaveAsHtmlFile("MyQRWithLogo.html") ' Standalone HTML myQRCodeWithLogo.SaveAsPng("MyQRWithLogo.png") ' PNG image myQRCodeWithLogo.SaveAsJpeg("MyQRWithLogo.jpg") ' JPEG image $vbLabelText $csharpLabel 每种格式都有其特定的用途: PDF :非常适合用于打印文档和报告 HTML :非常适合无需外部依赖项的 Web 集成 PNG/JPEG :用途广泛的标准图像格式 定制完成后如何验证二维码的可读性? 颜色修改和添加徽标可能会影响二维码的扫描效果。 使用Verify()方法确保您的自定义二维码仍然可读: using IronBarCode; using IronSoftware.Drawing; using System; using System.Drawing; // Generate QR code with logo QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png"); GeneratedBarcode myVerifiedQR = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo); // Apply light color (may affect readability) myVerifiedQR.ChangeBarCodeColor(Color.LightBlue); // Verify the QR code can still be scanned if (!myVerifiedQR.Verify()) { Console.WriteLine("LightBlue is not dark enough to be read accurately. Let's try DarkBlue"); myVerifiedQR.ChangeBarCodeColor(Color.DarkBlue); } // Save verified QR code myVerifiedQR.SaveAsHtmlFile("MyVerifiedQR.html"); // Open in default browser System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo { FileName = "MyVerifiedQR.html", UseShellExecute = true }); using IronBarCode; using IronSoftware.Drawing; using System; using System.Drawing; // Generate QR code with logo QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png"); GeneratedBarcode myVerifiedQR = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo); // Apply light color (may affect readability) myVerifiedQR.ChangeBarCodeColor(Color.LightBlue); // Verify the QR code can still be scanned if (!myVerifiedQR.Verify()) { Console.WriteLine("LightBlue is not dark enough to be read accurately. Let's try DarkBlue"); myVerifiedQR.ChangeBarCodeColor(Color.DarkBlue); } // Save verified QR code myVerifiedQR.SaveAsHtmlFile("MyVerifiedQR.html"); // Open in default browser System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo { FileName = "MyVerifiedQR.html", UseShellExecute = true }); Imports IronBarCode Imports IronSoftware.Drawing Imports System Imports System.Drawing ' Generate QR code with logo Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png") Private myVerifiedQR As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo) ' Apply light color (may affect readability) myVerifiedQR.ChangeBarCodeColor(Color.LightBlue) ' Verify the QR code can still be scanned If Not myVerifiedQR.Verify() Then Console.WriteLine("LightBlue is not dark enough to be read accurately. Let's try DarkBlue") myVerifiedQR.ChangeBarCodeColor(Color.DarkBlue) End If ' Save verified QR code myVerifiedQR.SaveAsHtmlFile("MyVerifiedQR.html") ' Open in default browser System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo With { .FileName = "MyVerifiedQR.html", .UseShellExecute = True }) $vbLabelText $csharpLabel Verify()方法会对你的二维码执行全面的扫描测试。 这样可以确保在部署前,不同扫描设备和光照条件下的兼容性。 已验证的二维码,采用深蓝色和 Visual Studio 徽标。 一个已成功验证的深蓝色二维码,对比度良好,可确保可靠扫描。 如何在二维码中编码二进制数据? 二维码在高效存储二进制数据方面表现出色。 此功能支持加密数据传输、文件共享和物联网设备配置等高级应用: using IronBarCode; using System; using System.Linq; // Convert string to binary data byte[] binaryData = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/"); // Create QR code from binary content QRCodeWriter.CreateQrCode(binaryData, 500).SaveAsPng("MyBinaryQR.png"); // Read and verify binary data integrity var myReturnedData = BarcodeReader.Read("MyBinaryQR.png").First(); // Confirm data matches original if (binaryData.SequenceEqual(myReturnedData.BinaryValue)) { Console.WriteLine("Binary Data Read and Written Perfectly"); } else { throw new Exception("Data integrity check failed"); } using IronBarCode; using System; using System.Linq; // Convert string to binary data byte[] binaryData = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/"); // Create QR code from binary content QRCodeWriter.CreateQrCode(binaryData, 500).SaveAsPng("MyBinaryQR.png"); // Read and verify binary data integrity var myReturnedData = BarcodeReader.Read("MyBinaryQR.png").First(); // Confirm data matches original if (binaryData.SequenceEqual(myReturnedData.BinaryValue)) { Console.WriteLine("Binary Data Read and Written Perfectly"); } else { throw new Exception("Data integrity check failed"); } Imports IronBarCode Imports System Imports System.Linq ' Convert string to binary data Private binaryData() As Byte = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/") ' Create QR code from binary content QRCodeWriter.CreateQrCode(binaryData, 500).SaveAsPng("MyBinaryQR.png") ' Read and verify binary data integrity Dim myReturnedData = BarcodeReader.Read("MyBinaryQR.png").First() ' Confirm data matches original If binaryData.SequenceEqual(myReturnedData.BinaryValue) Then Console.WriteLine("Binary Data Read and Written Perfectly") Else Throw New Exception("Data integrity check failed") End If $vbLabelText $csharpLabel 二维码中的二进制编码具有以下几个优点: -效率:以紧凑的二进制格式存储数据 -多功能性:可处理任何数据类型(文件、加密内容、序列化对象) -完整性:保留精确的字节序列,无编码问题 此功能使 IronBarcode 与基本的二维码库区别开来,从而在您的应用程序中实现复杂的数据交换场景。 包含二进制编码数据的二维码 存储二进制数据的二维码,展示了IronBarcode的先进编码功能。 如何在C#中读取二维码? IronBarcode 提供灵活的二维码读取功能。 最简单的方法如下: using IronBarCode; using System; using System.Linq; // Read QR code with optimized settings BarcodeResults result = BarcodeReader.Read("QR.png", new BarcodeReaderOptions() { ExpectBarcodeTypes = BarcodeEncoding.QRCode }); // Extract and display the decoded value if (result != null && result.Any()) { Console.WriteLine(result.First().Value); } else { Console.WriteLine("No QR codes found in the image."); } using IronBarCode; using System; using System.Linq; // Read QR code with optimized settings BarcodeResults result = BarcodeReader.Read("QR.png", new BarcodeReaderOptions() { ExpectBarcodeTypes = BarcodeEncoding.QRCode }); // Extract and display the decoded value if (result != null && result.Any()) { Console.WriteLine(result.First().Value); } else { Console.WriteLine("No QR codes found in the image."); } Imports IronBarCode Imports System Imports System.Linq ' Read QR code with optimized settings Private result As BarcodeResults = BarcodeReader.Read("QR.png", New BarcodeReaderOptions() With {.ExpectBarcodeTypes = BarcodeEncoding.QRCode}) ' Extract and display the decoded value If result IsNot Nothing AndAlso result.Any() Then Console.WriteLine(result.First().Value) Else Console.WriteLine("No QR codes found in the image.") End If $vbLabelText $csharpLabel 对于需要精细控制的更复杂场景: using IronBarCode; using System; using System.Linq; // Configure advanced reading options BarcodeReaderOptions options = new BarcodeReaderOptions { Speed = ReadingSpeed.Faster, // Optimize for speed ExpectMultipleBarcodes = false, // Single QR code expected ExpectBarcodeTypes = BarcodeEncoding.QRCode, // QR codes only Multithreaded = true, // Enable parallel processing MaxParallelThreads = 4, // Utilize multiple CPU cores RemoveFalsePositive = true, // Filter out false detections ImageFilters = new ImageFilterCollection() // Apply preprocessing { new AdaptiveThresholdFilter(), // Handle varying lighting new ContrastFilter(), // Enhance contrast new SharpenFilter() // Improve edge definition } }; // Read with advanced configuration BarcodeResults result = BarcodeReader.Read("QR.png", options); using IronBarCode; using System; using System.Linq; // Configure advanced reading options BarcodeReaderOptions options = new BarcodeReaderOptions { Speed = ReadingSpeed.Faster, // Optimize for speed ExpectMultipleBarcodes = false, // Single QR code expected ExpectBarcodeTypes = BarcodeEncoding.QRCode, // QR codes only Multithreaded = true, // Enable parallel processing MaxParallelThreads = 4, // Utilize multiple CPU cores RemoveFalsePositive = true, // Filter out false detections ImageFilters = new ImageFilterCollection() // Apply preprocessing { new AdaptiveThresholdFilter(), // Handle varying lighting new ContrastFilter(), // Enhance contrast new SharpenFilter() // Improve edge definition } }; // Read with advanced configuration BarcodeResults result = BarcodeReader.Read("QR.png", options); Imports IronBarCode Imports System Imports System.Linq ' Configure advanced reading options Private options As New BarcodeReaderOptions With { .Speed = ReadingSpeed.Faster, .ExpectMultipleBarcodes = False, .ExpectBarcodeTypes = BarcodeEncoding.QRCode, .Multithreaded = True, .MaxParallelThreads = 4, .RemoveFalsePositive = True, .ImageFilters = New ImageFilterCollection() From { New AdaptiveThresholdFilter(), New ContrastFilter(), New SharpenFilter() } } ' Read with advanced configuration Private result As BarcodeResults = BarcodeReader.Read("QR.png", options) $vbLabelText $csharpLabel 这些高级读取选项能够在光线不足、图像失真或印刷质量差等具有挑战性的条件下可靠地检测二维码。 二维码开发的下一步是什么? 既然您已经掌握了使用 IronBarcode 生成二维码,不妨探索以下高级主题: 从PDF文档中提取二维码 -实现批量二维码处理 -对疑难扫描图像进行图像校正 下载资源 查看完整源代码和示例: GitHub 仓库 源代码下载 API 文档 请参阅 API 参考文档,了解完整的功能集: QRCodeWriter 类参考 条形码读取器类参考 GeneratedBarcode 类参考 替代方案:IronQR,适用于高级二维码应用 对于需要尖端二维码功能的项目,请考虑使用IronQR ——Iron Software 的专业二维码库,它具有机器学习驱动的读取功能,准确率高达 99.99%,并提供高级生成选项。 准备好在您的 .NET 应用程序中实现二维码了吗? 立即开始免费试用或下载 IronBarcode 。 常见问题解答 如何在 C# 中生成 QR 码? 你可以在C#中使用IronBarcode的QRCodeWriter.CreateQrCode()方法生成二维码。此方法允许你传递内容、尺寸和纠错级别以高效地创建二维码。 二维码可以导出为哪些图像格式? 使用IronBarcode,你可以将二维码导出为多种格式,包括PNG、JPEG、PDF和HTML。方法如SaveAsPng()、SaveAsJpeg()、SaveAsPdf()和SaveAsHtmlFile()可用于此目的。 如何将公司Logo添加到二维码中? IronBarcode提供CreateQrCodeWithLogo()方法,你可以传递包含Logo图像的QRCodeLogo对象。该库确保Logo尺寸和位置正确,以保持二维码可读性。 什么是二维码错误校正,应该选择哪个级别? 二维码中的错误校正使其即使在部分损坏时仍可扫描。IronBarcode提供四个级别:低(7%)、中(15%)、四分之一(25%)和高(30%)。中适用于大多数用途,而高在具有挑战性的环境中是理想的选择。 如何验证定制二维码的可读性? 你可以在GeneratedBarcode对象上使用Verify()方法,以确保你的定制二维码在进行颜色变化或Logo添加后的修改后仍然可扫描。 可以在二维码中编码二进制数据吗? 是的,IronBarcode的CreateQrCode()方法支持编码字节数组,使你能够在二维码中存储二进制数据,例如文件或加密内容。 如何在C#中从图像读取二维码? 要在C#中从图像读取二维码,请使用IronBarcode的BarcodeReader.Read()方法。为了优化性能,指定BarcodeEncoding.QRCode在BarcodeReaderOptions中。 二维码的最大数据容量是多少? IronBarcode生成的二维码最多可容纳2,953字节、4,296个字母数字字符或7,089个数字字符,具体取决于所选的错误校正级别。 如何更改二维码的颜色同时确保其可读性? IronBarcode中的ChangeBarCodeColor()方法允许你改变二维码的颜色。始终在颜色变化后使用Verify()方法确认二维码的可读性未受影响。 专用二维码库提供哪些功能? IronQR是Iron Software的专门库,包含高级功能,如支持99.99%精度的机器学习驱动的二维码读取和针对复杂应用定制的强大生成能力。 Jacob Mellor 立即与工程团队聊天 首席技术官 Jacob Mellor 是 Iron Software 的首席技术官,是 C# PDF 技术的先锋工程师。作为 Iron Software 核心代码库的原始开发者,自公司成立以来,他就塑造了公司的产品架构,并与首席执行官 Cameron Rimington 一起将其转变成一家公司,拥有50多人,服务于 NASA、特斯拉和全球政府机构。Jacob 拥有曼彻斯特大学 (1998-2001) 的一级荣誉土木工程学士学位。1999 年在伦敦创办了自己的第一家软件公司,并于 2005 年创建了他的第一个 .NET 组件后,他专注于解决微软生态系统中的复杂问题。他的旗舰 IronPDF 和 Iron Suite .NET 库在全球已获得超过 3000 万次的 NuGet 安装,其基础代码继续为全球使用的开发者工具提供支持。拥有 25 年商业经验和 41 年编程经验的 Jacob 仍专注于推动企业级 C#、Java 和 Python PDF 技术的创新,同时指导下一代技术领导者。 准备开始了吗? Nuget 下载 1,979,979 | Version: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:1,979,979 查看许可证