IronBarcode 教程 在 C# .NET 中生成條碼圖片 如何在 C# .NET 應用程式中產生條碼圖像 Jacob Mellor 更新:8月 20, 2025 下載 IronBarcode NuGet 下載 DLL 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English 需要在 .NET 應用程式中快速產生專業條碼圖像嗎? 本教學將向您展示如何使用 IronBarcode 建立、自訂和匯出條碼——從簡單的單行實現到進階樣式技術,讓您可以完全控制條碼的外觀。 快速入門:立即建立並儲存條碼圖像 使用 IronBarcode,只需一次簡單的呼叫即可產生和匯出條碼圖像。 使用 CreateBarcode 方法建立條碼,選擇格式和大小,然後呼叫 SaveAsPng——無需複雜的設定。 立即開始使用 NuGet 建立 PDF 檔案: 使用 NuGet 套件管理器安裝 IronBarcode PM > Install-Package BarCode 複製並運行這段程式碼。 IronBarCode.BarcodeWriter.CreateBarcode("Hello123", BarcodeWriterEncoding.Code128, 200, 100).SaveAsPng("barcode.png"); 部署到您的生產環境進行測試 立即開始在您的專案中使用 IronBarcode,免費試用! 免費試用30天 最小工作流程(5 個步驟) 透過 NuGet 套件管理器安裝 IronBarcode 用一行程式碼產生一個簡單的條碼 為條碼套用自訂樣式和註釋 將條碼匯出為圖像、PDF 或 HTML 格式 使用 Fluent API 實現高效條碼生成 如何在 C# 中安裝條碼產生器庫? 使用 NuGet 套件管理器安裝 IronBarcode 只需幾秒鐘。 您可以直接透過套件管理器控制台安裝,也可以手動下載 DLL 檔案。 Install-Package BarCode IronBarcode 為 .NET 開發人員提供全面的條碼產生功能。 如何使用 C# 產生簡單的條碼? 創建你的第一個條碼只需要兩行程式碼。 下面的範例示範如何產生標準 Code128 條碼並將其儲存為影像檔案。 using IronBarCode; // Create a barcode with your desired content and encoding type GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128); // Save the barcode as a PNG image file myBarcode.SaveAsPng("myBarcode.png"); // Optional: Open the generated image in your default viewer System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("myBarcode.png") { UseShellExecute = true }); using IronBarCode; // Create a barcode with your desired content and encoding type GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128); // Save the barcode as a PNG image file myBarcode.SaveAsPng("myBarcode.png"); // Optional: Open the generated image in your default viewer System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("myBarcode.png") { UseShellExecute = true }); Imports IronBarCode ' Create a barcode with your desired content and encoding type Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128) ' Save the barcode as a PNG image file myBarcode.SaveAsPng("myBarcode.png") ' Optional: Open the generated image in your default viewer System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("myBarcode.png") With {.UseShellExecute = True}) $vbLabelText $csharpLabel BarcodeWriter.CreateBarcode()方法是產生條碼的入口點。 它接受兩個參數:要編碼的資料和來自BarcodeWriterEncoding枚舉的條碼格式。 IronBarcode 支援所有主流條碼格式,包括 Code128、Code39、EAN13、UPC-A、PDF417、DataMatrix 和 QR 碼。 產生後的GeneratedBarcode物件提供了多種匯出選項。 您可以將其儲存為各種影像格式(PNG、JPEG、GIF、TIFF),匯出為 PDF,甚至可以將其作為System.Drawing.Bitmap檢索,以便在您的應用程式中進行進一步處理。 A Code128 barcode generated with IronBarcode displaying a URL 我可以自訂產生的條碼的外觀嗎? IronBarcode 提供豐富的自訂選項,遠遠超出基本的條碼產生功能。 您可以新增註解、調整顏色、設定邊距,並控制條碼外觀的各個方面。 using IronBarCode; using IronSoftware.Drawing; // Create a QR code with advanced styling options GeneratedBarcode myBarCode = BarcodeWriter.CreateBarcode( "https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode ); // Add descriptive text above the barcode myBarCode.AddAnnotationTextAboveBarcode("Product URL:"); // Display the encoded value below the barcode myBarCode.AddBarcodeValueTextBelowBarcode(); // Set consistent margins around the barcode myBarCode.SetMargins(100); // Customize the barcode color (purple in this example) myBarCode.ChangeBarCodeColor(Color.Purple); // Export as an HTML file for web integration myBarCode.SaveAsHtmlFile("MyBarCode.html"); using IronBarCode; using IronSoftware.Drawing; // Create a QR code with advanced styling options GeneratedBarcode myBarCode = BarcodeWriter.CreateBarcode( "https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode ); // Add descriptive text above the barcode myBarCode.AddAnnotationTextAboveBarcode("Product URL:"); // Display the encoded value below the barcode myBarCode.AddBarcodeValueTextBelowBarcode(); // Set consistent margins around the barcode myBarCode.SetMargins(100); // Customize the barcode color (purple in this example) myBarCode.ChangeBarCodeColor(Color.Purple); // Export as an HTML file for web integration myBarCode.SaveAsHtmlFile("MyBarCode.html"); Imports IronBarCode Imports IronSoftware.Drawing ' Create a QR code with advanced styling options Private myBarCode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode) ' Add descriptive text above the barcode myBarCode.AddAnnotationTextAboveBarcode("Product URL:") ' Display the encoded value below the barcode myBarCode.AddBarcodeValueTextBelowBarcode() ' Set consistent margins around the barcode myBarCode.SetMargins(100) ' Customize the barcode color (purple in this example) myBarCode.ChangeBarCodeColor(Color.Purple) ' Export as an HTML file for web integration myBarCode.SaveAsHtmlFile("MyBarCode.html") $vbLabelText $csharpLabel GeneratedBarcode類別提供了一系列豐富的自訂方法: -註解:使用AddAnnotationTextAboveBarcode()和AddAnnotationTextBelowBarcode()函數在條碼周圍新增自訂標籤或說明。 -值顯示: AddBarcodeValueTextBelowBarcode()方法會自動以人類可讀的格式顯示編碼後的資料。 -間距:使用SetMargins()控制留白,以確保良好的掃描效果和視覺美觀。 -顏色:使用ChangeBarCodeColor()和ChangeBackgroundColor()來變更前景色和背景色。 匯出選項:儲存為影像檔案、PDF 或獨立的 HTML 文件 帶有自訂顏色和註釋文字的樣式化二維碼 有關詳細的自訂選項,請參閱GeneratedBarcode類別文檔,其中涵蓋了所有可用的樣式方法和屬性。 如何用一行程式碼建立並匯出條碼? IronBarcode 實現了一個流暢的 API 設計模式,支援方法鏈,從而編寫出更簡潔、更易讀的程式碼。 當需要對條碼進行多次轉換時,這種方法尤其有用。 using IronBarCode; using IronSoftware.Drawing; // Generate, style, and convert a barcode in a single statement string value = "https://ironsoftware.com/csharp/barcode"; // Create PDF417 barcode with chained operations AnyBitmap barcodeBitmap = BarcodeWriter .CreateBarcode(value, BarcodeWriterEncoding.PDF417) // Create PDF417 barcode .ResizeTo(300, 200) // Set specific dimensions .SetMargins(10) // Add 10px margins .ToBitmap(); // Convert to bitmap // Convert to System.Drawing.Bitmap for legacy compatibility System.Drawing.Bitmap legacyBitmap = barcodeBitmap; using IronBarCode; using IronSoftware.Drawing; // Generate, style, and convert a barcode in a single statement string value = "https://ironsoftware.com/csharp/barcode"; // Create PDF417 barcode with chained operations AnyBitmap barcodeBitmap = BarcodeWriter .CreateBarcode(value, BarcodeWriterEncoding.PDF417) // Create PDF417 barcode .ResizeTo(300, 200) // Set specific dimensions .SetMargins(10) // Add 10px margins .ToBitmap(); // Convert to bitmap // Convert to System.Drawing.Bitmap for legacy compatibility System.Drawing.Bitmap legacyBitmap = barcodeBitmap; Imports IronBarCode Imports IronSoftware.Drawing ' Generate, style, and convert a barcode in a single statement Private value As String = "https://ironsoftware.com/csharp/barcode" ' Create PDF417 barcode with chained operations Private barcodeBitmap As AnyBitmap = BarcodeWriter.CreateBarcode(value, BarcodeWriterEncoding.PDF417).ResizeTo(300, 200).SetMargins(10).ToBitmap() ' Convert to bitmap ' Convert to System.Drawing.Bitmap for legacy compatibility Private legacyBitmap As System.Drawing.Bitmap = barcodeBitmap $vbLabelText $csharpLabel 流暢式 API 模式具有以下幾個優點: -可讀性:將操作以邏輯順序串聯起來,使其讀起來像自然語言。 -效率:減少變數宣告與中間步驟 -靈活性:無需重構程式碼即可輕鬆新增或刪除操作 常見的流暢操作包括: ResizeTo() :精確控制條碼尺寸 SetMargins() :增加一致的間距 ChangeBarCodeColor() :修改外觀 AddAnnotationTextAboveBarcode() :新增描述性文字 ToBitmap() 、 SaveAsPng() 、 SaveAsPdf() :匯出為多種格式 A PDF417 barcode generated using fluent method chaining IronBarcode支援哪些條碼格式? IronBarcode 透過BarcodeWriterEncoding枚舉支援全面的條碼格式產生。 支援的格式包括 一維條碼:Code128、Code39、Code93、Codabar、ITF、MSI、Plessey、UPCA、UPCE、EAN8、EAN13 二維條碼:QR碼、DataMatrix碼、PDF417碼、Aztec碼、MaxiCode碼 特殊格式:IntelligentMail、DataBar、DataBarExpanded 和各種 GS1 標準 每種格式都有其特定的特點和應用場景。 例如,二維碼擅長儲存網址和大量數據,而 EAN13 是零售產品的標準。 了解更多關於如何為您的應用程式選擇合適的條碼格式的資訊。 如何驗證我產生的條碼是否可讀? 品質保證對於條碼的實施至關重要。 IronBarcode 內建驗證功能,可確保您產生的條碼始終可掃描: // Generate and verify a barcode GeneratedBarcode myBarcode = BarcodeWriter .CreateBarcode("TEST123", BarcodeWriterEncoding.Code128) .ResizeTo(200, 100) .ChangeBarCodeColor(Color.DarkBlue); // Verify the barcode is still readable after modifications bool isReadable = myBarcode.Verify(); Console.WriteLine($"Barcode verification: {(isReadable ? "PASS" : "FAIL")}"); // Generate and verify a barcode GeneratedBarcode myBarcode = BarcodeWriter .CreateBarcode("TEST123", BarcodeWriterEncoding.Code128) .ResizeTo(200, 100) .ChangeBarCodeColor(Color.DarkBlue); // Verify the barcode is still readable after modifications bool isReadable = myBarcode.Verify(); Console.WriteLine($"Barcode verification: {(isReadable ? "PASS" : "FAIL")}"); ' Generate and verify a barcode Dim myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("TEST123", BarcodeWriterEncoding.Code128).ResizeTo(200, 100).ChangeBarCodeColor(Color.DarkBlue) ' Verify the barcode is still readable after modifications Dim isReadable As Boolean = myBarcode.Verify() Console.WriteLine($"Barcode verification: {(If(isReadable, "PASS", "FAIL"))}") $vbLabelText $csharpLabel Verify()方法檢查條碼在套用調整大小或重新著色等轉換後是否仍可讀取機器。 當使用非標準顏色或非常小的尺寸時,這一點尤其重要。 哪裡可以找到更多條碼生成範例? 若要擴充條碼產生功能,請探索以下其他資源: 原始碼和範例 下載本教學的完整原始碼: GitHub 倉庫 C# 原始碼 ZIP 高級主題 -產生帶有徽標的二維碼- 為您的二維碼添加品牌標識 條碼樣式指南- 掌握進階自訂技巧 -從影像中讀取條碼- 透過條碼掃描完成循環 -批次產生條碼- 高效率產生多個條碼 API 文件 BarcodeWriter器類別參考- 完整的方法文檔 GeneratedBarcode類別參考- 所有自訂選項 BarcodeWriterEncoding枚舉- 支援的條碼格式 準備好在您的應用程式中產生專業條碼了嗎? IronBarcode 讓條碼產生變得簡單,同時又能提供專業應用所需的彈性。 無論您需要簡單的產品程式碼還是具有自訂樣式的複雜二維條碼,IronBarcode 都能用最少的程式碼輕鬆處理。 立即下載 IronBarcode ,幾分鐘內即可開始產生條碼。 需要協助選擇合適的授權嗎? 查看我們的授權選項或申請免費試用金鑰,以便在您的生產環境中測試 IronBarcode。 常見問題解答 如何在 C# 中創建條碼圖片? 要在 C# 中創建條碼圖片,你可以使用 IronBarcode 的 BarcodeWriter.CreateBarcode() 方法。這允許您指定數據和條碼格式,然後使用 SaveAsPng() 等方法保存為 PNG 或 JPEG 格式的圖片。 在 .NET 項目中安裝 IronBarcode 的步驟有哪些? 您可以通過在 Visual Studio 中使用 NuGet 套件管理器在 .NET 項目中安裝 IronBarcode。或者,您可以從 IronBarcode 網站下載 DLL 並將其添加到項目引用中。 如何在 C# 中將條碼導出為 PDF? IronBarcode 允許使用 SaveAsPdf() 方法從 GeneratedBarcode 類導出條碼為 PDF,提供了一種簡單的方法以 PDF 格式保存條碼。 在 C# 中條碼有哪些自定義選項? IronBarcode 提供廣泛的自定義選項,例如使用 ChangeBarCodeColor() 更改條碼顏色,使用 AddAnnotationTextAboveBarcode() 添加文字註釋,以及使用 SetMargins() 設置邊距。 如何快速在一行代碼中創建和樣式化條碼? 使用 IronBarcode 的流暢 API,您可以通過方法鏈在一行中創建和樣式化條碼:BarcodeWriter.CreateBarcode(data, encoding).ResizeTo(300, 200).SetMargins(10).SaveAsPng(path)。 如何確保在修改後我的條碼可掃描? 在對條碼進行樣式或調整大小後,使用 GeneratedBarcode 對象的 Verify() 方法來驗證其機器可讀性。 我可以在 C# 中生成帶有標誌的 QR 碼嗎? 可以,IronBarcode 支持使用 QRCodeWriter 類生成帶有嵌入標誌的 QR 碼,其中包括標誌插入和增強的錯誤更正級別功能。 在 C# 中高效生成多個條碼的過程是什麼? 您可以使用 IronBarcode 高效生成多個條碼,這支持批量處理並允許使用循環或並行處理來處理高量條碼生成。 在 C# 中我可以使用什麼文件格式導出條碼? IronBarcode 支持以多種格式導出條碼,包括 PNG、JPEG、GIF、TIFF、BMP、PDF 和 HTML,為不同的應用需求提供靈活性。 如何在 C# 中在條碼下添加人類可讀文本? 要在 C# 中在條碼下添加人類可讀文本,使用 AddBarcodeValueTextBelowBarcode() 方法,它會自動在條碼圖片下方顯示編碼值的文本格式。 Jacob Mellor 立即與工程團隊聊天 首席技术官 Jacob Mellor 是 Iron Software 的首席技術官,作為 C# PDF 技術的先鋒工程師。作為 Iron Software 核心代碼的原作者,他自開始以來塑造了公司產品架構,與 CEO Cameron Rimington 一起將其轉變為一家擁有超過 50 名員工的公司,為 NASA、特斯拉 和 全世界政府機構服務。Jacob 持有曼徹斯特大學土木工程一級榮譽学士工程學位(BEng) (1998-2001)。他於 1999 年在倫敦開設了他的第一家軟件公司,並於 2005 年製作了他的首個 .NET 組件,專注於解決 Microsoft 生態系統內的複雜問題。他的旗艦產品 IronPDF & Iron Suite .NET 庫在全球 NuGet 被安裝超過 3000 萬次,其基礎代碼繼續為世界各地的開發工具提供動力。擁有 25 年的商業經驗和 41 年的編碼專業知識,Jacob 仍專注於推動企業級 C#、Java 及 Python PDF 技術的創新,同時指導新一代技術領袖。 準備好開始了嗎? Nuget 下載 1,979,979 | Version: 2025.11 剛發表 免費下載 NuGet 下載總數:1,979,979 檢視授權