Fixed width and height Barcode PDF417 Format

3k views Asked by At

How to set Fixed width(500) and height(300) barcode size? I'm using Zxing library to generate barcode image.

private byte[] GenerateBarCodeZXing(string data)
    {
        var writer = new BarcodeWriter
        {
            Format = BarcodeFormat.PDF_417,
            Options = new EncodingOptions { Width = 500, Height = 300, Margin=0 } 
        };
        var imgBitmap = writer.Write(data);
        using (var stream = new MemoryStream())
        {
            imgBitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            return stream.ToArray();
        }
    }

OutPut Image:

enter image description here

enter image description here

2

There are 2 answers

0
Michael On BEST ANSWER

You can't set a fixed width and height with ZXing.Net. The library only supports scaling without antialiasing. The resulting size depends first on the content which should be encoded. More content means more rows and columns. The single black (and white) bars of every column and row have a specified size. In most cases the sum of all bars are not equal with the desired size. If there is more space needed the resulting PDF417 code is bigger than the requested size. If less than there is more white space around. The renderer only supports integer scaling factors.

The best way is, request a size of 1 pixel width and 1 pixel height. The barcode writer generates a barcode bitmap of the minimal needed size. Resize the bitmap after that with your prefered image manipulation library.

0
Elias Bobadilla On

there is a property that you can set a relative size ... try create a new dimention() and set 4 parametets ... dimention(min columns, max columns, min rows, max rows) this set the min and max size of barcode upc417