IronBarcode 教程 在 C# .NET 中生成條碼圖片 How to Generate Barcode Images in C# .NET Applications Jacob Mellor 更新日期:8月 20, 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 This article was translated from English: Does it need improvement? Translated View the article in English 需要在您的.NET應用程序中快速生成專業的條碼圖像嗎? 本教程精確地展示了如何使用IronBarcode創建、自定義和導出條碼,從簡單的一行實現到高級樣式技術,讓您完全掌控條碼外觀。 作為標題:2(快速入門:立即創建並保存條形碼圖像) 使用IronBarcode,您只需一個簡單的調用即可生成並導出條碼圖像。 使用CreateBarcode方法與您的文本,選擇格式和大小,然後調用SaveAsPng — 不需要複雜的設置。 Get started making PDFs with NuGet now: Install IronBarcode with NuGet Package Manager PM > Install-Package BarCode Copy and run this code snippet. IronBarCode.BarcodeWriter.CreateBarcode("Hello123", BarcodeWriterEncoding.Code128, 200, 100).SaveAsPng("barcode.png"); Deploy to test on your live environment Start using IronBarcode in your project today with a free trial Free 30 day Trial class="hsg-featured-snippet"> 最小化工作流程(5步) 通過NuGet包管理器安裝IronBarcode 用一行代碼生成簡單條碼 在條碼上應用自定義樣式和註釋 將條碼導出為圖像、PDF或HTML 使用流暢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檢索以便在您的應用程式中進行進一步處理。 使用IronBarcode生成的Code128條碼顯示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設計模式,能夠進行方法鏈接以獲得更簡潔且易讀的代碼。 此方法尤其在對您的條碼應用多個變換時特別有用。 流暢API模式提供了幾個優勢: ```csharp 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; ``` - **可讀性**:在像自然語言一樣的邏輯序列中鏈接操作 - **效率**:減少變量聲明和中間步驟 - **靈活性**:輕鬆添加或刪除操作而無需重構代碼 常見的流暢操作包括: - `ResizeTo()`:控制精確的條碼尺寸 - `SetMargins()`:添加一致的間距 - `ChangeBarCodeColor()`:修改外觀 - `AddAnnotationTextAboveBarcode()`:添加描述性文本 - `ToBitmap()`, `SaveAsPng()`, `SaveAsPdf()`:以各種格式導出  IronBarcode支持哪些條碼格式? IronBarcode通過`BarcodeWriterEncoding`枚舉支持全面的條碼格式生成。 支持的格式包括: **1D條碼**: Code128、Code39、Code93、Codabar、ITF、MSI、Plessey、UPCA、UPCE、EAN8、EAN13 **2D條碼**: QRCode、DataMatrix、PDF417、Aztec、MaxiCode **專門化格式**: IntelligentMail、DataBar、DataBar擴展和各種GS1標準 每種格式都有其特定的特點和用例。 例如,QR碼在存儲URL和大量數據方面表現優異,而EAN13是零售產品的標準。 了解有關[選擇正確條碼格式](/csharp/barcode/get-started/supported-barcode-formats/)的更多信息。 ## 如何驗證我生成的條碼是可讀的? 對於條碼實現來說,質量保證是至關重要的。 IronBarcode包括內置的驗證功能以確保生成的條碼保持可掃描: `Verify()`方法檢查您的條碼在應用變換(如調整大小或重新顏色)後是否仍然機器可讀。 ```csharp // 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")}"); ``` 這在使用非標準顏色或非常小尺寸時尤為重要。 ## 我可以在哪裡找到更多的條碼生成範例? 要擴展您的條碼生成能力,請探索這些額外的資源: ### 源代碼和示例 下載本教程的完整源代碼: - [C#源代碼ZIP](/downloads/assets/tutorials/csharp-barcode-image-generator/Iron-Barcode-CSharp-Barcode-Image-Generator-Tutorial.zip) - [C#源代碼ZIP](/downloads/assets/tutorials/csharp-barcode-image-generator/Iron-Barcode-CSharp-Barcode-Image-Generator-Tutorial.zip) ### 高級主題 - [生成帶有標誌的QR碼](/csharp/barcode/examples/csharp-create-qr-code/) - 為您的QR碼添加品牌元素 - [條碼樣式指南](/csharp/barcode/how-to/customize-barcode-style/) - 掌握高級自定義技術 - [從圖像讀取條碼](/csharp/barcode/how-to/read-barcodes-from-images/) - 完成帶有條碼掃描的週期 - [批量條碼生成](/csharp/barcode/how-to/create-barcode-images/) - 高效生成多個條碼 ### API文檔 - [`BarcodeWriter`類參考](/csharp/barcode/object-reference/api/IronBarCode.BarcodeWriter.html) - 完整的方法文檔 - [`GeneratedBarcode`類參考](/csharp/barcode/object-reference/api/IronBarCode.GeneratedBarcode.html) - 所有自定義選項 - [`BarcodeWriterEncoding`枚舉](/csharp/barcode/object-reference/api/IronBarCode.BarcodeWriterEncoding.html) - 支持的條碼格式 ## 準備好在您的應用程式中生成專業條碼了嗎? IronBarcode使條碼生成變得簡便,同時提供了專業應用所需的靈活性。 無論您需要簡單的產品代碼還是具有自定義樣式的複雜2D條碼,IronBarcode都能以最少的代碼處理一切。 [立即下載IronBarcode](download-modal)並在幾分鐘內開始生成條碼。 需要幫助選擇合適的許可證嗎? 查看我們的[申請免費試用密鑰](trial-license)以在您的生產環境中測試IronBarcode。 Check our [licensing options](/csharp/barcode/licensing/) or [request a free trial key](trial-license) to test IronBarcode in your production environment. 常見問題解答 如何在 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 & IronSuite .NET 庫在全球 NuGet 被安裝超過 3000 萬次,其基礎代碼繼續為世界各地的開發工具提供動力。擁有 25 年的商業經驗和 41 年的編碼專業知識,Jacob 仍專注於推動企業級 C#、Java 及 Python PDF 技術的創新,同時指導新一代技術領袖。 準備好開始了嗎? Nuget 下載 1,935,276 | 版本: 2025.11 剛剛發布 免費 NuGet 下載 總下載量:1,935,276 查看許可證