using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Runtime.InteropServices; using System.Drawing.Imaging; namespace z_ConvertTOZPLString { public static class z_ConvertTOZPLString { public static string getZPLString(string FilePathLoc) // Full path of the file. The file should be of type BMP monochrome { string bitmapFilePath = FilePathLoc; int w, h; Bitmap b = new Bitmap(bitmapFilePath); // normal file. //Bitmap imageToConvert = new Bitmap(bitmapFilePath); //var rectangle = new Rectangle(0, 0, imageToConvert.Width, imageToConvert.Height); //Bitmap b = imageToConvert.Clone(rectangle, PixelFormat.Format1bppIndexed); 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[24].ToString()); // Monochrome image required! int bitmapDataLength = bitmapFileData.Length - bitmapDataOffset; double widthInBytes = Math.Ceiling(width / 8.0); while (widthInBytes % 4 != 0) { widthInBytes++; } // Copy over the actual bitmap data from the bitmap file. // This represents the bitmap data without the header information. byte[] bitmap = new byte[bitmapDataLength]; Buffer.BlockCopy(bitmapFileData, bitmapDataOffset, bitmap, 0, bitmapDataLength); string valu2e = ASCIIEncoding.ASCII.GetString(bitmap); // 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); // Create ZPL command to print image string ZPLCommand = string.Empty; ZPLCommand += "^XA"; ZPLCommand += "^LT-20^POI^PMY^FO20,20"; ZPLCommand += "^FO20,20"; ZPLCommand += "^GFA, " + bitmapDataLength.ToString() + "," + bitmapDataLength.ToString() + "," + widthInBytes.ToString() + "," + ZPLImageDataString; ZPLCommand += "^XZ"; return ZPLCommand; } } }