使用 IRONBARCODE 如何在 C# Windows 應用程式中產生二維碼 Curtis Chau 更新:2025年7月28日 下載 IronBarcode NuGet 下載 DLL 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 本教學深入探討如何創建二維碼,二維碼在工業應用和零售業中越來越受歡迎。 我們將使用IronBarcode庫(最受歡迎、功能最強大的庫之一)來示範如何產生二維碼。 如何在 C# Windows 窗體應用程式中產生二維碼 在 Microsoft Visual Studio 中建立一個 Windows 窗體應用程式 安裝二維碼庫 匯入命名空間以建立條碼 用一行程式碼建立二維碼 在二維碼圖像中加入徽標 將影像儲存為 PDF 或 HTML 1. 在 Microsoft Visual Studio 中建立一個 Windows 窗體應用程式 開啟 Visual Studio > 按一下"建立新專案" > 選擇"Windows 表單應用程式範本" > 按下"下一步" > 為專案命名 > 按"下一步" > 選擇目標 .NET Framework > 按一下"建立"按鈕。 建立專案後,使用 Visual Studio 工具箱中的下列控制項設計窗體: PictureBox 、 Label 、 TextBox和Button 。 如何在 C# Windows 應用程式中產生二維碼,圖 1:用於載入映像並產生二維碼的 Windows 窗體應用程式 UI 一個用於載入圖像並產生二維碼的 Windows 窗體應用程式使用者介面 2. 在 C# 中安裝 QR 碼產生器 .NET 函式庫 第一步是安裝條碼庫。 您可以透過以下三種方法之一來實現: 2.1. 軟體包管理器控制台 在軟體包管理器控制台中輸入以下命令。 它將為您下載並安裝該軟體包。 Install-Package BarCode 如何在 C# Windows 應用程式中產生二維碼,圖 2:套件管理器控制台 UI 中的安裝進度 軟體包管理器控制台介面中的安裝進度 2.2. NuGet 套件管理器解決方案 您也可以使用 NuGet 套件解決方案安裝條碼庫。 只需按照以下步驟操作: 按一下"工具" > "NuGet 套件管理員" > "管理解決方案的 NuGet 套件" 。 這將開啟 NuGet 套件管理器。 點擊"瀏覽",搜尋"條碼",然後安裝類別庫。 如何在 C# Windows 應用程式中產生二維碼,圖 3:在 NuGet 套件管理器中尋找 BarCode 庫 在 NuGet 套件管理器中尋找 BarCode 庫 2.3. 從連結下載 作為替代方案,可以從.NET 條碼 DLL下載IronBarCode.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 queries 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 queries Imports IronBarCode ' Provides functionality for QR and barcode generation Imports System ' Contains fundamental classes and base classes that define commonly-used value and reference data types Imports System.Drawing ' Provides access to GDI+ basic graphic functionality Imports System.Linq ' Provides classes and interfaces that support queries $vbLabelText $csharpLabel 4. 建立一個包含一行程式碼的二維碼 以下範例程式碼可讓您僅使用一行程式碼產生二維碼圖像。 在文字方塊中輸入要產生二維碼的文字。 將此程式碼放入"產生 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"); } ' Simple QR Code generation Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) ' Generate a QR code from the text provided in the TextBox Dim qrCode As GeneratedBarcode = QRCodeWriter.CreateQrCode(textBox1.Text) ' Save the generated QR code as a PNG file qrCode.SaveAsPng("QrCode.png") End Sub $vbLabelText $csharpLabel 以下是二維碼產生器的輸出結果: 如何在 C# Windows 應用程式中產生二維碼,圖 4:二維碼範例:https://ironsoftware.com/csharp/barcode/docs/ 二維碼:https://ironsoftware.com/csharp/barcode/docs/ 5. 在二維碼圖像中加入徽標 透過使用QRCodeWriter類別中的CreateQrCodeWithLogo方法,可以為二維碼添加徽標等附加資訊。 範例程式碼展示了這有多麼簡單。 從您的電腦瀏覽徽標,它將在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; } ' Open file dialog to select an image Dim open As 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 Then ' 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 End If $vbLabelText $csharpLabel 接下來,只需在文本框中輸入文本,將此代碼放入"生成 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"); ' Generate a QR code with a logo Dim qrCode As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo(textBox1.Text, ImageFileName, 500) ' Save the generated QR code with logo as a PNG file qrCode.SaveAsPng("QrCodeWithImage.png") $vbLabelText $csharpLabel 這段程式碼會將鐵桿標誌添加到條碼中。 它會自動調整大小,使純程式碼仍然可讀,並將徽標與二維碼方格網格對齊,使其看起來合適。 如何在 C# Windows 應用程式中產生二維碼,圖 5:使用標誌圖像在 C# 中建立二維碼 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"); ' Generate a QR code with a logo Dim qrCode As GeneratedBarcode = 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") $vbLabelText $csharpLabel 摘要 IronBarcode 為開發人員提供了一個友好的 API,用於讀取和寫入 C# .NET 條碼和二維碼的數據,從而優化準確性並確保在實際應用中的低錯誤率。 有關 IronBarcode 的更多信息,請訪問此文檔網站。 此外,IronBarcode 還支援從影像中讀取條碼,並提供額外的選項以更精確地讀取條碼或對影像套用濾鏡。 目前,如果您購買完整的 Iron Suite,您只需支付兩個庫的價格即可獲得五個庫。 請造訪定價頁面以了解更多詳情。 常見問題解答 如何在C# Windows應用程式中產生二維碼? 您可以使用 IronBarcode 函式庫,透過QRCodeWriter.CreateQrCode方法在 C# Windows 應用程式中產生二維碼。此方法可讓您根據文字輸入產生二維碼並將其儲存為 PNG 檔案。 使用 IronBarcode 產生二維碼有哪些好處? IronBarcode 提供了一個用戶友好的 API,用於產生高精度、低錯誤率的二維碼。它還支援其他功能,例如在二維碼中添加徽標以及將二維碼保存為 PDF 或 HTML 檔案。 如何在 Microsoft Visual Studio 中設定用於產生二維碼的 Windows 窗體應用程式? 要在 Microsoft Visual Studio 中設定 Windows 窗體應用程序,請開啟 Visual Studio,選擇“建立新專案”,選擇“Windows 窗體應用程式範本”,為專案命名,選擇目標 .NET Framework,然後按一下“建立”。 在 C# 專案中安裝 QR 碼庫的步驟是什麼? IronBarcode 庫可以透過套件管理器控制台、NuGet 套件管理器解決方案安裝到 C# 專案中,也可以直接下載 IronBarCode.DLL 檔案。 我可以使用IronBarcode在二維碼中加入logo嗎? 是的,您可以使用 IronBarcode 庫,透過QRCodeWriter類別中的CreateQrCodeWithLogo方法向 QR 碼添加徽標,該方法允許您從電腦中選擇圖像。 是否可以使用 IronBarcode 將二維碼轉換為 PDF 或 HTML? 是的,IronBarcode 允許您使用SaveAsPdf將二維碼轉換為 PDF 文件,或使用SaveAsHtmlFile將二維碼轉換為 HTML 文件。 使用 IronBarcode 產生二維碼需要哪些命名空間? 要使用 IronBarcode 產生二維碼,您需要包含「IronBarCode」命名空間,以及系統命名空間,例如 System、System.Drawing 和 System.Linq。 IronBarcode還提供哪些其他條碼功能? IronBarcode 支援從影像中讀取各種條碼格式,提供提高準確性的選項,並可套用濾鏡來改善條碼辨識。 在哪裡可以找到關於使用 IronBarcode 的更詳細文件? 您可以造訪 IronBarcode 文件網站,以取得有關使用該程式庫進行二維碼產生和其他條碼相關任務的更多詳細資訊和指導。 Curtis Chau 立即與工程團隊聊天 技術撰稿人 Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。 相關文章 更新2026年1月22日 ASP.NET 條碼掃描器教學:C# 條碼產生器指南 學習如何使用 IronBarcode 在 ASP.NET 中掃描條碼 閱讀更多 發表日期 2026年1月21日 C# 資料矩陣產生器:IronBarcode 完整指南 C# 資料矩陣條碼產生器教學。學習如何使用 IronBarcode 建立 ECC200 資料矩陣條碼。提供簡單的二維條碼生成程式碼範例。 閱讀更多 發表日期 2026年1月21日 使用 IronBarcode 的 Xamarin 條碼產生器建立專業品質的條碼 使用 IronBarcode 和 Xamarin 條碼產生器,學習如何建立專業品質的條碼。 閱讀更多 如何使用 IronBarcode 在 C# 中產生二維碼如何在 ASP.NET 中使用 C# 列...
發表日期 2026年1月21日 C# 資料矩陣產生器:IronBarcode 完整指南 C# 資料矩陣條碼產生器教學。學習如何使用 IronBarcode 建立 ECC200 資料矩陣條碼。提供簡單的二維條碼生成程式碼範例。 閱讀更多
發表日期 2026年1月21日 使用 IronBarcode 的 Xamarin 條碼產生器建立專業品質的條碼 使用 IronBarcode 和 Xamarin 條碼產生器,學習如何建立專業品質的條碼。 閱讀更多