Generated barcodes too wide

Questions, comments and suggestions concerning VintaSoft Barcode .NET SDK.

Moderator: Alex

IAmByB
Posts: 15
Joined: Thu Jan 19, 2017 2:01 pm

Generated barcodes too wide

Post by IAmByB »

Hello,

I am using Vintasoft barcode writer to create barcode images that I am inserting into a PDF document creatéd with iTextSharp.

I want to create a Barcode128 image with a specific Dimension (XFactor) ( XFactor = 0.375 millimeters) but the resulting barcode is always wider (almost 2 x the expected size) than what I expected, so the barcode does not fit in the document I created.

The value to store in the barcode is given by our client, so it is supposed to fit the barcode width specified (50 mm).

Can you help me ? Is there any error in the configuration I use ?

Thank you.

The code I use is the following :

Code: Select all

resolution = 72
textForBarcode = "8R27854156264";
width = 50; // in millimeters
height = 25; // in millimeters

public static Bitmap GetBarcode128(ParcelLabelOrientation parcelLabelOrientation, string textForBarcode, int resolution, int width, int height)
{
    if (String.IsNullOrEmpty(textForBarcode))
    {
        return null;
    }
    Bitmap img = null;

    // set barcode writer settings
    BarcodeWriter barcodeWriter = new BarcodeWriter();
    barcodeWriter.Settings.Resolution = resolution;
    barcodeWriter.Settings.Barcode = BarcodeType.Code128;
    barcodeWriter.Settings.SetHeight(height, UnitOfMeasure.Millimeters);
    barcodeWriter.Settings.SetMinWidth(0.375, UnitOfMeasure.Millimeters);
    barcodeWriter.Settings.Code128EncodingMode = Vintasoft.Barcode.BarcodeInfo.Code128EncodingMode.Undefined;
    barcodeWriter.Settings.ValueVisible = false;
    barcodeWriter.Settings.Value = textForBarcode;

    // get a barcode image
    img = barcodeWriter.GetBarcodeAsBitmap();

    return img;
}
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: Generated barcodes too wide

Post by Alex »

Hello,

Here is an artcile that shows how to create barcode with specified size and resolution:
http://www.vintasoft.com/docs/vsbarcode ... ution.html

Please read the artcile and let me know if you will have any question or problem.

Best regards, Alexander
IAmByB
Posts: 15
Joined: Thu Jan 19, 2017 2:01 pm

Re: Generated barcodes too wide

Post by IAmByB »

Thank you for your answer.

I already read this article, but I found nothing about how to specify the XFactor (or Dimension) which must be 0.375 millimeters in this specific case.

I assumed it is the MinWidth parameter in Vintasoft software, but still need a validation of this hypothesis.

In an example, I want to create a barcode 128 with :

Resolution = 72
Value = "8R27854156301"
Width = 50 millimeters
Height = 25 millimeters.
XFactor = 0.375 millimeters.

As a result of the code I posted before, Vintasoft barcode writer return a PNG image of 103.74 millimeters X 26.46 millimeters, with resolution = 72 (I don't have a tool to check the XFactor value).

Is this normal ? The resulting width is almost 2 X the required width.

Thank you.
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: Generated barcodes too wide

Post by Alex »

Thank you for information.

Code128 barcode with value "8R27854156301" has 147 bars. Our barcode writer uses 2 pixels as the minimum value of the bar width for Code128 barcode. In other words, the minimum width for image with Code128 barcode is 147*2=294 pixels.

You want to create image with width 50 mm and resolution 72 dpi, i.e. you want to create image with width 141 pixels.

Result - you want to draw barcode with size 294 pixels in image with size 141 pixels.

For solving your problem you need create barcode in vector form, scale vector graphics and draw scaled graphics on image.

Here is a code snippet that allow to get the Code128 barcode in the vector form and draw * on image:

Code: Select all

string filename = "test.png";
ImageFormat imageformat = ImageFormat.Png;
string barcodeValue = "8R27854156301";
int width = (50 / 10) / 2.54 * 72;
int height = (25 / 10) / 2.54 * 72;

BarcodeWriter writer = new BarcodeWriter();
writer.Settings.SetHeight(height - writer.Settings.ValueFont.Height * 2, UnitOfMeasure.Pixels);
writer.Settings.Barcode = BarcodeType.Code128;
writer.Settings.Value = barcodeValue;
using (GraphicsPath barcodePath = writer.GetBarcodeAsGraphicsPath())
{
    using (Image image = new Bitmap(width, height, PixelFormat.Format24bppRgb))
    {
        using (Graphics g = Graphics.FromImage(image))
        {
            // draw barcode path
            RectangleF barcodePathBounds = barcodePath.GetBounds();
            g.Clear(Color.White);
            float padding = 5;
            using (Matrix m = new Matrix((width - padding * 2) / barcodePathBounds.Width, 0, 0, 1, padding, padding))
                barcodePath.Transform(m);
            // enable AntiAlias if need!
            //g.SmoothingMode = SmoothingMode.AntiAlias;
            g.FillPath(Brushes.Black, barcodePath);

            // draw barcode value
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            RectangleF textRect = new RectangleF(
                0,
                barcodePathBounds.Height + writer.Settings.ValueGap,
                width,
                height - barcodePathBounds.Height - writer.Settings.ValueGap);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.DrawString(contents, writer.Settings.ValueFont, Brushes.Black, textRect, sf);
        }
    }
    image.Save(filename, imageformat);
}
Best regards, Alexander
IAmByB
Posts: 15
Joined: Thu Jan 19, 2017 2:01 pm

Re: Generated barcodes too wide

Post by IAmByB »

Thank you for your very clear and informative answer.

I have been trying the code you posted. It seems to be working as the generated barcode seems to have the correct width now.

My new problem is that the resulting bitmap cannot be passed to iTextSharp to insert it into my PDF document. Every try leads to an exception.
I have to find a way now to solve this problem, but I want anyway to thank you for the code you wrote.
IAmByB
Posts: 15
Joined: Thu Jan 19, 2017 2:01 pm

Re: Generated barcodes too wide

Post by IAmByB »

Hello,


The bug was mine, sorry.

Thank you for your help.
IAmByB
Posts: 15
Joined: Thu Jan 19, 2017 2:01 pm

Re: Generated barcodes too wide

Post by IAmByB »

Hello,

Your code was very helpful, but I still have questions about the barcode writer.

As a matter of fact, when I use your code and ask for a 50mm wide barcode, the result is a 45mm barcode (value "8R27854156950")
When I ask for a 78mm wide barcode, the result is a 64mm wide barcode (value "8R1775508128050100000050").
When I ask for a 85mm wide barcode, the result is a 74mm wide barcode (value "%00775508R278541569500835250").

I understand the barcode width depend of the value, but I would like to know which parameters of the writer should I use to get a better approximation of the width I ask for ?

Thank you.
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: Generated barcodes too wide

Post by Alex »

Here is formula that calculates the barcode width in pixels: [barcode_width_in_pixels] = Math.Ceiling(([user_desired_barcode_width_in_pixels] - [offsets_in_pixels]) / [bar_count]) * [bar_count]

Best regards, Alexander
IAmByB
Posts: 15
Joined: Thu Jan 19, 2017 2:01 pm

Re: Generated barcodes too wide

Post by IAmByB »

Thank you for the formula.

I am working on PDF labels for Colissimo parcels in France.

They ask for a barcode 128 with "base module" (ie the size of the smallest bar) of 0.375 millimeters.

As my barcode is printed in 72 dpi resolution, I made the following calculations :

0.375 mm = 0,01476378 inch
Resolution is 72 ppi, so the width of the smallest bar in pixels should be : 0,01476378 x 72 = 1,06299216 pixels

To generate such barcodes with Vintasoft, is it OK to use a MinWidth value of 1 ?

Thank you for your help.
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: Generated barcodes too wide

Post by Alex »

IAmByB wrote:0.375 mm = 0,01476378 inch
Resolution is 72 ppi, so the width of the smallest bar in pixels should be : 0,01476378 x 72 = 1,06299216 pixels

To generate such barcodes with Vintasoft, is it OK to use a MinWidth value of 1 ?
As I said you before our barcode writer uses 2 pixels as the minimum bar width for Code128 barcode. This was made because we do not recommend to use 1 pixels bars in Code128 barcodes if you want to create the stable barcode reading system. In your situation you need generate barcode in vector form (as a graphics path) and scale the vector as you want.

Best regards, Alexander
Post Reply