We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Find Code128 Width

I am creating a Code128 barcode and providing an XDimension. Is there a way to determine the barcode width after generation?

Thank you,


Thanks for your query.

See the document with example code for your reference:
Managing Different Barcode Settings

@Amjad_Sahi - thank you for your reply.

I don’t see anything on this page that will return the width of a Code128 barcode. I see where I can set it - but if I’m setting the XDimension then I don’t want to set the width.

Does this make sense?

Thank you,

Yes, your understanding is right that when you set X-Dimension, then there is no need to set the width. However, I could not understand why you need the width of the image? You may please check the following articles where you can generate images of the desired size. Please give it a try and share the feedback. If your requirement is not fulfilled, share details along with the sample code, program output and desired output generated by any third-party tool for our reference.

Generating barcode by specifying custom image size
Generate Barcode Using Custom Width Support

I am adding other elements around the barcode and I need to know how big the barcode is so I can be sure by barcode is designed to spec.

Here is what I am attempting - image.png (258.5 KB)

Does this make sense?

@Amjad_Sahi @ahsaniqbalsidiqui
Ultimately this image is going on an AsposePDF - is there a way to center an image on a PDF?

Thank you,

You may give a try to the sample codes in the above articles and share the issues (if any) with us. Like following sample code generates an image of a specified size. Please give it a try and share the feedback along with the sample code for our reference.

        BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, "1234567890");
        generator.Parameters.AutoSizeMode = AutoSizeMode.Nearest;
        generator.Parameters.ImageHeight.Millimeters = 50;
        generator.Parameters.ImageWidth.Millimeters = 150;


Regarding placing the image in the centre of PDF, you may post this query on our relevant forum Aspose.PDF.


Did you review the screenshot I sent? I cannot set the width of the barcode. I can set the XDimension to the spec provided by the USPS. Once the barcode is generated it would be ideal to be able to retrieve the width of the barcode (based on the XDimension and the characters provided for encoding).

Is there something that I’m not understanding?

We are analyzing your issue here and will share our feedback soon.

You can determine generated image size after generation. Image size includes barcode itself and barcode caption, however you can generate barcode without caption (it is described in examples) and manually add it.

BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, “123456”);

//set XDimension
generator.Parameters.Barcode.XDimension.Pixels = 3;

//disable caption if required
// generator.Parameters.Barcode.CodeTextParameters.Location = CodeLocation.None;

Bitmap lGeneratedBitmap = generator.GenerateBarCodeImage();

//show image size
MessageBox.Show(“Image size, Width:” + lGeneratedBitmap.Width.ToString() + " Height:" + lGeneratedBitmap.Height.ToString());

//save image
lGeneratedBitmap.Save(@“d:\save\rec\gen.png”, ImageFormat.Png);