跳過到頁腳內容
使用 IRONQR

如何在 C# 中創建 QR 碼生成器應用程序

歡迎來到使用 C# 建立 QR 代碼的指南! QR 碼和 .NET 條碼 DLL 已經成為快速、有效率地分享資訊的流行方式。 無論您是在開發應用程式、管理網站,或只是在尋找一種整潔的方式來分享連結,這些代碼都會非常有用。 在本指南中,我們將示範如何使用 IronQR有效率地產生 QR 碼,以確保您能產生符合需求的 QR 碼。 這個函式庫讓任何使用 C# 的人都能輕鬆建立 QR 碼,而不需要涉入複雜的邏輯。 我們會指導您完成整個步驟,確保您擁有開始工作所需的一切。 無論您是想在您的應用程式中加入 QR 代碼產生器功能,或是只是好奇它是如何產生的,您都找對地方了。 讓我們開始吧

如何在 C# 中建立 QR 碼產生器

IronQR 的一些主要功能讀取 QR 碼 qrResults = qrReader.Read(qrInput); // Print the value of each QR code found in the image foreach (var result in qrResults) { Console.WriteLine(result.Value); } ``` 我們使用下列 QR 進行掃描: ![How to Create a QR Code Generator Application In C#:圖 1 - QR Code PNG 圖檔](/static-assets/qr/blog/csharp-qr-code-generator-application-tutorial/csharp-qr-code-generator-application-tutorial-1.webp)。 我們得到了這樣的結果: ![How to Create a QR Code Generator Application In C#:圖 2 - 讀取 QR 碼輸出](/static-assets/qr/blog/csharp-qr-code-generator-application-tutorial/csharp-qr-code-generator-application-tutorial-2.webp)。 此過程會先納入必要的命名空間,`IronQR` 和 `IronSoftware.Drawing` ,並特別提到 `IronSoftware.Drawing` 命名空間中的 `Color` 以處理影像操作。 在進入 QR 代碼讀取程序之前,必須先將授權金鑰指定給 `IronQR.License.LicenseKey` 來啟動軟體。 代碼接著使用 `AnyBitmap.FromFile("QRCode.png")` 從檔案載入 QR 代碼影像。 影像載入後,下一步就是準備 QR 碼偵測。 這項準備工作是透過建立 `QrImageInput` 物件來完成,該物件可作為影像的容器。 此功能的核心在於 `QrReader` 類別,該類別會實體化並用於執行 QR 代碼讀取作業。 讀取器會分析準備好的影像 `qrInput` ,搜尋其中包含的任何 QR 代碼。 此操作的結果是 `QrResult` 物件的集合,每個物件代表影像中偵測到的 QR 代碼。 為了存取並使用 QR 代碼中編碼的資料,程式碼會使用 `foreach` 環路迭代結果集合。每個 `QrResult` 物件包含可存取和顯示的屬性,例如 QR 代碼的值。

自訂 QR 讀取模式選項

mixedScanResults = new QrReader().Read(mixedScanInput); // Using machine learning scan mode QrImageInput mlScanInput = new QrImageInput(inputImage, QrScanMode.OnlyDetectionModel); IEnumerable mlScanResults = new QrReader().Read(mlScanInput); // Using basic scan mode QrImageInput basicScanInput = new QrImageInput(inputImage, QrScanMode.OnlyBasicScan); IEnumerable basicScanResults = new QrReader().Read(basicScanInput); ```

閱讀進階 QR 碼

scanResults = qrScanner.Read(qrInput); // Print the value, URL, and coordinates of each QR code found in the image foreach (QrResult qrResult in scanResults) { Console.WriteLine(qrResult.Value); Console.WriteLine(qrResult.Url); foreach (IronSoftware.Drawing.PointF coordinate in qrResult.Points) { Console.WriteLine($"{coordinate.X}, {coordinate.Y}"); } } ``` 這是我們使用 IronQR 掃描 QR 碼時的輸出: ![How to Create a QR Code Generator Application In C#:圖 3 - 掃描 QR 碼輸出](/static-assets/qr/blog/csharp-qr-code-generator-application-tutorial/csharp-qr-code-generator-application-tutorial-3.webp) 我們使用以下 QR 碼: ![How to Create a QR Code Generator Application In C#:圖 4 - QR 碼輸入](/static-assets/qr/blog/csharp-qr-code-generator-application-tutorial/csharp-qr-code-generator-application-tutorial-4.webp) 每個 `QrResult` 物件可存取解碼資料 (`Value`)、任何內嵌的 URL (`Url`) 以及影像中 QR 代碼的空間座標 (`Points`)。 對於偵測到的每個 QR 碼,IronQR 都會提供詳細的資訊,包括 QR 碼中所包含的確切內容和任何 URL。 此外,該函式庫還提供了圖像中 QR 碼角落的精確座標(透過 `Points` 屬性)。 若要在 C# 應用程式中使用 IronQR 的 QR 碼程式庫建立 QR 碼產生器,請仔細遵循下列步驟。 本指南將帶您完成設定 Windows 表單應用程式、安裝 IronQR 函式庫、編寫代碼以產生 QR 碼,以及瞭解輸出結果。

步驟 1:在 Visual Studio 中建立 Windows 應用程式

步驟 2:安裝 IronQR 函式庫使用 NuGet 套件管理員安裝使用 NuGet 套件管理員安裝 **NuGet套件管理員** > **套件管理員控制台**。 ![How to Create a QR Code Generator Application In C#:圖 9 - NuGet 套件管理員](/static-assets/qr/blog/csharp-qr-code-generator-application-tutorial/csharp-qr-code-generator-application-tutorial-9.webp) * 輸入 **Install-Package IronQR**,然後按 Enter。 ![How to Create a QR Code Generator Application In C#:圖 10 - 安裝 IronQR](/static-assets/qr/blog/csharp-qr-code-generator-application-tutorial/csharp-qr-code-generator-application-tutorial-10.webp)

步驟 3:設計前端

3.1標題標頭3.2「輸入」部分QR Code 的文字輸入標誌選擇顏色設定3.3 造型參數尺寸設定邊界設定3.4輸出預覽3.5動作按鈕儲存 QR 碼重置表單步驟 4:撰寫後端邏輯4.1 設定與初始化4.2 授權金鑰組態4.3目錄管理4.4顏色選擇 0 ? Convert.ToInt32(txt_dimension.Value) : throw new ArgumentException("Please select valid dimensions!"), Margins = Convert.ToInt32(txt_margin.Value), Logo = logoBmp != null ? new QrLogo { Bitmap = logoBmp, Width = 50, Height = 50, CornerRadius = 5 } : null }; } ``` 此方法會建立一個 `QrStyleOptions` 物件,然後 QR 代碼產生邏輯會使用此物件來套用使用者的偏好設定。 選項包括 * **`BackgroundColor`和`Color`**:這些屬性可設定 QR 代碼的背景和前景顏色,讓您擁有符合品牌或美學偏好的個人化外觀。 * **`尺寸`**:此屬性決定 QR 代碼的尺寸,可讓 QR 代碼在不同的情境或媒體中靈活運用。 * **`邊界`**:此屬性可設定 QR 代碼周圍的邊界大小,確保 QR 代碼與周圍的元素隔離,這對於可擴展性而言非常重要。 * **`徽標`**:如果使用者選擇包含徽標,則會在此設定特定的尺寸和角落半徑,以提供精緻的外觀。 ### 4.7 儲存 QR 碼。 儲存功能由「Save」按鈕觸發,該按鈕連結至 `btn_save_Click` 方法。 此方法會呼叫 `SaveQRCode` 來實作儲存邏輯。 過程包括顯示儲存檔案對話框,讓使用者選擇儲存 QR 代碼的檔案格式和位置。 ```csharp private void btn_save_Click(object sender, EventArgs e) { SaveQRCode(); } private void SaveQRCode() { if (pictureBox.Image == null) { MessageBox.Show("There is no QR code to save.", "Error"); return; } saveFileDialog.Filter = "PNG Files|*.png|JPEG Files|*.jpg"; saveFileDialog.Title = "Save QR Code"; saveFileDialog.FileName = "QRCode"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { try { pictureBox.Image.Save(saveFileDialog.FileName, DetermineImageFormat(saveFileDialog.FileName)); MessageBox.Show("QR Code has been saved!", "Success"); } catch (Exception ex) { ShowError("An error occurred while saving the QR code", ex.Message); } } } ``` 此方法會檢查是否有已產生的 QR 代碼。 如果是這樣,它會向使用者提出以 PNG 或 JPEG 格式儲存檔案的選項。 `DetermineImageFormat` 函式可確保圖像能根據使用者選擇的檔案副檔名,以正確的格式儲存。 ```csharp private System.Drawing.Imaging.ImageFormat DetermineImageFormat(string filePath) { return System.IO.Path.GetExtension(filePath).ToLower() == ".jpg" ? System.Drawing.Imaging.ImageFormat.Jpeg : System.Drawing.Imaging.ImageFormat.Png; } ``` 這種靈活性可讓使用者選擇最適合其需求的格式,無論是優先考量品質 (PNG) 或檔案大小 (JPEG)。 ### 4.8 重設應用程式。 重設功能與「Reset」按鈕相關聯,該按鈕呼叫 `btn_reset_Click` 方法。 此方法反過來會呼叫 `ResetFields` 這個函式,其目的是清除所有使用者輸入,並恢復所有設定的預設值,包括文字欄位、顏色選項和選取的標誌。 ```csharp private void btn_reset_Click(object sender, EventArgs e) { ResetFields(); } private void ResetFields() { txt_QR.Text = string.Empty; txt_dimension.Value = 200; txt_margin.Value = 0; bgColor = Color.White; color = Color.Black; txt_selected_color.BackColor = bgColor; txt_selected_bgcolor.BackColor = color; logoBmp = null; selected_logo.Image = null; pictureBox.Image = null; } ``` 此方法可重新設定 QR 代碼產生所涉及的每個元件。 例如,清除 QR 代碼文字、將尺寸和邊距設定為預設值,並移除任何選取的顏色或標誌。

4.9 錯誤處理

4.10完整程式碼範例 0 ? Convert.ToInt32(txt_dimension.Value) : throw new ArgumentException("Please select valid dimensions!"), Margins = Convert.ToInt32(txt_margin.Value), Logo = logoBmp != null ? new QrLogo { Bitmap = logoBmp, Width = 50, Height = 50, CornerRadius = 5 } : null }; } private void btn_save_Click(object sender, EventArgs e) { SaveQRCode(); } private void SaveQRCode() { if (pictureBox.Image == null) { MessageBox.Show("There is no QR code to save.", "Error"); return; } saveFileDialog.Filter = "PNG Files|*.png|JPEG Files|*.jpg"; saveFileDialog.Title = "Save QR Code"; saveFileDialog.FileName = "QRCode"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { try { pictureBox.Image.Save(saveFileDialog.FileName, DetermineImageFormat(saveFileDialog.FileName)); MessageBox.Show("QR Code has been saved!", "Success"); } catch (Exception ex) { ShowError("An error occurred while saving the QR code", ex.Message); } } } private System.Drawing.Imaging.ImageFormat DetermineImageFormat(string filePath) { return System.IO.Path.GetExtension(filePath).ToLower() == ".jpg" ? System.Drawing.Imaging.ImageFormat.Jpeg : System.Drawing.Imaging.ImageFormat.Png; } private void btn_reset_Click(object sender, EventArgs e) { ResetFields(); } private void ResetFields() { txt_QR.Text = string.Empty; txt_dimension.Value = 200; txt_margin.Value = 0; bgColor = Color.White; color = Color.Black; txt_selected_color.BackColor = bgColor; txt_selected_bgcolor.BackColor = color; logoBmp = null; selected_logo.Image = null; pictureBox.Image = null; } private static void ShowError(string title, string message) { MessageBox.Show($"{title}: {message}", "Error"); } } } ```

步驟 5:執行應用程式

結論

常見問題解答

如何在 C# 中創建一個 QR 代碼生成器應用程式?

若要在 C# 中創建一個 QR 代碼生成器應用程式,從在 Visual Studio 中設置 Windows Forms 應用程式開始,通過 NuGet 安裝 IronQR 庫,設計包含文本、徽標和顏色輸入欄位的界面,然後使用 IronQR 的方法實現 QR 代碼生成邏輯。

在 C# 中,QR 代碼有哪些自定義選項?

IronQR 為 QR 代碼提供自定義選項,如更改顏色、尺寸和邊距。您還可以通過調整 QrStyleOptions 對象中的設置來向 QR 代碼添加徽標。

如何在 C# 項目中安裝 QR 代碼庫?

使用 Visual Studio 中的 NuGet 包管理器在您的 C# 項目中安裝 IronQR 庫。搜索 IronQR 並點擊"安裝"按鈕以將其添加到您的項目中。

在 C# 中,我可以使用哪些格式保存生成的 QR 代碼?

使用 IronQR,您可以將生成的 QR 代碼保存為多種格式,包括 PNG 和 JPEG,從而在質量和文件大小方面提供靈活性。

我可以使用 C# 庫讀取 QR 代碼嗎?

是的,IronQR 包含讀取 QR 代碼的功能,讓您能夠有效地解碼和提取 QR 圖像中的數據。

使用 C# QR 代碼庫有哪些好處?

像 IronQR 這樣的 C# QR 代碼庫簡化了生成和讀取 QR 代碼的過程,提供多格式支持、自定義選項以及與各種 .NET 版本的兼容性等功能。

如何在 C# 中處理生成 QR 代碼的錯誤?

錯誤處理可以通過在您的 QR 代碼生成應用程式中使用 try-catch 塊來進行。這可確保在 QR 代碼創建過程中發生的任何問題得到妥善管理,提供用戶友好的反饋。

我需要許可證才能在 C# 應用程式中使用 QR 代碼庫嗎?

是的,要無限制地使用 IronQR,您需要一個有效的許可證密鑰。可以提供試用版進行初步測試,專業許可證可供購買以獲得完全訪問。

我該如何在 C# 中將徽標整合到 QR 代碼中?

使用 IronQR,您可以通過在 QrStyleOptions 對象中設置徽標圖片來將徽標整合到 QR 代碼中。這使您能夠以品牌元素個性化 QR 代碼。

運行 C# 中的 QR 代碼生成器應用程式需要哪些步驟?

在 C# 中設置和自定義 QR 代碼生成器應用程式後,只需在 Visual Studio 中運行應用程式,生成 QR 代碼,並使用提供的選項將其保存為您想要的格式。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。