IronBarcode 教程 在 C# .NET 中生成条形码图像 How to Generate Barcode Images in C# .NET Applications Jacob Mellor 已更新:八月 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 Need to quickly generate professional barcode images in your .NET applications? This tutorial shows you exactly how to create, customize, and export barcodes using IronBarcode - from simple one-line implementations to advanced styling techniques that give you complete control over your barcode appearance. Quickstart: Create and Save a Barcode Image Instantly With IronBarcode you can generate and export a barcode image in just one simple call. Use the CreateBarcode method with your text, choose the format and size, then call SaveAsPng — no complex setup needed. 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 Minimal Workflow (5 steps) Install IronBarcode via NuGet Package Manager Generate a Simple Barcode with One Line of Code Apply Custom Styling and Annotations to Your Barcode Export Barcodes as Images, PDFs, or HTML Use Fluent API for Efficient Barcode Generation How Do I Install a Barcode Generator Library in C#? Installing IronBarcode takes just seconds using the NuGet Package Manager. You can install it directly through the Package Manager Console or download the DLL manually. Install-Package BarCode IronBarcode provides comprehensive barcode generation capabilities for .NET developers How Can I Generate a Simple Barcode Using C#? Creating your first barcode requires just two lines of code. The example below demonstrates generating a standard Code128 barcode and saving it as an image file. 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 The BarcodeWriter.CreateBarcode() method is your entry point for barcode generation. It accepts two parameters: the data you want to encode and the barcode format from the BarcodeWriterEncoding enum. IronBarcode supports all major barcode formats including Code128, Code39, EAN13, UPC-A, PDF417, DataMatrix, and QR codes. Once generated, the GeneratedBarcode object provides multiple export options. You can save it as various image formats (PNG, JPEG, GIF, TIFF), export to PDF, or even retrieve it as a System.Drawing.Bitmap for further processing in your application. A Code128 barcode generated with IronBarcode displaying a URL Can I Customize the Appearance of Generated Barcodes? IronBarcode offers extensive customization options that go far beyond basic barcode generation. You can add annotations, adjust colors, set margins, and control every aspect of your barcode's appearance. 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 The GeneratedBarcode class provides a rich set of methods for customization: Annotations: Use AddAnnotationTextAboveBarcode() and AddAnnotationTextBelowBarcode() to add custom labels or instructions around your barcode Value Display: The AddBarcodeValueTextBelowBarcode() method automatically displays the encoded data in human-readable format Spacing: Control whitespace with SetMargins() to ensure proper scanning and visual appeal Colors: Change foreground and background colors using ChangeBarCodeColor() and ChangeBackgroundColor() Export Options: Save as image files, PDFs, or self-contained HTML documents A styled QR code featuring custom color and annotation text For detailed customization options, explore the GeneratedBarcode class documentation which covers all available styling methods and properties. How Do I Create and Export a Barcode in One Line of Code? IronBarcode implements a fluent API design pattern that enables method chaining for more concise and readable code. This approach is particularly useful when applying multiple transformations to your barcode. 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 The fluent API pattern offers several advantages: Readability: Chain operations in a logical sequence that reads like natural language Efficiency: Reduce variable declarations and intermediate steps Flexibility: Easily add or remove operations without restructuring your code Common fluent operations include: ResizeTo(): Control exact barcode dimensions SetMargins(): Add consistent spacing ChangeBarCodeColor(): Modify appearance AddAnnotationTextAboveBarcode(): Include descriptive text ToBitmap(), SaveAsPng(), SaveAsPdf(): Export in various formats A PDF417 barcode generated using fluent method chaining What Barcode Formats Are Supported by IronBarcode? IronBarcode supports comprehensive barcode format generation through the BarcodeWriterEncoding enum. Supported formats include: 1D Barcodes: Code128, Code39, Code93, Codabar, ITF, MSI, Plessey, UPCA, UPCE, EAN8, EAN13 2D Barcodes: QRCode, DataMatrix, PDF417, Aztec, MaxiCode Specialized Formats: IntelligentMail, DataBar, DataBarExpanded, and various GS1 standards Each format has specific characteristics and use cases. For example, QR codes excel at storing URLs and large amounts of data, while EAN13 is standard for retail products. Learn more about choosing the right barcode format for your application. How Can I Verify My Generated Barcode Is Readable? Quality assurance is critical for barcode implementation. IronBarcode includes built-in verification to ensure your generated barcodes remain scannable: // 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 The Verify() method checks whether your barcode remains machine-readable after applying transformations like resizing or recoloring. This is especially important when using non-standard colors or very small sizes. Where Can I Find More Barcode Generation Examples? To expand your barcode generation capabilities, explore these additional resources: Source Code and Examples Download the complete source code for this tutorial: GitHub Repository C# Source Code ZIP Advanced Topics Generate QR Codes with Logos - Add branding to your QR codes Barcode Styling Guide - Master advanced customization techniques Reading Barcodes from Images - Complete the cycle with barcode scanning Batch Barcode Generation - Generate multiple barcodes efficiently API Documentation BarcodeWriter Class Reference - Complete method documentation GeneratedBarcode Class Reference - All customization options BarcodeWriterEncoding Enum - Supported barcode formats Ready to Generate Professional Barcodes in Your Application? IronBarcode makes barcode generation straightforward while providing the flexibility needed for professional applications. Whether you need simple product codes or complex 2D barcodes with custom styling, IronBarcode handles it all with minimal code. Download IronBarcode today and start generating barcodes in minutes. Need help choosing the right license? Check our licensing options or request a free trial key 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 允许使用 GeneratedBarcode 类中的 SaveAsPdf() 方法将条形码导出为 PDF,这是一种以 PDF 格式保存条形码的简便方法。 在 C# 中,条形码有哪些自定义选项? IronBarcode 提供广泛的自定义选项,例如使用 ChangeBarCodeColor() 更改条形码颜色、使用 AddAnnotationTextAboveBarcode() 添加文本注释,以及使用 SetMargins() 设置边距。 我如何快速在一行代码中创建和样式化条形码? 使用 IronBarcode 的流畅 API,您可以通过方法链在一行代码中创建和样式化条形码:BarcodeWriter.CreateBarcode(data, encoding).ResizeTo(300, 200).SetMargins(10).SaveAsPng(path). 我如何确保修改后的条形码可扫描? 要验证样式或调整大小后的条形码的可扫描性,使用 GeneratedBarcode 对象上的 Verify() 方法以检查其机器可读性。 我可以在 C# 中生成带有徽标的二维码吗? 是的,IronBarcode 支持使用 QRCodeWriter 类生成带有嵌入徽标的二维码,该类包含徽标插入和增强错误更正级别的功能。 在 C# 中高效生成多个条形码的过程是什么? 您可以使用 IronBarcode 高效地在 C# 中生成多个条形码,该工具支持批处理,并允许使用循环或并行处理来处理大批量条形码生成。 在 C# 中,我可以使用哪些文件格式导出条形码? IronBarcode 支持以多种格式导出条形码,包括 PNG、JPEG、GIF、TIFF、BMP、PDF 和 HTML,为不同的应用程序需求提供了灵活性。 我如何在 C# 中在条形码下方添加人类可读的文本? 要在 C# 中在条形码下方添加人类可读的文本,使用 AddBarcodeValueTextBelowBarcode() 方法,该方法会自动显示编码值为文本格式并置于条形码图像下方。 Jacob Mellor 立即与工程团队聊天 首席技术官 Jacob Mellor 是 Iron Software 的首席技术官,是 C# PDF 技术的先锋工程师。作为 Iron Software 核心代码库的原始开发者,自公司成立以来,他就塑造了公司的产品架构,并与首席执行官 Cameron Rimington 一起将其转变成一家公司,拥有50多人,服务于 NASA、特斯拉和全球政府机构。Jacob 拥有曼彻斯特大学 (1998-2001) 的一级荣誉土木工程学士学位。1999 年在伦敦创办了自己的第一家软件公司,并于 2005 年创建了他的第一个 .NET 组件后,他专注于解决微软生态系统中的复杂问题。他的旗舰 IronPDF 和 IronSuite .NET 库在全球已获得超过 3000 万次的 NuGet 安装,其基础代码继续为全球使用的开发者工具提供支持。拥有 25 年商业经验和 41 年编程经验的 Jacob 仍专注于推动企业级 C#、Java 和 Python PDF 技术的创新,同时指导下一代技术领导者。 准备开始了吗? Nuget 下载 1,935,276 | 版本: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:1,935,276 查看许可证