micjahn/ZXing.Net

Barcode generator: unpredictable whitespace padding around it depending on requested Width

Opened this issue · 0 comments

If you ask the barcode writer for an arbitrary width that isn't a multiple of the "default minimum" barcode width for that specific code, then you end up with unpredictable horizontal padding around the barcode.

an example:

public FileContentResult GithubExample()
{
    var code = "123456789";
    var barcodeWriter = new ZXing.ImageSharp.BarcodeWriter<Rgba32>
    {
        Format = BarcodeFormat.CODE_39,
        Options = new EncodingOptions
        {
            Height = 400,
            Width = 400,
            Margin = 0
        }
    };

    using var image = barcodeWriter.Write(code);;

    using var stream = new MemoryStream();

    var imageEncoder = new PngEncoder();
    var mime = "image/png";
    image.Save(stream, imageEncoder);

    var bytes = stream.ToArray();
    return File(bytes, mime);
}

result:
image

The workaround we currently have is to generate with Width = 1, then resize using ImageSharp after the fact. But it seems like something that ought to be handled by the generator anyway.