How to use OcrProgress Tracking in C#

Customize and Style Barcodes in C# for .NET with IronBarcode

IronBarcode enables developers to customize barcodes in C# by changing colors, resizing dimensions, and adding annotations, with simple method calls like ChangeBarCodeColor() and ResizeTo() for complete styling control.

Over the years, barcode usage has become increasingly popular and is used in a wide range of applications, whether storing data, IDs, or webpage URLs. In some applications, barcodes are made visible on products, resulting in an increased demand for styling options. Therefore, some barcode types have developed unique appearances such as PDF417, Aztec, IntelligentMail, MaxiCode, DataMatrix, and many more. For a comprehensive list of supported formats, see our Supported Barcode Formats documentation.

In addition, IronBarcode provides users with options to further style barcodes, including aspects like barcode colors, barcode resize, and background colors. This is made possible with the assistance of our open-source library, IronDrawing. These styling capabilities build upon IronBarcode's comprehensive barcode generation features.

Quickstart: Customize Barcode Color & Background

Here's a simple example showing how developers can quickly apply custom colors to a barcode's bars and background using IronBarcode. You'll see how easy it is to generate a styled barcode with just one chained call. For more advanced examples, check our C# Barcode Image Generator tutorial.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. Copy and run this code snippet.

    IronBarCode.BarcodeWriter.CreateBarcode("HELLO123", IronBarCode.BarcodeEncoding.Code128)
        .ChangeBarCodeColor(IronSoftware.Drawing.Color.Blue)
        .ChangeBackgroundColor(IronSoftware.Drawing.Color.White)
        .SaveAsImage("styled.png");
  3. Deploy to test on your live environment

    Start using IronBarcode in your project today with a free trial
    arrow pointer

How Do I Resize a Barcode?

When Should I Use the ResizeTo Method?

Resizing a barcode is one aspect of customization that users can achieve with IronBarcode. To use this feature, simply call the ResizeTo method and input the new width and height measurements in pixels (px) for the barcode. This action triggers a lossless re-rendering of the barcode. This method maintains the barcode's quality while adjusting its dimensions, making it perfect for scenarios where you need to fit barcodes into specific layouts or print sizes.

Please noteValues that are too small for the barcode to be readable will be ignored.

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");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

The ResizeTo method can be invoked on the GeneratedBarcode object. When working with different output formats, you might also want to explore our Create Barcode as PDF guide. Below are the barcode images generated by running the code snippet above.

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

Why Use ResizeToMil Method for 1D Barcodes?

Another aspect of resizing available in IronBarcode is the ResizeToMil method. Unlike the ResizeTo method, this one adjusts the following components:

  • Barcode element: The width of the narrowest barcode element, measured in thousandths of an inch (mil).
  • Height: The height of the barcode, measured in inches (the default is 1 inch).
  • Resolution: Dots per inch (the default is 96 DPI).

This method is particularly suitable for 1D barcodes and is commonly used in industrial applications where precise measurements are critical. The mil measurement system is an industry standard that ensures consistent barcode readability across different scanners and printing conditions.

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");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

You can also call this method on the GeneratedBarcode object. For more information on setting precise barcode dimensions, refer to our Set Barcodes Margins guide. In the image below, you'll see the effects of applying the ResizeToMil method: the white spaces at the edges of the barcode are eliminated, and both the narrowest element and the height of the barcode are adjusted according to the parameter values provided to the method.

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

How Can I Change Barcode and Background Colors?

One of the most sought-after features for styling barcodes is the ability to change both the barcode and background colors. Thanks to IronDrawing, IronBarcode provides this capability. By using both the ChangeBarCodeColor and ChangeBackgroundColor methods on the GeneratedBarcode object, users can alter the colors of the barcode and its background. This feature is particularly useful for branding purposes or when creating themed barcodes for special events or product lines.

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");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

When working with colored barcodes, it's important to maintain sufficient contrast between the barcode and background colors to ensure readability. For more styling options specific to QR codes, see our Customize and Style QR Codes tutorial.

QR code with custom green background and tan foreground colors demonstrating barcode color customization

How Do I Add Annotations to a Barcode?

IronBarcode also provides the option to add and style barcode annotations. The styling for annotations is also assisted by the functionality from IronDrawing, including editing the annotation color and fonts. Annotations are essential for providing human-readable information alongside the machine-readable barcode, making them crucial for inventory management, product labeling, and shipping applications.

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");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel
Teal and beige QR code generated by IronBarcode containing ironsoftware.com URL

As an extension of the previous code snippet, we instantiate two new IronSoftware.Drawing.Font objects to serve as fonts for annotations both above and below the barcode. Only the Font Family is required to instantiate the font, though you can specify additional properties like size and style for more control.

  • AddAnnotationTextAboveBarcode: Adds custom annotation text above the barcode.
  • AddBarcodeValueTextBelowBarcode: Adds the barcode value below the barcode.

These two methods accept the same parameters: the IronSoftware.Drawing.Font objects, an IronSoftware.Drawing.Color object, and the amount of spacing between the barcode and the text. Additionally, the AddAnnotationTextAboveBarcode method requires a string for the annotation text, as it adds custom text above the barcode.

IronBarcode offers a wide range of customization options for styling barcodes. For applications requiring Unicode support in annotations, check our Writing Unicode Barcodes guide. To learn more about customizing QR codes, refer to "How to Customize and Add Logos to QR Codes". For exporting styled barcodes to different formats, explore our Create Barcode as HTML tutorial.

Frequently Asked Questions

How can I change the color of a barcode in C#?

IronBarcode provides the ChangeBarCodeColor() method to easily customize barcode colors. Simply chain this method after creating your barcode to apply any color from the IronSoftware.Drawing.Color palette, allowing complete control over the visual appearance of your barcodes.

What method should I use to resize a barcode without losing quality?

Use IronBarcode's ResizeTo() method to resize barcodes without quality loss. This method triggers a lossless re-rendering of the barcode at the specified width and height in pixels, maintaining clarity while adjusting dimensions to fit specific layouts or print requirements.

Can I customize the background color of my barcodes?

Yes, IronBarcode allows you to customize barcode backgrounds using the ChangeBackgroundColor() method. This feature lets you set any background color using the IronSoftware.Drawing.Color palette, enabling seamless integration with your design requirements.

Which barcode formats support unique styling options?

IronBarcode supports various barcode formats with unique appearances including PDF417, Aztec, IntelligentMail, MaxiCode, and DataMatrix. Each format has its distinct visual characteristics while still allowing additional customization through IronBarcode's styling methods.

How do I add annotations to barcodes?

IronBarcode enables you to add annotations above and below barcodes, enhancing readability and providing additional context. This feature is particularly useful for adding human-readable text, product codes, or other identifying information alongside the barcode.

What's the difference between ResizeTo and ResizeToMil methods?

IronBarcode offers two resizing methods: ResizeTo() for pixel-based resizing with lossless re-rendering, and ResizeToMil() for resizing the barcode element using mil measurements. Both methods maintain quality while serving different measurement requirements.

Hairil Hasyimi Bin Omar
Software Engineer
Like all great engineers, Hairil is an avid learner. He’s refining his knowledge of C#, Python, and Java, using that knowledge to add value to team members across Iron Software. Hairil joined the Iron Software team from Universiti Teknologi MARA in Malaysia, where he graduated with a Bachelor's degree ...
Read More
Ready to Get Started?
Nuget Downloads 2,002,059 | Version: 2025.12 just released