IRONSOFTWAREHOME

使用 IronBarcode 在 C## 适用于 .NET 中自定义和风格化条形码

Hairil Hasyimi Bin Omar
Hairil Hasyimi Bin Omar
Updated: 2026年6月29日

IronBarcode让开发人员通过更改颜色、调整尺寸和添加注释自定义C#中的条形码,简单方法调用如ResizeTo()提供完整的样式控制。

多年来,条形码的使用越来越普及,无论是存储数据、ID 还是网页 URL,都被广泛应用。 在某些应用中,产品上的条形码是可见的,因此对样式选项的需求也随之增加。 因此,某些条形码类型发展出独特的外观,如ResizeTo,以及许多其他。 有关支持格式的完整列表,请参阅我们的 Supported BarCode Formats 文档。

此外,IronBarcode 还为用户提供了进一步风格化条形码的选项,包括条形码颜色条形码大小调整背景颜色等方面。 这得益于我们开源库IronDrawing的帮助。 这些样式功能建立在 IronBarcode 的 综合条码生成功能之上。

快速入门:自定义条码颜色和背景

下面是一个简单的示例,展示了开发人员如何使用 IronBarcode 对条形码的条形和背景快速应用自定义颜色。 您将看到,只需一次链式调用,就能轻松生成样式化的 BarCode。 有关更多高级示例,请查看我们的 C# BarCode 图像生成器教程

  1. 1Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. 2复制并运行这段代码。

    IronBarCode.BarcodeWriter.CreateBarcode("HELLO123", IronBarCode.BarcodeEncoding.Code128)
        .ChangeBarCodeColor(IronSoftware.Drawing.Color.Blue)
        .ChangeBackgroundColor(IronSoftware.Drawing.Color.White)
        .SaveAsImage("styled.png");
    C#
  3. 3部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronBarcode
    arrow pointer

如何调整 BarCode 的大小?

何时应使用 ResizeTo 方法?

调整条形码大小是用户可以使用IronBarcode实现的一个自定义方面。 要使用此功能,只需调用ResizeTo方法并输入条形码的新宽度和高度测量(以像素为单位)。 此操作会触发对 BarCode 的无损重新渲染。 这种方法既能保持条形码的质量,又能调整其尺寸,非常适合需要将条形码纳入特定布局或打印尺寸的场景。

请注意: 条码不可读的过小值将被忽略。
using IronBarCode;

public class BarcodeResizer
{
    public static void ResizeBarcode(string barcodeText, int newWidth, int newHeight)
    {
        // Generate a barcode
        BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128)
                     // Resize the barcode
                     .ResizeTo(newWidth, newHeight)
                     // Save the resized barcode
                     .SaveAsImage("resized_barcode.png");
    }
    
    // Example usage with different size requirements
    public static void ResizeForDifferentFormats()
    {
        var barcode = BarcodeWriter.CreateBarcode("PRODUCT-12345", BarcodeEncoding.Code128);
        
        // Resize for product label
        barcode.ResizeTo(200, 50).SaveAsImage("product_label.png");
        
        // Resize for shipping label
        barcode.ResizeTo(300, 75).SaveAsImage("shipping_label.png");
        
        // Resize for inventory tag
        barcode.ResizeTo(150, 40).SaveAsImage("inventory_tag.png");
    }
}

GeneratedBarcode上调用。 在处理不同的输出格式时,您可能还想了解一下我们的 Create BarCode as PDF 指南。以下是运行上述代码片段生成的 BarCode 图像。

调整大小操作之前具有标准尺寸的原始条形码
调整大小后的条形码,显示尺寸修改后清晰的黑白垂直条纹

为什么对一维 BarCode 使用 ResizeToMil 方法?

IronBarcode可用的另一种调整大小方面是ResizeToMil方法。 与ResizeTo方法不同,此方法调整以下组件:

  • **条形码元素:**狭窄条形码元素的宽度,以千分之一英寸(mil)为单位。
  • **高度:**条形码的高度,以英寸为单位(默认值为1英寸)。
  • **分辨率:**每英寸点数(默认值为96 DPI)。

这种方法尤其适用于一维 BarCode,常用于对精确测量要求极高的工业应用领域。 密尔测量系统是一项行业标准,可确保不同扫描仪和打印条件下条形码的可读性保持一致。

using IronBarCode;

public class BarcodeResizer
{
    public static void ResizeBarcodeToMil(string barcodeText, int elementWidthMil, int heightInches, int dpi = 96)
    {
        // Generate a barcode
        BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128)
                     // Resize the barcode to mil
                     .ResizeToMil(elementWidthMil, heightInches, dpi)
                     // Save the resized barcode
                     .SaveAsImage("resized_barcode_mil.png");
    }
    
    // Example for different industrial standards
    public static void CreateIndustrialBarcodes()
    {
        // Standard retail barcode (10 mil width, 1 inch height)
        BarcodeWriter.CreateBarcode("RETAIL-001", BarcodeEncoding.Code128)
                     .ResizeToMil(10, 1, 300)
                     .SaveAsImage("retail_barcode.png");
        
        // High-density warehouse barcode (5 mil width, 0.5 inch height)
        BarcodeWriter.CreateBarcode("WAREHOUSE-002", BarcodeEncoding.Code128)
                     .ResizeToMil(5, 0.5f, 600)
                     .SaveAsImage("warehouse_barcode.png");
        
        // Large shipping barcode (15 mil width, 2 inch height)
        BarcodeWriter.CreateBarcode("SHIP-003", BarcodeEncoding.Code128)
                     .ResizeToMil(15, 2, 200)
                     .SaveAsImage("shipping_barcode.png");
    }
}

您也可以在GeneratedBarcode对象上调用此方法。 有关设置精确条形码尺寸的更多信息,请参考我们的设置条形码边距指南。在下图中,您将看到应用ResizeToMil方法的效果:条形码边缘的白色空格被消除,并根据提供给方法的参数值调整条形码的最窄元素和高度。

应用 ResizeToMil 方法之前具有标准尺寸的原始条形码
应用 ResizeToMil 方法后的线性条形码结果,显示黑白垂直条纹

如何更改 BarCode 和背景颜色?

条形码样式最受欢迎的功能之一是可更改条形码和背景颜色。 得益于IronDrawing,IronBarcode提供了此功能。 通过在ChangeBackgroundColor方法,用户可以更改条形码及其背景的颜色。 该功能在品牌推广或为特殊活动或产品系列创建主题条形码时特别有用。

using IronBarCode;
using IronSoftware.Drawing; // Required for color manipulation

public class BarcodeColorChanger
{
    public static void ChangeBarcodeColors(string barcodeText, Color barcodeColor, Color backgroundColor)
    {
        // Generate a barcode
        var barcode = BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128);

        // Change the barcode color
        barcode.ChangeBarCodeColor(barcodeColor);

        // Change the background color
        barcode.ChangeBackgroundColor(backgroundColor);

        // Save the colored barcode
        barcode.SaveAsImage("colored_barcode.png");
    }
    
    // Example: Create branded barcodes with company colors
    public static void CreateBrandedBarcodes()
    {
        // Company brand colors example
        var barcode = BarcodeWriter.CreateBarcode("BRAND-2024", BarcodeEncoding.Code128);
        
        // Apply brand colors
        barcode.ChangeBarCodeColor(Color.FromHex("#1E3A8A")) // Company blue
               .ChangeBackgroundColor(Color.FromHex("#F3F4F6")) // Light gray background
               .SaveAsImage("branded_barcode.png");
        
        // Create seasonal variation
        var seasonalBarcode = BarcodeWriter.CreateBarcode("HOLIDAY-2024", BarcodeEncoding.Code128);
        seasonalBarcode.ChangeBarCodeColor(Color.DarkGreen)
                       .ChangeBackgroundColor(Color.LightYellow)
                       .SaveAsImage("seasonal_barcode.png");
    }
}

在使用彩色条形码时,条形码与背景颜色之间必须保持足够的对比度,以确保可读性。 有关 QR 代码特有的更多样式选项,请参阅我们的 Customize and Style QR Codes 教程。

带有自定义绿色背景和棕褐色前景色的 QR 代码,演示条形码颜色自定义

如何为 BarCode 添加注释?

IronBarcode还提供了添加和样式化条形码注释的选项。 注释的样式也受到IronDrawing提供的功能的帮助,包括编辑注释的颜色和字体。 注释对于在机器可读条形码的同时提供人类可读信息至关重要,因此对于库存管理、产品标签和装运应用至关重要。

using IronBarCode;
using IronSoftware.Drawing; // Required for font and color manipulation

public class BarcodeAnnotator
{
    public static void AnnotateBarcode(string barcodeText, string annotationText, Font annotationFont, Color annotationColor, float annotationSpacing)
    {
        // Generate a barcode
        var barcode = BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128);

        // Add annotation above the barcode
        barcode.AddAnnotationTextAboveBarcode(annotationText, annotationFont, annotationColor, annotationSpacing);

        // Add barcode value text below the barcode
        barcode.AddBarcodeValueTextBelowBarcode(annotationFont, annotationColor, annotationSpacing);

        // Save the annotated barcode
        barcode.SaveAsImage("annotated_barcode.png");
    }
    
    // Example: Create product label with annotations
    public static void CreateProductLabel()
    {
        var productCode = "PRD-12345-XL";
        var barcode = BarcodeWriter.CreateBarcode(productCode, BarcodeEncoding.Code128);
        
        // Define fonts for different purposes
        var titleFont = new Font("Arial", FontStyle.Bold, 14);
        var valueFont = new Font("Arial", FontStyle.Regular, 12);
        
        // Add product name above
        barcode.AddAnnotationTextAboveBarcode("Premium Widget XL", titleFont, Color.Black, 5);
        
        // Add product code below
        barcode.AddBarcodeValueTextBelowBarcode(valueFont, Color.DarkGray, 3);
        
        // Apply additional styling
        barcode.ResizeTo(250, 80)
               .SaveAsImage("product_label_annotated.png");
    }
}
由 IronBarcode 生成的包含 ironsoftware.com URL 的茶色和米色 QR 代码

作为前一代码片段的扩展,我们实例化了两个新的IronSoftware.Drawing.Font对象,将作为条形码上方和下方注释的字体。 只有 Font Family 是实例化字体所必需的,不过您可以指定尺寸和样式等其他属性,以获得更多控制权。

  • AddAnnotationTextAboveBarcode:在条形码上方添加自定义注释文本。
  • AddBarcodeValueTextBelowBarcode:在条形码下方添加条形码值。

这两个方法接受相同的参数:IronSoftware.Drawing.Color对象和条形码与文本之间的间距。 此外,AddAnnotationTextAboveBarcode方法需要一个字符串以表示注释文本,因为它在条形码上方添加自定义文本。

IronBarcode 为条码样式提供了多种自定义选项。 对于需要在注释中支持 Unicode 的应用程序,请查看我们的 Writing Unicode BarCode 指南。要了解有关自定义 QR 代码的更多信息,请参阅"如何自定义并向 QR 代码添加徽标"。 如需将样式化条形码导出为不同格式,请浏览我们的 Create Barcode as HTML 教程。

常见问题解答

如何在 C# 中更改 BarCode 的颜色?

IronBarcode 提供的 ChangeBarCodeColor() 方法可轻松自定义条形码颜色。只需在创建条形码后链入该方法,即可应用 IronSoftware.Drawing.Color 调色板中的任意颜色,从而完全控制条形码的视觉外观。

我应该使用什么方法来调整 BarCode 的大小而不降低质量?

using IronBarcode 的 ResizeTo() 方法调整条形码的大小,而不会造成质量损失。该方法以指定的宽度和高度(以像素为单位)触发条形码的无损重新渲染,在保持清晰度的同时调整尺寸以适应特定的布局或打印要求。

我可以自定义 BarCode 的背景颜色吗?

是的,IronBarcode 允许您使用 ChangeBackgroundColor() 方法自定义条形码背景。该功能可让您使用 IronSoftware.Drawing.Color 调色板设置任何背景颜色,从而与您的设计要求实现无缝集成。

哪些条形码格式支持独特的样式选项?

IronBarcode 支持各种具有独特外观的条形码格式,包括 PDF417、Aztec、IntelligentMail、MaxiCode 和 DataMatrix。每种格式都有其独特的视觉特征,同时还允许通过 IronBarcode 的样式方法进行额外的定制。

如何为 BarCode 添加注释?

IronBarcode 使您能够在条形码的上方和下方添加注释,增强可读性并提供额外的上下文。该功能尤其适用于在条码旁边添加人类可读文本、产品代码或其他识别信息。

ResizeTo 和 ResizeToMil 方法有什么区别?

IronBarcode 提供两种调整大小的方法:ResizeTo()用于基于像素的大小调整和无损重新渲染,ResizeToMil()用于使用密尔测量值调整条形码元素的大小。这两种方法都能在满足不同测量要求的同时保证质量。

How does IronBarcode maintain barcode quality during resizing?

IronBarcode maintains barcode quality during resizing by triggering a lossless re-rendering process when using methods like ResizeTo(). This ensures the barcode remains high quality and readable after being resized.

Is there support for advanced color manipulation in IronBarcode?

Advanced color manipulation in IronBarcode is supported through integration with the IronDrawing library. This allows for precise control over barcode and background colors, which can be tailored to meet branding or thematic requirements.

How can IronBarcode assist with creating product labels?

IronBarcode can assist in creating product labels by allowing the addition of annotations, customizing barcode dimensions, and altering colors to match product branding. Methods like AddAnnotationTextAboveBarcode help include human-readable information on labels.

What font options are available for barcode annotations in IronBarcode?

For barcode annotations, IronBarcode provides options through IronSoftware.Drawing.Font, allowing you to customize font family, size, style, and color. This adds flexibility for styling annotations aligned with specific product or branding needs.

准备开始了吗?

Nuget Downloads 2,422,100版本:2026.9刚刚发布

免费获取

30天试用密钥 即刻获取。

bullet_checked无需信用卡或创建账户
bullet_test在生产环境中测试
且无水印
bullet_calendar30天完全
功能性产品
bullet_support试用期间提供
24/5技术支持
立即获取您的免费30 天试用密钥
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package BarCode
nuget.org/packages/BarCode/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择浏览并搜索 "IronBarcode"
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

  1. 下载并将 IronBarCode 解压到解决方案目录中的 ~/Libs 等位置
  2. 在 Visual Studio 解决方案资源管理器中,右键单击引用。选择浏览,"IronBarcode.dll"

许可 $999

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户