【发布时间】:2016-05-30 05:01:00
【问题描述】:
我想编写一个将位图图像转换为 zpl 的代码。我为此找到了以下代码:
string bitmapFilePath = @"D:\Demo.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;
// The following is known about test.bmp. It is up to the developer
// to determine this information for bitmaps besides the given test.bmp.
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()); // 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);
//byte[] ReverseBitmap = new byte[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);
// Create ZPL command to print image
string ZPLCommand = string.Empty;
ZPLCommand += "^XA";
ZPLCommand += "^PMY^FO20,20";
ZPLCommand +=
"^GFA, " +
bitmapDataLength.ToString() + "," +
bitmapDataLength.ToString() + "," +
widthInBytes.ToString() + "," +
ZPLImageDataString;
ZPLCommand += "^XZ";
System.IO.StreamWriter sr = new StreamWriter(@"D:\logoImage.zpl", false, System.Text.Encoding.Default);
sr.Write(ZPLCommand);
sr.Close();
上面的代码运行完美,但问题是它正在生成几乎 4 kb 的 zpl 文件,假设但相同的文件由 labelary 转换为 2 kb 大小。我想知道 GFA、a、b、c、data 中使用的标签是什么格式。
【问题讨论】: