BinaryKits / BinaryKits.Zpl

BinaryKits.Zpl a set of .net libraries. The project supports you in the simple creation of zebra labels. It generates the ZPL data, it is a printer description language from Zebra Technologies. ZPL II is now emulated by many label printers from different manufacturers. So with this implementation you can create labels for most printers.
MIT License
320 stars 115 forks source link

png hexdata call ImageSharpImageConverter.ConvertImage retun transparent image with no content. #244

Open masternan opened 6 months ago

masternan commented 6 months ago

png hexdata call ImageSharpImageConverter.ConvertImage retun empty data. and copy zpl code to https://labelary.com/, it display transparent image zone for png data.

snap1 snap2

masternan commented 6 months ago

I modified ImageSharpImageConverter.ConvertImage like this,it is ok.

` public ImageResult ConvertImage(byte[] imageData) { using MemoryStream memoryStream = new MemoryStream(); using Image image = Image.Load(imageData); using Image whiteBackgroundImage = new Image(image.Width, image.Height); whiteBackgroundImage.Mutate(ctx => ctx.BackgroundColor(Color.White)); whiteBackgroundImage.Mutate(ctx => ctx.DrawImage(image, new Point(0, 0), 1.0f)); int num = ((whiteBackgroundImage.Width % 8 > 0) ? (whiteBackgroundImage.Width / 8 + 1) : (whiteBackgroundImage.Width / 8)); int binaryByteCount = whiteBackgroundImage.Height * num; int num2 = 0; int num3 = 0; for (int i = 0; i < whiteBackgroundImage.Height; i++) { for (int j = 0; j < whiteBackgroundImage.Width; j++) { Rgba32 rgba = whiteBackgroundImage[j, i]; if ((rgba.R + rgba.G + rgba.B) / 3 < 128) { num2 |= 1 << 7 - num3; }

         num3++;
         if (num3 == 8 || j == whiteBackgroundImage.Width - 1)
         {
             memoryStream.WriteByte((byte)num2);
             num2 = 0;
             num3 = 0;
         }
     }
 }
 return new ImageResult
 {
     RawData = memoryStream.ToArray(),
     BinaryByteCount = binaryByteCount,
     BytesPerRow = num
 };

}`

primo-ppcg commented 6 months ago

Unable to reproduce.

^XA
^FO32,128^GFA,2688,2688,28,:Z64:eNrtwTEBAAAAwqD+qWcLL6AAAOBnXV92Fw==:84a9
^FT245,180^A0N,50,50^FH\^FD#PartName#^FS
^XZ

image

masternan commented 6 months ago

hotprocess.txt Attach is the file exported from zebra designer,and i used DrawerOptions in the following code to analyze it. Then i transfer it to konva object in order to edit it in web form with Konva.js. At last I transfer the konva object to zpl, something goes wrong such as i describled.

private static DrawerOptions BuildDrawOption(string fontName = "NotoSansSC-SemiBold") { var fontFile = Path.Combine(AppContext.BaseDirectory, "fonts", $"{fontName}.ttf"); var drawOptions = new DrawerOptions { FontLoader = fontName => { return SKTypeface.FromFile(fontFile); }, OpaqueBackground=false, PdfOutput=false, RenderFormat = SKEncodedImageFormat.Png, RenderQuality = 100, Antialias=true, ReplaceDashWithEnDash = false }; return drawOptions; }

primo-ppcg commented 6 months ago

The "hotprocess.txt" file contains two labels; I suspect you may only be exporting the first, which is blank and contains only directives.

The second label in the file renders correctly, according to what is show in Zebra Designer: label

masternan commented 6 months ago

Thank you,but the case you described is not the reason. When I modified ImageSharpImageConverter.ConvertImage function to add a whiteBackground to image label before analyzing it ,and it returns ok.

image

Attach is the exception before i modified the function, please refer it,thanks.

primo-ppcg commented 6 months ago

Please ensure that you are using SixLabors.ImageSharp v2.1.8 throughout your project. I suspect you may have a dependency conflict.

masternan commented 6 months ago

I use SixLabors.ImageSharp3.1.4 ,but when i modified the ImageSharpImageConverter.ConvertImage function,it returns ok. so i think the problem was caused by the transparent png data. Attatch is the right result after i modified the function. image

primo-ppcg commented 6 months ago

Reopening this. Upgrading to ImageSharp 3.1.4 breaks rendering of GraphicField elements, and some barcodes. The project should be updated for forwards compatibility.