Set X dimension of Barcode

Spire.Barcode is a free .NET component specially designed for developers. It can generate many kinds of barcode such as EAN128, Codabar, DataMatrix, PostNet and so on. It can also scan the barcode images. X dimension is the measure of the narrowest bar in a barcode. Barcodes and scanners have different X dimensions, so they must be matched. Using Spire.Barcode, it is quiet an easy job to do this.

In this document, I will introduce you how to so.

Step 1: Create a BarcodeSettings instance.

BarcodeSettings setting = new BarcodeSettings();

Step 2: Set the data to render.

setting.Data = "58465157484";
setting.Data2D = "58465157484";

Step 3: Set the type of barcode to generate.

setting.Type = BarCodeType.UPCA;

Step 4: Set the value of X dimension.

setting.Unit = GraphicsUnit.Millimeter;
setting.X = 0.8F;

The property Unit specifies the measurement unit. In this sample, the measurement unit is millimeter.

Step 5: Generate barcode image using BarCodeGenerator.

BarCodeGenerator gen = new BarCodeGenerator(setting);
Image img = gen.GenerateImage();
img.Save("barcode.png");

Screenshot and Full Code:

Set X dimension of Barcode

[C#]
using Spire.Barcode;
using System.Drawing;


namespace SetXDimension
{
    class Program
    {
        static void Main(string[] args)
        {
            BarcodeSettings barsetting = new BarcodeSettings();

            //set the x dimension
            barsetting.X = 0.8f;
            barsetting.Unit = GraphicsUnit.Millimeter;

            barsetting.HasBorder = true;
            barsetting.BorderWidth = 0.5F;

            //set the data
            barsetting.Data = "58465157484";
            barsetting.Data2D = "58465157484";

            //generate UPCA barcode
            barsetting.Type = BarCodeType.UPCA;

            BarCodeGenerator bargenerator = new BarCodeGenerator(barsetting);
            Image barcodeimage = bargenerator.GenerateImage();
            barcodeimage.Save("barcode.png");

            System.Diagnostics.Process.Start("barcode.png");
        }
    }
}
[VB.NET]
Imports Spire.Barcode
Imports System.Drawing


Namespace SetXDimension
	Class Program
		Private Shared Sub Main(args As String())
			Dim barsetting As New BarcodeSettings()

			'set the x dimension
			barsetting.X = 0.8F
			barsetting.Unit = GraphicsUnit.Millimeter

			barsetting.HasBorder = True
			barsetting.BorderWidth = 0.5F

			'set the data
			barsetting.Data = "58465157484"
			barsetting.Data2D = "58465157484"

			'generate UPCA barcode
			barsetting.Type = BarCodeType.UPCA

			Dim bargenerator As New BarCodeGenerator(barsetting)
			Dim barcodeimage As Image = bargenerator.GenerateImage()
			barcodeimage.Save("barcode.png")

			System.Diagnostics.Process.Start("barcode.png")
		End Sub
	End Class
End Namespace

Additional Info

  • tutorial_title:
Last modified on Wednesday, 15 September 2021 03:29