跳過到頁腳內容
使用IRONBARCODE
如何使用C#和IronBarcode生成QR碼

如何在C# Windows應用程式中生成QR碼

本教學深入探討如何創建二維碼,二維碼在工業應用和零售業中越來越受歡迎。 我們將使用IronBarcode庫(最受歡迎、功能最強大的庫之一)來示範如何產生二維碼。

如何在 C# Windows 窗體應用程式中產生二維碼

  1. 在 Microsoft Visual Studio 中建立一個 Windows 窗體應用程式
  2. 安裝二維碼庫
  3. 匯入命名空間以建立條碼
  4. 用一行程式碼建立二維碼
  5. 在二維碼圖像中加入徽標
  6. 將影像儲存為 PDF 或 HTML

1. 在 Microsoft Visual Studio 中建立一個 Windows 窗體應用程式

開啟 Visual Studio > 按一下"建立新專案" > 選擇"Windows 表單應用程式範本" > 按下"下一步" > 為專案命名 > 按"下一步" > 選擇目標.NET Framework > 按一下"建立"按鈕。

建立專案後,使用 Visual Studio 工具箱中的下列控制項設計表單:TextBoxButton

如何在 C# Windows 應用程式中產生二維碼,圖 1:用於載入映像並產生二維碼的 Windows 窗體應用程式 UI 一個用於載入圖像並產生二維碼的 Windows 窗體應用程式使用者介面

2. Install the QR Code Generator .NET Library in C

第一步是安裝條碼庫。 您可以透過以下三種方法之一來實現:

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
$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");
}
$vbLabelText   $csharpLabel

以下是二維碼產生器的輸出結果:

如何在 C# Windows 應用程式中產生二維碼,圖 4:二維碼來自:https://ironsoftware.com/csharp/barcode/docs/ 二維碼:https://ironsoftware.com/csharp/barcode/docs/

5. 在二維碼圖像中加入徽標

透過使用QRCodeWriter類別中的@@--CODE-505-- @@ 方法,可以為二維碼添加其他訊息,例如徽標。 範例程式碼展示了這有多麼簡單。

從您的電腦瀏覽徽標,它將在 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;
}
$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");
$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");
$vbLabelText   $csharpLabel

概括

IronBarcode為開發人員提供了一個友好的 API,用於讀取和寫入 C# .NET條碼和二維碼的數據,從而優化準確性並確保在實際應用中的低錯誤率。 有關IronBarcode的更多信息,請訪問此文檔網站

此外, IronBarcode也支援從影像中讀取條碼,並提供額外的選項以更精確地讀取條碼對影像套用濾鏡

目前,如果您購買完整的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 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me