Converting PNG image to ZPL code

A Armin Sabic 3 years 7 months ago
7672 2 0

I need to print a PNG image onto a label with a ZPL printer. The idea is to convert the PNG image to a monochrome one and then generate the necessary ZPL code with the image data to print the image.
After some googling and coding, I have a piece of code that does just that. The generated ZPL code seems fine on labelary (http://labelary.com).
The code for generating the ZPL code was mostly taken from here --> How to optimize ASCII HEX for BMP to ZPL as using in Labelary
Unfortunately, when trying to print a label with the generated ZPL code, it comes out like this: Not supposed to look like this
Image should look like this: ImageToConvert
(This is just a sample image.)
The code that i use is this:
static void Main(string[] args)   {   // 1. Convert Image to monochrome bmp   string bitmapFilePath = @"somepath.bmp";   Bitmap imageToConvert = new Bitmap(bitmapFilePath);   var rectangle = new Rectangle(0, 0, imageToConvert.Width, imageToConvert.Height);   Bitmap monochromeImage = imageToConvert.Clone(rectangle, PixelFormat.Format1bppIndexed);   // Mirror image  monochromeImage.RotateFlip(RotateFlipType.Rotate180FlipX);   // Save mono image    monochromeImage.Save("somePathMono.bmp", ImageFormat.Bmp);   // 2. Convert to ZPL   ConvertImage();      }   public static void ConvertImage()   {   string bitmapFilePath = "somePathMono.bmp";   int w, h;   Bitmap b = new Bitmap(bitmapFilePath);  w = b.Width; h = b.Height;   byte[] bitmapFileData = System.IO.File.ReadAllBytes(bitmapFilePath);   int fileSize = bitmapFileData.Length;   int bitmapDataOffset = int.Parse(bitmapFileData[10].ToString()); ;   int width = w; // int.Parse(bitmapFileData[18].ToString()); ;   int height = h; // int.Parse(bitmapFileData[22].ToString()); ;   int bitsPerPixel = int.Parse(bitmapFileData[28].ToString());    int bitmapDataLength = bitmapFileData.Length - bitmapDataOffset;   double widthInBytes = Math.Ceiling(width / 8.0);   while (widthInBytes % 4 != 0)   {  widthInBytes++;   }   // Copy over the actual bitmap data without header data     byte[] bitmap = new byte[bitmapDataLength];   Buffer.BlockCopy(bitmapFileData, bitmapDataOffset, bitmap, 0, bitmapDataLength);      // Invert bitmap colors   for (int i = 0; i

Please Register or Login to post a reply

2 Replies

D Dmitry Prokhorov

Hello,

Would it be an option for you to print your PNG files using Link-OS Multiplatform SDK in a following way: GraphicsUtil example (Zebra API (build v2.14.5097)) ?

Dmitry Prokhorov
Software Engineer, Kutir Mobility
Posted on behalf of Zebra Technologies

K Keith Austermiller

One issue I see is the calculation of the widthInBytes.  WidthInBytes should be calculated as shown below...
widthInBytes = bitmapDataLength / height
(The following comments are just to help someone else who stumbles on to this reply)
** bitmapDataLength is the total length of the bitmap data bytes minus the bitmaps header.

** height is the height of the bitmap which can be derived from the bitmap's header.

CONTACT
Can’t find what you’re looking for?