如何生成一个动态GRF图像ZPL斑马打印 [英] How to generate a dynamic GRF image to ZPL ZEBRA print
本文介绍了如何生成一个动态GRF图像ZPL斑马打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题。
I'm生成一个动态的BMP图像,并试图通过ZPL发送给斑马打印机命令。
我需要我的BMP转换为GRF的形象。我认为我的十六进制由isn't正确的BMP图像中提取。
打印出的图像模糊和不正确的。
这是我的代码:
字符串bitmapFilePath = @oldArquivo; //文件附加到这个支持文章
字节[] = bitmapFileData System.IO.File.ReadAllBytes(bitmapFilePath);
INT档案大小= bitmapFileData.Length;
位图ImgTemp =新位图(bitmapFilePath);
规格= ImgSize ImgTemp.Size;
ImgTemp.Dispose();
//下面的人知道TEST.BMP。它是由开发商
//确定此信息,除了给定的位图TEST.BMP。
INT宽度= ImgSize.Width;
INT高度= ImgSize.Height;
INT bitmapDataOffset = 62; // 62 =图像
为int的头bitmapDataLength =档案大小 - 62; // 8160;
双widthInBytes = Math.Ceiling(宽/ 8.0);
//复制了从位图文件中的实际位图数据。
//这表示没有标题信息的位图数据。
字节[] =位图新的字节[bitmapDataLength]
Buffer.BlockCopy(bitmapFileData,bitmapDataOffset,位图,0,(bitmapDataLength));
//反转位图的颜色
的for(int i = 0; I< bitmapDataLength;我++)
{
位[I] ^ = 0xFF的;
}
//创建进制位图数据
串的ASCII码的ZPL串ZPLImageDataString = BitConverter.ToString(位图).Replace( - ,的String.Empty);
串comandoCompleto =〜DG+ nomeImagem +.GRF,0+ bitmapDataLength.ToString()+,0+ widthInBytes.ToString()+,+ ZPLImageDataString;
解决方案
试试下面的代码。 未测试!
公共静态字符串CreateGRF(字符串的文件名,字符串imagename)
{
BMP位图= NULL;
的BitmapData imgData = NULL;
字节[]像素;
INT的x,y,宽度;
StringBuilder的某人;
IntPtr的PTR;
试
{
BMP =新位图(文件名);
imgData = bmp.LockBits(新的Rectangle(0,0,bmp.Width,bmp.Height),ImageLockMode.ReadOnly,PixelFormat.Format1bppIndexed);
宽度=(bmp.Width + 7)/ 8;
像素=新的字节[宽度]
某人=新的StringBuilder(宽* bmp.Height * 2);
PTR = imgData.Scan0;
为(Y = 0; Y< bmp.Height; Y ++)
{
Marshal.Copy(PTR,像素,0,宽度);
为(X = 0; X<宽度; X ++)
sb.AppendFormat({0:X2}(字节)〜像素[X]);
PTR =(IntPtr的)(ptr.ToInt64()+ imgData.Stride);
}
}
终于
{
如果(BMP!= NULL)
{
如果(imgData!= NULL)bmp.UnlockBits (imgData);
bmp.Dispose();
}
}
返回的String.Format(〜DG {0} .GRF,{1},{2},imagename,宽度* Y,宽度)+ sb.ToString ();
}
I have a problem.
I´m generating a dynamic BMP image and trying to send this to a ZEBRA printer by ZPL commands. I need to convert my BMP to a GRF image. I think that my Hexadecimal extracted by the BMP image isn´t correct.
The printed image is blurred and incorrect.
This is my code:
string bitmapFilePath = @oldArquivo; // file is attached to this support article
byte[] bitmapFileData = System.IO.File.ReadAllBytes(bitmapFilePath);
int fileSize = bitmapFileData.Length;
Bitmap ImgTemp = new Bitmap(bitmapFilePath);
Size ImgSize = ImgTemp.Size;
ImgTemp.Dispose();
// 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 width = ImgSize.Width;
int height = ImgSize.Height;
int bitmapDataOffset = 62; // 62 = header of the image
int bitmapDataLength = fileSize - 62;// 8160;
double widthInBytes = Math.Ceiling(width / 8.0);
// 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));
// 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).Replace("-", string.Empty);
string comandoCompleto = "~DG" + nomeImagem + ".GRF,0" + bitmapDataLength.ToString() + ",0" + widthInBytes.ToString() + "," + ZPLImageDataString;
解决方案
Try the following code. Not tested!
public static string CreateGRF(string filename, string imagename)
{
Bitmap bmp = null;
BitmapData imgData = null;
byte[] pixels;
int x, y, width;
StringBuilder sb;
IntPtr ptr;
try
{
bmp = new Bitmap(filename);
imgData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format1bppIndexed);
width = (bmp.Width + 7) / 8;
pixels = new byte[width];
sb = new StringBuilder(width * bmp.Height * 2);
ptr = imgData.Scan0;
for (y = 0; y < bmp.Height; y++)
{
Marshal.Copy(ptr, pixels, 0, width);
for (x = 0; x < width; x++)
sb.AppendFormat("{0:X2}", (byte)~pixels[x]);
ptr = (IntPtr)(ptr.ToInt64() + imgData.Stride);
}
}
finally
{
if (bmp != null)
{
if (imgData != null) bmp.UnlockBits(imgData);
bmp.Dispose();
}
}
return String.Format("~DG{0}.GRF,{1},{2},", imagename, width * y, width) + sb.ToString();
}
这篇关于如何生成一个动态GRF图像ZPL斑马打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文