如何在C#中使用OcrProgress跟踪

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

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronBarcode使开发人员能够通过更改颜色、调整尺寸和添加注释来在 C# 中自定义条形码,并使用像 ChangeBarCodeColor()ResizeTo() 这样的简单方法调用来实现完全的样式控制。

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

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

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

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

  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/BarCode

    PM > Install-Package BarCode
  2. 复制并运行这段代码。

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

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

    arrow pointer

如何调整 BarCode 的大小?

何时应使用 ResizeTo 方法?

调整条形码大小是用户可以使用IronBarcode实现的一个自定义方面。 要使用此功能,只需调用 ResizeTo 方法,并以像素 (px) 为单位输入条形码的新宽度和高度测量值。 此操作会触发对 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");
    }
}
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");
    }
}
$vbLabelText   $csharpLabel

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

Original barcode with standard dimensions before resize operation
Resized barcode showing clear black and white vertical bars after dimension modification

为什么对一维 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");
    }
}
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");
    }
}
$vbLabelText   $csharpLabel

你也可以在 GeneratedBarcode 对象上调用此方法。 如需了解有关设置精确条形码尺寸的更多信息,请参阅我们的"设置条形码边距"指南。在下图中,您可以看到应用 ResizeToMil 方法的效果:条形码边缘的空白区域被消除,条形码的最窄元素和高度均根据提供给该方法的参数值进行调整。

Original barcode with standard dimensions before ResizeToMil method is applied
Linear barcode showing result after ResizeToMil method application with vertical black and white bars

如何更改 BarCode 和背景颜色?

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

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");
    }
}
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");
    }
}
$vbLabelText   $csharpLabel

在使用彩色条形码时,条形码与背景颜色之间必须保持足够的对比度,以确保可读性。 有关 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");
    }
}
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");
    }
}
$vbLabelText   $csharpLabel
由 IronBarcode 生成的包含 ironsoftware.com URL 的茶色和米色 QR 代码

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

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

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

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

常见问题解答

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

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

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

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

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

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

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

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

如何为 BarCode 添加注释?

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

ResizeTo 和 ResizeToMil 方法有什么区别?

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

Hairil Hasyimi Bin Omar
软件工程师
如所有伟大的工程师一般,Hairil 是个热心的学习者。他正在提高对 C#、Python 和 Java 的知识,并利用这些知识为 Iron Software 团队成员增值。Hairil 从马来西亚的玛拉工业大学加入 Iron Software 团队,获得化学与工艺工程学士学位。
准备开始了吗?
Nuget 下载 2,108,094 | 版本: 2026.3 刚刚发布
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package BarCode
运行示例 观看您的字符串变成 BarCode。