0

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

Keerthi Teja
  • 48
  • 1
  • 8
  • you did not mention what is wrong with your code/outcome. Isn't it 500x300? – kennyzx Sep 03 '17 at 13:52
  • ^ the image size is 500x300 but i want the barcode to be of equal size. If you look at the above two images, the second image height/width is changed. I'm working on a webapp which generates PDF with barcode pdf_417 n some information. All data is dynamic so I want the barcode to be of equal in size in all PDFs. – Keerthi Teja Sep 03 '17 at 15:42

2 Answers2

2

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.

Michael
  • 2,361
  • 1
  • 15
  • 15
0

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

Elias Bobadilla
  • 131
  • 1
  • 2