How to Customize and Style Barcodes

Over the years, barcode usage has been increasingly popular and is used in a wide range of applications, be it to store data, ID, or URL of a webpage. In some applications, barcodes are made visible on products, resulting in an increased demand for options to style barcodes. Therefore, some barcode types/encodings have come up with their unique appearance such as PDF417, Aztec, IntelligentMail, MaxiCode, DataMatrix, and many more.

In addition to that, IronBarcode provides users with options to further style the barcodes, including aspects like barcode colors, barcode resize, and background colors. This is made possible with the assistance of our open-source library, IronDrawing.

Get started with IronBarcode

Start using IronBarcode in your project today with a free trial.

First Step:
green arrow pointer


Resize Barcode Example

Use 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 will trigger a lossless re-rendering of the barcode.

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");
    }
}
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");
    }
}
Imports IronBarCode

Public Class BarcodeResizer
	Public Shared Sub ResizeBarcode(ByVal barcodeText As String, ByVal newWidth As Integer, ByVal newHeight As Integer)
		' Generate a barcode
		BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128).ResizeTo(newWidth, newHeight).SaveAsImage("resized_barcode.png")
	End Sub
End Class
$vbLabelText   $csharpLabel

The ResizeTo method can be invoked on the GeneratedBarcode object. Below are the barcode images generated by running the code snippet above.

Barcode before resize
Barcode after resize

Use ResizeToMil Method

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.

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");
    }
}
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");
    }
}
Imports IronBarCode

Public Class BarcodeResizer
	Public Shared Sub ResizeBarcodeToMil(ByVal barcodeText As String, ByVal elementWidthMil As Integer, ByVal heightInches As Integer, Optional ByVal dpi As Integer = 96)
		' Generate a barcode
		BarcodeWriter.CreateBarcode(barcodeText, BarcodeEncoding.Code128).ResizeToMil(elementWidthMil, heightInches, dpi).SaveAsImage("resized_barcode_mil.png")
	End Sub
End Class
$vbLabelText   $csharpLabel

You can also call this method on the GeneratedBarcode object. 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.

Barcode before ResizeToMil
Barcode after ResizeToMil

Change Barcode and Background Color

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. Below is a simple code snippet demonstrating how to achieve this.

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");
    }
}
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");
    }
}
Imports IronBarCode
Imports IronSoftware.Drawing ' Required for color manipulation

Public Class BarcodeColorChanger
	Public Shared Sub ChangeBarcodeColors(ByVal barcodeText As String, ByVal barcodeColor As Color, ByVal backgroundColor As Color)
		' Generate a barcode
		Dim 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")
	End Sub
End Class
$vbLabelText   $csharpLabel
Barcode with color

Add Barcode Annotation Example

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.

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");
    }
}
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");
    }
}
Imports IronBarCode
Imports IronSoftware.Drawing ' Required for font and color manipulation

Public Class BarcodeAnnotator
	Public Shared Sub AnnotateBarcode(ByVal barcodeText As String, ByVal annotationText As String, ByVal annotationFont As Font, ByVal annotationColor As Color, ByVal annotationSpacing As Single)
		' Generate a barcode
		Dim 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")
	End Sub
End Class
$vbLabelText   $csharpLabel
Colored barcode with annotations

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.

  • 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 opportunities for users to customize and style their barcodes, limited only by one's imagination. To learn more about customizing QR codes, refer to "How to Customize and Add Logos to QR Codes".

Frequently Asked Questions

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

You can change the color of a barcode in .NET C# using IronBarcode's ChangeBarCodeColor method. This allows you to customize the barcode's appearance to match your design needs.

What methods are available for resizing barcodes in .NET C#?

IronBarcode provides methods like ResizeTo for resizing barcodes in pixels and ResizeToMil for adjusting widths in thousandths of an inch, suitable for 1D barcodes.

Can I add text annotations to barcodes in C#?

Yes, you can add text annotations to barcodes in C# using IronBarcode's AddAnnotationTextAboveBarcode and AddBarcodeValueTextBelowBarcode methods, allowing for enhanced labeling and information.

What should I do if my resized barcode is unreadable?

If your resized barcode becomes unreadable due to small dimensions, IronBarcode will ignore those values to maintain readability, ensuring that the barcode remains functional.

How do I ensure high-quality barcode rendering in .NET C#?

High-quality barcode rendering is achieved using IronBarcode's lossless resizing methods like ResizeTo and ResizeToMil, which maintain the image quality while adjusting the size.

What library can assist with barcode color and annotation styling?

IronDrawing, an open-source library, assists with color manipulation and annotation styling, allowing for creative and personalized barcode designs in IronBarcode.

Is it possible to change the font of barcode annotations in C#?

Yes, you can customize the font of barcode annotations using IronSoftware.Drawing.Font objects, providing flexibility in text styling above and below the barcode.

Where can I download the library for barcode customization in C#?

The C# library for barcode customization can be downloaded from NuGet at https://www.nuget.org/packages/BarCode/, enabling you to start customizing barcodes with IronBarcode.

How can I customize the background color of a barcode in .NET C#?

You can customize the background color of a barcode in .NET C# using IronBarcode's ChangeBackgroundColor method, allowing for unique design and branding opportunities.

What is the best way to maintain barcode readability after resizing?

To maintain readability after resizing, use IronBarcode's resizing methods which ensure that the barcode dimensions do not compromise its functionality and clarity.

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