IronBarcode 教程 C# QR碼生成器 Generate QR Codes in C# - Complete Tutorial for .NET Developers Jacob Mellor 更新日期:8月 20, 2025 Download IronBarcode NuGet 下載 DLL 下載 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English role="alert"> IronQR 是 Iron Software 全新的 .NET QR 代碼庫。利用尖端的機器學習技術從任何角度閱讀 QR 代碼,準確率達到 99.99%。輕鬆生成和自定義新的 QR 代碼! 立即開始使用 IronQR! 需要在你的 C# 應用程序中生成 QR 代碼嗎? 本教程將向您展示如何使用 IronBarcode 創建、自定義和驗證 QR 代碼,從簡單的單行實現到高級功能,如嵌入徽標和編碼二进制數據。 無論您是在構建庫存系統、活動票務平台,還是無接觸支付解決方案,都將學習如何在您的 .NET 應用程序中實現專業級別的 QR 代碼功能。 快速入門:IronBarcode 的單行 QR 代碼創建 準備好快速生成 QR 代碼了嗎?以下是如何使用 IronBarcode 的 QRCodeWriter API 生成 QR 代碼,只需一行代碼——自定義是可選但強大的。 Get started making PDFs with NuGet now: Install IronBarcode with NuGet Package Manager PM > Install-Package BarCode Copy and run this code snippet. var qr = QRCodeWriter.CreateQrCode("https://ironsoftware.com/", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium); qr.SaveAsPng("MyQR.png"); Deploy to test on your live environment Start using IronBarcode in your project today with a free trial Free 30 day Trial class="hsg-featured-snippet"> 最小化工作流程(5 步) 通過 NuGet 安裝 IronBarcode 使用一行代碼創建 QR 代碼:`QRCodeWriter.CreateQrCode()` 使用 `CreateQrCodeWithLogo()` 嵌入徽標 使用 `GeneratedBarcode.Verify()` 驗證可讀性 為高級應用程序編碼二進制數據 我如何在 C# 中安裝 QR 代碼庫? 立即開始在您的項目中使用 IronBarcode 並免費試用。 第一步: 免費啟動 使用 NuGet 包管理器安裝 IronBarcode,使用此簡單命令: Install-Package BarCode 通過 NuGet 安裝 或者,直接下載 IronBarcode DLL 並將其添加作為項目的參考。 導入所需的命名空間 添加這些命名空間以訪問 IronBarcode 的 QR 代碼生成功能: 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# 中創建簡單的 QR 代碼? 使用 IronBarcode 的 CreateQrCode 方法,只需一行代碼生成 QR 代碼: 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、文本或任何字符串數據) 尺寸:正方形 QR 代碼的像素尺寸(此範例中為 500x500) 錯誤更正:確定在次優環境下的可讀性(低、中、高、中四分位) 更高的錯誤更正級別使 QR 代碼即便部分損壞或隱蔽仍可讀,但它們會產生更密集的模式,帶有更多的數據模塊。 包含 "hello world" 文本的基本 QR 代碼,在 500x500 像素下使用中等錯誤更正生成 我如何向 QR 代碼添加徽標? 在 QR 代碼中嵌入徽標可以增強品牌識別,同時保持可掃描性。 IronBarcode 自動定位和調整徽標大小以保持 QR 代碼的完整性: 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 方法智能地處理徽標的放置方式: 自動調整徽標大小以保持 QR 代碼的可讀性 將其放置在靜區中以避免數據損壞 在更改 QR 代碼顏色時保留徽標的原始顏色 這種方法確保您的品牌 QR 代碼在所有掃描設備和應用程序中都保持全功能。 展示 IronBarcode 自動徽標調整和定位的 QR 代碼,帶有 Visual Studio 徽標 我如何將 QR 代碼導出到不同的格式? IronBarcode 支持多種導出格式,適應不同的使用情境。 將您的 QR 代碼導出為圖片、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:完美的網頁集成,不需要外部依賴 PNG/JPEG:標準圖片格式,用途廣泛 自定義後如何驗證 QR 代碼的可讀性? 顏色修改和徽標添加可能會影響 QR 代碼的可掃描性。 使用 Verify() 方法,確保自定義的 QR 代碼保持可讀: 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() 方法對 QR 代碼進行全面的掃描測試。 在部署前,這確保了不同掃描設備和光照條件下的兼容性。 成功驗證的深藍色 QR 代碼,體現了可靠掃描所需的適當對比度 我如何在 QR 代碼中編碼二進制數據? QR 代碼在有效存儲二進制數據方面表現出色。 這種能力支持高級應用,例如加密數據傳輸、文件共享和物聯網設備配置: 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 QR 代碼中的二進制編碼具有多個優勢: 效率:以緊湊的二進制格式存儲數據 多樣性:可處理任何數據類型(文件、加密內容、序列化對象) 完整性:無編碼問題地保留精確的字節序列 此功能將 IronBarcode 與基本的 QR 代碼庫區分開來,使您的應用程序能夠實現復雜數據交換場景。 展示 IronBarcode 高級編碼能力的存儲二進制數據的 QR 代碼 我如何在 C# 中讀取 QR 代碼? IronBarcode 提供靈活的 QR 代碼閱讀能力。 這是最簡單的方法: 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 這些高級閱讀選項使得在光照不足、圖像變形或印刷質量低劣等挑戰條件下也能可靠檢測 QR 代碼。 QR 代碼開發的下一步是什麼? 既然您已經掌握了使用 IronBarcode 的 QR 代碼生成,請探索以下高級主題: 從 PDF 文檔中提取 QR 代碼 實現批量 QR 代碼處理 對於困難的掃描應用圖像校正 下載資源 訪問完整的源代碼和示例: GitHub 儲存庫 源代碼下載 BarcodeWriter類參考 - 完整的方法文檔 在 API 參考中探究完整功能集: QRCodeWriter 類參考 BarcodeReader 類參考 GeneratedBarcode 類參考 替代方案:適用於高級 QR 應用的 IronQR 對於需要尖端 QR 代碼功能的項目,請考慮IronQR—Iron Software 的專門 QR 代碼庫,具有機器學習驅動的閱讀,準確率達到 99.99%,並具備先進的生成選項。 準備好在您的 .NET 應用程序中實現 QR 代碼了嗎? Start your free trial or 立即下載 IronBarcode。 常見問題解答 如何在 C# 中生成 QR 代碼? 您可以使用 IronBarcode 的 QRCodeWriter.CreateQrCode() 方法在 C# 中生成 QR 码。此方法允许您传递内容、大小和误差校正级别以有效创建 QR 码。 QR 码可以导出到哪些图像格式? 使用 IronBarcode,您可以将 QR 码导出为多种格式,包括 PNG、JPEG、PDF 和 HTML。为此可以使用 SaveAsPng()、SaveAsJpeg()、SaveAsPdf() 和 SaveAsHtmlFile() 方法。 如何将公司徽标添加到 QR 码? IronBarcode 提供 CreateQrCodeWithLogo() 方法,您可以传递包含徽标图像的 QRCodeLogo 对象。该库确保徽标尺寸和位置正确,以保持 QR 码的可读性。 什么是 QR 码误差校正,我应该选择哪个级别? QR 码中的误差校正即使部分损坏仍能保持可扫描性。IronBarcode 提供四个级别:低 (7%)、中 (15%)、四分位 (25%) 和高 (30%)。中是大多数用途合适的,而高适用于环境艰难的情况下。 如何验证自定义 QR 码的可读性? 您可以使用 GeneratedBarcode 对象上的 Verify() 方法来确保自定义 QR 码在进行诸如颜色更改或徽标添加等修改后仍然可扫描。 可以在 QR 码中编码二进制数据吗? 是的,IronBarcode 的 CreateQrCode() 方法支持编码字节数组,使您能够在 QR 码中存储文件或加密内容等二进制数据。 如何从 C# 中的图像读取 QR 码? 要从 C# 中的图像读取 QR 码,请使用 IronBarcode 的 BarcodeReader.Read() 方法。为了优化性能,在 BarcodeReaderOptions 中指定 BarcodeEncoding.QRCode。 QR 码的最大数据容量是多少? IronBarcode 生成的 QR 码根据选择的误差校正级别可以容纳多达 2,953 字节、4,296 个字母数字字符或 7,089 个数字。 如何改变 QR 码的颜色并确保其可扫描性? IronBarcode 中的 ChangeBarCodeColor() 方法允许您改变 QR 码的颜色。请务必在颜色更改后使用 Verify() 方法,以确保 QR 码的可读性不受影响。 一个专业的 QR 码库有哪些特性? IronQR 是 Iron Software 的专业库,包括机器学习驱动的 QR 码读取,准确率达 99.99%,并针对复杂应用的强大生成能力。 Jacob Mellor 立即與工程團隊聊天 首席技术官 Jacob Mellor 是 Iron Software 的首席技術官,作為 C# PDF 技術的先鋒工程師。作為 Iron Software 核心代碼的原作者,他自開始以來塑造了公司產品架構,與 CEO Cameron Rimington 一起將其轉變為一家擁有超過 50 名員工的公司,為 NASA、特斯拉 和 全世界政府機構服務。Jacob 持有曼徹斯特大學土木工程一級榮譽学士工程學位(BEng) (1998-2001)。他於 1999 年在倫敦開設了他的第一家軟件公司,並於 2005 年製作了他的首個 .NET 組件,專注於解決 Microsoft 生態系統內的複雜問題。他的旗艦產品 IronPDF & IronSuite .NET 庫在全球 NuGet 被安裝超過 3000 萬次,其基礎代碼繼續為世界各地的開發工具提供動力。擁有 25 年的商業經驗和 41 年的編碼專業知識,Jacob 仍專注於推動企業級 C#、Java 及 Python PDF 技術的創新,同時指導新一代技術領袖。 準備好開始了嗎? Nuget 下載 1,935,276 | 版本: 2025.11 剛剛發布 免費 NuGet 下載 總下載量:1,935,276 查看許可證