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

Free Support Forum - aspose.com

Generate barcodes with custom width and height using Aspose.BarCode for .NET

I’m generating a Codecode type Barcode with Aspose but I can not give a width of 93mm and length of 12mm.
When I create the Barcode, I get a size error.

es:
MemoryStream mStream = new MemoryStream();
Aspose.BarCode.BarCodeBuilder barCode = new Aspose.BarCode.BarCodeBuilder(barcode, Aspose.BarCode.Generation.EncodeTypes.Code128);
barCode.GraphicsUnit = System.Drawing.GraphicsUnit.Millimeter;
barCode.AutoSize = false;
barCode.ImageWidth = 93;
barCode.ImageHeight = 12;
barCode.Save(mStream, Aspose.BarCode.BarCodeImageFormat.Svg);

Error size:

Can you help me?

@Ascopiave,

You will get runtime exception in case the code text does not fit in custom defined height and width. Minimum barcode image size depends upon code text length. Please use the latest version of the API. It contains a new method GetMinimumBarCodeSize(). This method will enables you to know the minimum size required to generate a barcode. This method is exposed to facilitate the user to get minimum possible dimension. Furthermore If you remove margins and set setCodeLocation to None then the image size will be different. Please consider these and try at your end.

126/5000
I am using version 18.3.0.0.
Below is the modified code:

        MemoryStream mStream = new MemoryStream();
        Aspose.BarCode.BarCodeBuilder barCode = new Aspose.BarCode.BarCodeBuilder("18920170015076820158120000009933111000000363233896", Aspose.BarCode.Generation.EncodeTypes.Code128);
        barCode.GraphicsUnit = System.Drawing.GraphicsUnit.Millimeter;
        barCode.AutoSize = false;
        barCode.BorderWidth = 0f;
        barCode.CodeLocation = Aspose.BarCode.CodeLocation.None;
        barCode.Margins = new Aspose.BarCode.MarginsF(0f, 0f, 0f, 0f);
        barCode.Save(mStream, Aspose.BarCode.BarCodeImageFormat.Svg);

The exception that triggers the code at the time of saving
Too small image’s size - width: 351px, height: 45px. The generated barcode don’t fit to image. Barcode size - width: 616px, height: 57px.

@Ascopiave,

Please use the following code snippet to generate the barcode image.

CODE:

System.IO.MemoryStream mStream = new System.IO.MemoryStream();
        Aspose.BarCode.BarCodeBuilder barCode = new Aspose.BarCode.BarCodeBuilder("18920170015076820158120000009933111000000363233896", Aspose.BarCode.Generation.EncodeTypes.Code128);
        barCode.GraphicsUnit = System.Drawing.GraphicsUnit.Millimeter;
        barCode.AutoSize = false;
        barCode.BorderWidth = 0f;
        barCode.CodeLocation = Aspose.BarCode.CodeLocation.None;
        barCode.Margins = new Aspose.BarCode.MarginsF(0f, 0f, 0f, 0f);

        System.Drawing.SizeF minSize = barCode.GetMinimumBarCodeSize();
        barCode.ImageWidth = minSize.Width;
        barCode.ImageHeight = minSize.Height;

        barCode.Save(mStream, Aspose.BarCode.BarCodeImageFormat.Svg);

        using (System.Drawing.Bitmap image = barCode.BarCodeImage)
            image.Save(@"minimumresult.png");