Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net core 6 rendering issue when using replacing System.Drawing with ImageSharp and ZXing #422

Closed
Alik2015 opened this issue May 13, 2022 · 1 comment
Assignees
Labels

Comments

@Alik2015
Copy link

As we have upgraded to net core 6 we are rewriting some of our code base. We have a tag helper in AspNet Core which generates a barcode. This currently uses System.Drawing and ZXing.

TagHelper Old version using System.Drawing - working (top barcode)

public override void Process(TagHelperContext context, TagHelperOutput output)
{
    var margin = 0;
    var qrCodeWriter = new ZXing.BarcodeWriterPixelData
    {
        Format = ZXing.BarcodeFormat.PDF_417,
        Options = new ZXing.Common.EncodingOptions
        {
            Height = this.Height > 80 ? this.Height : 80,
            Width = this.Width > 400 ? this.Width : 400,
            Margin = margin
        }
    };
    var pixelData = qrCodeWriter.Write(QRCodeContent);
    // creating a bitmap from the raw pixel data; if only black and white colors are used it makes no difference
    // that the pixel data ist BGRA oriented and the bitmap is initialized with RGB
    using (var bitmap = new Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
    using (var ms = new MemoryStream())
    {
        var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height),
        System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
        try
        {
            // we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
            System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0,
            pixelData.Pixels.Length);
        }
        finally
        {
            bitmap.UnlockBits(bitmapData);
        }
        // save to stream as PNG
        bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        output.TagName = "img";
        output.Attributes.Clear();
        output.Attributes.Add("width", Width);
        output.Attributes.Add("height", Height);
        output.Attributes.Add("alt", Alt);
        output.Attributes.Add("src",
        $"data:image/png;base64,{Convert.ToBase64String(ms.ToArray())}");
    }
}

TagHelper new version using ImageSharp - almost working but not exactly (bottom barcode)

public override void Process(TagHelperContext context, TagHelperOutput output)
{
    var margin = 0;
    var barcodeWriter = new ZXing.ImageSharp.BarcodeWriter<SixLabors.ImageSharp.PixelFormats.La32>
    {
        Format = ZXing.BarcodeFormat.PDF_417,
        Options = new ZXing.Common.EncodingOptions
        {
            Height = this.Height > 80 ? this.Height : 80,
            Width = this.Width > 400 ? this.Width : 400,
            Margin = margin
        }
    };

    var image = barcodeWriter.Write(QRCodeContent);
    output.TagName = "img";
    output.Attributes.Clear();
    output.Attributes.Add("width", Width);
    output.Attributes.Add("height", Height);
    output.Attributes.Add("alt", Alt);
    output.Attributes.Add("src", $"{image.ToBase64String(PngFormat.Instance)}");
} 

The issue is as mentioned the 2nd barcode is very slightly different at the end seems to extend the last bar.

What am I missing?

Results

micjahn added a commit that referenced this issue May 18, 2022
@micjahn micjahn self-assigned this May 18, 2022
@micjahn micjahn added the bug label May 18, 2022
@micjahn
Copy link
Owner

micjahn commented May 18, 2022

The bug is fixed in the newest version of the zxing bindings for image sharp.
Thanks for reporting.

@micjahn micjahn closed this as completed May 18, 2022
micjahn added a commit that referenced this issue Jan 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants