IronBarcode 教程 C# QR碼生成器 使用 C# 產生二維碼 - .NET 開發人員完整教學 Jacob Mellor 更新:8月 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 代碼? 您可以使用 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 & Iron Suite .NET 庫在全球 NuGet 被安裝超過 3000 萬次,其基礎代碼繼續為世界各地的開發工具提供動力。擁有 25 年的商業經驗和 41 年的編碼專業知識,Jacob 仍專注於推動企業級 C#、Java 及 Python PDF 技術的創新,同時指導新一代技術領袖。 準備好開始了嗎? Nuget 下載 1,979,979 | Version: 2025.11 剛發表 免費下載 NuGet 下載總數:1,979,979 檢視授權