Problem with converting image to ZPL code

133 views Asked by At

I need to print a zpl label with a photo on it. I start by converting the received image to a black and white inverted bitmap. It looks good. Then I convert it to zpl code. The problem arises during the conversion. The received code generates an image in which there is a black bar on the right side or a repetition of the center of the image.

enter image description here

Below is my code. I based the code on the code from Converting PNG image to ZPL code

public static string ConvertToZpl(string path)
{
    
        string bitmapFilePath = $@"{path}";
        Bitmap imageToConvert = new Bitmap(bitmapFilePath);
        var rectangle = new Rectangle(0, 0, imageToConvert.Width, imageToConvert.Height);
        Bitmap monochromeImage = imageToConvert.Clone(rectangle, System.Drawing.Imaging.PixelFormat.Format1bppIndexed);

        // Mirror image
        monochromeImage.RotateFlip(RotateFlipType.Rotate180FlipX);

        // Save mono image            

        // // Save mono image            
        monochromeImage.Save( $@".\tmp\test_mono.bmp", System.Drawing.Imaging.ImageFormat.Bmp);

        // 2. Convert to ZPL
        return ConvertImage($@".\tmp\test_mono.bmp");   
        // return ConvertImage(monochromeImage);   

        
}

public static string ConvertImage(string path)
{
  string bitmapFilePath = path;
  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());
  DEBUG.Log(bitmapDataOffset.ToString());
  // bitmapDataOffset=58;
  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 < bitmapDataLength; i++)
  {
    bitmap[i] ^= 0xFF;
  }

  // Create ASCII ZPL string of hexadecimal bitmap data
  string ZPLImageDataString = BitConverter.ToString(bitmap);
  ZPLImageDataString = ZPLImageDataString.Replace("-", string.Empty);
  // Add new line every 1023 chars characters
  string ZPLImageDataStringWithNewLine = SpliceText(ZPLImageDataString, 1023);

  // Create ZPL command to print image
  string ZPLCommand = string.Empty;

  ZPLCommand += "^XA";
  ZPLCommand += "^FO20,20";
  ZPLCommand +=
  "^GFA," +
  bitmapDataLength.ToString() + "," +
  bitmapDataLength.ToString() + "," +
  widthInBytes.ToString() + "," +
  System.Environment.NewLine +
  ZPLImageDataStringWithNewLine;

  ZPLCommand += "^XZ";

  return ZPLCommand;
}
0

There are 0 answers