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 note
Values 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

What barcode types can be styled using IronBarcode?

IronBarcode supports styling for various barcode types including PDF417, Aztec, IntelligentMail, MaxiCode, DataMatrix, and many more.

How can I resize a barcode using IronBarcode?

You can resize a barcode by using the ResizeTo method in IronBarcode. This allows you to input new width and height measurements in pixels for a lossless re-rendering of the barcode.

What is the ResizeToMil method and when should it be used?

The ResizeToMil method adjusts the barcode element's width in thousandths of an inch (mil) and height in inches. It is particularly suitable for resizing 1D barcodes.

How can I change the color of a barcode and its background?

IronBarcode allows you to change the barcode and background colors using the ChangeBarCodeColor and ChangeBackgroundColor methods on the GeneratedBarcode object.

Can I add annotations to my barcode using IronBarcode?

Yes, you can add annotations above and below the barcode using the AddAnnotationTextAboveBarcode and AddBarcodeValueTextBelowBarcode methods.

What library assists with color and annotation styling in IronBarcode?

IronDrawing, an open-source library, assists with color manipulation and annotation styling in IronBarcode.

Is it possible to change the font of barcode annotations?

Yes, using IronSoftware.Drawing.Font objects, you can customize the font for annotations both above and below the barcode.

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

You can download the C# library for barcode customization from NuGet: https://www.nuget.org/packages/BarCode/

What should I do if resized barcode dimensions are too small?

If the resized dimensions are too small for the barcode to be readable, the values will be ignored to preserve readability.

How does IronBarcode ensure high-quality barcode rendering?

IronBarcode ensures high-quality barcode rendering through lossless re-rendering using methods like ResizeTo and ResizeToMil, which maintain image quality.

Hairil Hasyimi Bin Omar
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 in Chemical and Process Engineering.