How to Customize and Style Barcodes

by Hairil Hasyimi Bin Omar

Over the years, barcode usage has been increasingly popular and are 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 to products and this resulted in increase in demand to have options to style barcodes. Therefore, some barcode types/encodings have come up with their own unique appearance such as PDF417, Aztec, IntelligentMail, MaxiCode, DataMatrix and many more.

In addition to that, IronBarcode has come up with options for users to further style the barcodes, in the aspect of the barcode colors, barcode resize and background colors. This is made possible with 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.

:path=/static-assets/barcode/content-code-examples/how-to/customize-barcode-style-use-ResizeTo.cs
using IronBarCode;

// Create barcode
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.PDF417, 300, 100);

// Export barcode
barcode.SaveAsPng("output.png");

// Resize and export the barcode
barcode.ResizeTo(250, 100).SaveAsPng("useResizeTo.png");
Imports IronBarCode

' Create barcode
Private barcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.PDF417, 300, 100)

' Export barcode
barcode.SaveAsPng("output.png")

' Resize and export the barcode
barcode.ResizeTo(250, 100).SaveAsPng("useResizeTo.png")
VB   C#

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).

That being said, this method is particularly suitable for 1D barcodes.

:path=/static-assets/barcode/content-code-examples/how-to/customize-barcode-style-use-ResizeToMil.cs
using IronBarCode;

// Create barcode
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("12345", BarcodeEncoding.Codabar, 250, 100);

// Export barcode
barcode.SaveAsPng("output.png");

// Resize and export the barcode
barcode.ResizeToMil(20, .73, 200).SaveAsPng("useResizeToMil.png");
Imports IronBarCode

' Create barcode
Private barcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeEncoding.Codabar, 250, 100)

' Export barcode
barcode.SaveAsPng("output.png")

' Resize and export the barcode
barcode.ResizeToMil(20, .73, 200).SaveAsPng("useResizeToMil.png")
VB   C#

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.

:path=/static-assets/barcode/content-code-examples/how-to/customize-barcode-style-change-barcode-color.cs
using IronBarCode;
using IronSoftware.Drawing;

GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Aztec);

// Change barcode color
barcode.ChangeBarCodeColor(Color.DarkKhaki);

// Change barcode's background color
barcode.ChangeBackgroundColor(Color.ForestGreen);

barcode.SaveAsPng("coloredAztec2.png");
Imports IronBarCode
Imports IronSoftware.Drawing

Private barcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Aztec)

' Change barcode color
barcode.ChangeBarCodeColor(Color.DarkKhaki)

' Change barcode's background color
barcode.ChangeBackgroundColor(Color.ForestGreen)

barcode.SaveAsPng("coloredAztec2.png")
VB   C#
Barcode with color

Add Barcode Annotation Example

IronBarcode also have the option to add and style the barcode annotation. Again, the styling for annotation here is also assisted by the functionality from IronDrawing in terms of editing the annotation color and fonts.

:path=/static-assets/barcode/content-code-examples/how-to/customize-barcode-style-add-annotation.cs
using IronBarCode;
using IronSoftware.Drawing;

GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Aztec, 500, 500);

// Change barcode and background color
barcode.ChangeBarCodeColor(Color.DarkCyan);
barcode.ChangeBackgroundColor(Color.PeachPuff);

// Create font for annotation
Font annotationFont = new Font("Candara", FontStyle.Bold, 70);

// Add annotation
barcode.AddAnnotationTextAboveBarcode("IronBarcodeRocks!", annotationFont, Color.DarkOrange);

// Create font for barcode value
Font barcodeValueFont = new Font("Cambria", FontStyle.Regular, 70);

// Add displayed barcode value
barcode.AddBarcodeValueTextBelowBarcode(barcodeValueFont, Color.SandyBrown);

barcode.SaveAsPng("annotationAndBarcodeValue.png");
Imports IronBarCode
Imports IronSoftware.Drawing

Private barcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Aztec, 500, 500)

' Change barcode and background color
barcode.ChangeBarCodeColor(Color.DarkCyan)
barcode.ChangeBackgroundColor(Color.PeachPuff)

' Create font for annotation
Dim annotationFont As New Font("Candara", FontStyle.Bold, 70)

' Add annotation
barcode.AddAnnotationTextAboveBarcode("IronBarcodeRocks!", annotationFont, Color.DarkOrange)

' Create font for barcode value
Dim barcodeValueFont As New Font("Cambria", FontStyle.Regular, 70)

' Add displayed barcode value
barcode.AddBarcodeValueTextBelowBarcode(barcodeValueFont, Color.SandyBrown)

barcode.SaveAsPng("annotationAndBarcodeValue.png")
VB   C#
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".

Hairil related to Add Barcode Annotation Example

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.