使用IRONBARCODE 如何在C# Windows應用程式中生成QR碼 Jordi Bardia 更新日期:7月 28, 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 本教程深入介紹了如何創建二維碼,這些二維碼在工業應用和零售行業越來越受歡迎。 將使用IronBarcode庫之一個最受歡迎和強大的庫來展示如何生成二維碼。 如何在 C# Windows Forms 應用程序中生成二維碼 在 Microsoft Visual Studio 中創建一個Windows Forms應用程序 安裝二維碼庫 導入命名空間來創建條碼 使用一行代碼創建二維碼 向二維碼圖片添加徽標 將圖片保存為PDF或HTML 1. 在 Microsoft Visual Studio 中創建一個Windows Forms應用程序 打開 Visual Studio > 點擊創建新項目 > 選擇 Windows Forms 應用程序模板 > 按下一步 > 命名項目 > 按下一步 > 選擇目標 .NET Framework > 點擊創建按鈕。 創建項目後,使用Visual Studio工具箱中的以下控件來設計表單:PictureBox、Label、TextBox和Button。 用於加載圖像和生成二維碼的 Windows Forms 應用程序 UI 2. 安裝 QR 碼生成器 .NET 庫 第一步是安裝條碼庫。 您可以通過以下三種方法之一完成此操作: 2.1. 套件管理器控制台 在包管理器控制台中寫入以下命令。 它將為您下載並安裝該包。 Install-Package BarCode 套件管理器控制台 UI 中的安裝進度 2.2. NuGet 套件管理解決方案 您還可以使用 NuGet 套件解決方案安裝條碼庫。 只需按照以下步驟進行: 點擊工具 > NuGet包管理器 > 管理解決方案的NuGet包。 這將為您打開 NuGet 套件管理器。 點擊瀏覽,搜索BarCode,然後安裝類別庫。 在 NuGet 套件管理器中查找 BarCode 庫 2.3. 從鏈接下載 As an alternative, the IronBarCode.Dll can be downloaded and added to your project as a reference from .NET Barcode 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. 使用 1 行代碼創建二維碼 以下示例代碼允許您僅用一行代碼生成二維碼圖像。 在文本框中輸入您要生成二維碼的所需文本。 將此代碼放入“生成 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 以下是二維碼生成器的輸出: 二維碼: https://ironsoftware.com/csharp/barcode/docs/ 5. 為二維碼圖片添加徽標 By using the CreateQrCodeWithLogo method from the QRCodeWriter class, additional information, such as a logo, can be added to the QR code. 示例代碼說明了這很容易。 從計算機中瀏覽徽標,然後它將在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 此代碼將 Iron 徽標添加到條碼中。 它會自動調整到適當的大小,以確保純代碼仍可讀,並將該徽標對齊到二維碼的正方形網格,使其看起來適當。 C# 創建帶有徽標圖像的二維碼 6. 保存為 PDF 或 HTML 圖像 最後,生成的二維碼可以保存為 PDF 或 HTML 圖像。 最後一行代碼會在默認的 PDF 瀏覽器中打開 PDF,方便使用。 Add the SaveAsPdf in the Generate PDF button and SaveAsHtmlFile in the Generate HTML button. // 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 的更多信息,請訪問此文件網站。 Additionally, IronBarcode also supports reading barcodes from images, as well as providing extra options to read barcodes with more accuracy or apply filters to images. 目前,如果您購買完整的 Iron Suite,您只需支付兩個庫的價格即可獲得五個庫。 更多詳情請訪問報價頁面。 常見問題解答 如何在 C# Windows 應用程序中生成 QR 碼? 您可以使用 IronBarcode 庫在 C# Windows 應用程序中生成 QR 碼,利用 QRCodeWriter.CreateQrCode 方法。這允許您從文本輸入生成 QR 碼並將其保存為 PNG 文件。 使用 IronBarcode 生成 QR 碼有何好處? IronBarcode 提供用戶友好的 API,用於生成高準確性和低錯誤率的 QR 碼。它還支持其他功能,如向 QR 碼添加徽標以及將 QR 碼保存為 PDF 或 HTML 文件。 如何在 Microsoft Visual Studio 中設置 Windows Forms 應用程序以生成 QR 碼? 要在 Microsoft Visual Studio 中設置 Windows Forms 應用程序,請打開 Visual Studio,選擇 '創建新項目',選擇 'Windows Forms 應用程序模板',命名您的項目,選擇目標 .NET Framework,然後點擊 '創建'。 在 C# 項目中安裝 QR 碼庫的過程是什麼? IronBarcode 庫可以通過包管理器控制台、NuGet 包管理器解決方案或直接下載 IronBarCode.DLL 安裝到 C# 項目中。 我可以使用 IronBarcode 向 QR 碼添加徽標嗎? 是的,您可以使用 IronBarcode 庫通過 QRCodeWriter 類中的 CreateQrCodeWithLogo 方法向 QR 碼添加徽標,該方法允許您從計算機中選擇圖片。 是否可以使用 IronBarcode 將 QR 碼轉換為 PDF 或 HTML? 是的,IronBarcode 允許您使用 SaveAsPdf 將 QR 碼轉換為 PDF,或使用 SaveAsHtmlFile 將其轉換為 HTML 文件。 生成 QR 碼需要哪些與 IronBarcode 相關的命名空間? 要使用 IronBarcode 生成 QR 碼,您需要包括 'IronBarCode' 命名空間,以及 System、System.Drawing 和 System.Linq 等系統命名空間。 IronBarcode 提供哪些額外的條碼功能? IronBarcode 支持從圖像中讀取各種條碼格式,提供加強準確性的選項及應用過濾器以提高條碼識別能力。 我可以在哪裡找到有關使用 IronBarcode 的更詳細文檔? 您可以訪問 IronBarcode 文檔網站,了解有關使用該庫進行 QR 碼生成和其他條碼相關任務的更詳細信息和指導。 Jordi Bardia 立即與工程團隊聊天 軟體工程師 Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 發表日期 10月 19, 2025 如何使用VB.NET在Crystal Reports中打印條碼 在VB.NET中使用IronBarcode SDK在Crystal Reports中生成和打印條碼的分步教程,確保可靠的條碼集成。 閱讀更多 發表日期 9月 29, 2025 IronBarcode對比.NET中的開源條碼閱讀器 了解如何使用IronBarcode在C#中讀取條碼 閱讀更多 發表日期 9月 29, 2025 如何在ASP.NET應用程式中掃描條碼 了解如何在ASP.NET中使用IronBarcode掃描條碼 閱讀更多 .NET QR碼產生器(代碼示例教程)如何在ASP.NET C#中打印條碼
發表日期 10月 19, 2025 如何使用VB.NET在Crystal Reports中打印條碼 在VB.NET中使用IronBarcode SDK在Crystal Reports中生成和打印條碼的分步教程,確保可靠的條碼集成。 閱讀更多