跳至頁尾內容
使用IRONBARCODE
如何使用C#和IronBarcode生成QR碼

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

本教程深入介紹如何建立QR碼,這在工業應用和零售行業中越來越受歡迎。 將使用最受歡迎和強大的程式庫之一IronBarcode程式庫,來演示如何生成QR碼。

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

  1. 在Microsoft Visual Studio中建立一個Windows Forms應用程式
  2. 安裝QR碼程式庫
  3. 匯入命名空間以建立條形碼
  4. 用一行程式碼建立QR碼
  5. 向QR碼圖像新增標誌
  6. 將圖像儲存為PDF或HTML

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

打開Visual Studio > 點擊建立新專案 > 選擇Windows Forms應用程式模板 > 按下一步 > 為專案命名 > 按下一步 > 選擇您的目標.NET Framework > 點擊建立按鈕。

建立專案後,使用Visual Studio工具箱中的以下控件設計表單:PictureBox, Label, TextBox, 和 Button

如何在C# Windows應用程式中生成QR碼,圖1:用於載入圖像和生成QR碼的Windows Forms應用程式使用者介面 用於載入圖像和生成QR碼的Windows Forms應用程式使用者介面

2. 在C#中安裝QR碼生成器.NET程式庫

第一步是安裝條形碼程式庫。 您可以使用以下三種方法之一進行安裝:

2.1. 套件管理器控制台

在套件管理器主控台中輸入以下命令。 它會為您下載並安裝該套件。

Install-Package BarCode

如何在C# Windows應用程式中生成QR碼,圖2:套件管理器控制台使用者介面中的安裝進度 套件管理器控制台使用者介面中的安裝進度

2.2. NuGet套件管理解決方案

您還可以使用NuGet套件解決方案安裝條形碼程式庫。 只需遵循以下步驟:

點擊 工具 > NuGet 套件管理器 > 為解決方案管理 NuGet 套件

這將為您打開NuGet套件管理器。 點擊瀏覽,搜索BarCode,然後安裝類庫。

如何在C# Windows應用程式中生成QR碼,圖3:在NuGet套件管理器中查找BarCode程式庫 在NuGet套件管理器中查找BarCode程式庫

2.3. 從連結下載

作為替代方案,可以從.NET Barcode 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. 用1行程式碼建立QR碼

以下範例程式碼允許您僅用一行程式碼生成一個QR碼圖像。 在您想要生成QR碼的文字框中輸入所需的文字。 將此程式碼放在"生成PNG"按鈕的點擊事件中。 QR碼圖像可以儲存為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

這是QR碼生成器的輸出:

如何在C# Windows應用程式中生成QR碼,圖4:QR碼:https://ironsoftware.com/csharp/barcode/docs/ QR code of: https://ironsoftware.com/csharp/barcode/docs/****

5. 向QR碼圖像新增標誌

通過使用CreateQrCodeWithLogo方法和QRCodeWriter類,可以向QR碼新增額外資訊,例如一個標誌。 範例程式碼說明了這有多簡單。

從您的電腦中瀏覽標誌,然後它將在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標誌。 它會自動調整到適當的大小,使純碼仍可讀,並將該標誌對齊到QR碼的正方形網格上,使其看起來適宜。

如何在C# Windows應用程式中生成QR碼,圖5:C#建立包含標誌圖像的QR碼 C#建立包含標誌圖像的QR碼

6. 儲存為PDF或HTML圖像

最後,生成的QR碼可以儲存為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的條形碼和QR碼資料,優化準確性,並確保在現實世界中低錯誤率。 有關IronBarcode的更多資訊,請存取此文件網站

此外,IronBarcode還支持從圖像中讀取條碼,以及提供額外選項來更精確地讀取條碼對圖像應用濾鏡

目前,如果您購買完整的Iron Suite,您可以享受到五個程式庫僅需支付兩個的價格。 有關更多詳情,請存取定價頁面

常見問題

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

您可以使用IronBarcode程式庫在C# Windows應用程式中使用QRCodeWriter.CreateQrCode方法生成QR Code。這可讓您從文字輸入生成QR Code並將其儲存為PNG檔案。

使用IronBarcode生成QR Code的好處是什麼?

IronBarcode提供使用者友好的API,用於生成高精確度和低錯誤率的QR Code。它還支持其他功能,如在QR Code中新增標誌和將QR Code儲存為PDF或HTML文件。

如何在Microsoft Visual Studio中設定Windows Forms應用程式以生成QR Code?

要在Microsoft Visual Studio中設定Windows Forms應用程式,開啟Visual Studio,選擇『建立新專案』,選擇『Windows Forms應用程式範本』,為專案命名,選擇目標.NET Framework,然後點擊『建立』。

在C#專案中安裝QR Code程式庫的過程是什麼?

IronBarcode程式庫可以通過Package Manager Console、NuGet Package Manager Solution或直接下載IronBarCode.DLL安裝到C#專案中。

我可以使用IronBarcode為QR Code新增標誌嗎?

是的,您可以使用IronBarcode程式庫,利用QRCodeWriter類的CreateQrCodeWithLogo方法,從電腦中選擇一個圖像來為QR Code新增標誌。

可以使用IronBarcode將QR Code轉換為PDF或HTML嗎?

是的,IronBarcode允許您使用SaveAsPdf將QR Code轉換為PDF,或使用SaveAsHtmlFile轉換為HTML文件。

生成QR Code所需的命名空間是什麼?

要使用IronBarcode生成QR Code,您需要包含'IronBarCode'命名空間,以及System、System.Drawing和System.Linq等系統命名空間。

IronBarcode提供了哪些其他條碼功能?

IronBarcode支持從圖像中讀取各種條碼格式,提供增強精確度和應用過濾器以改善條碼識別的選項。

我可以在哪裡找到有關使用IronBarcode的更詳細文件?

您可以存取IronBarcode文件網站,獲取有關使用該程式庫生成QR Code和其他條碼相關任務的更詳細資訊和指導。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話