根据文档:
The first encoding,known as B64,encodes the data using the MIME
Base64 scheme. Base64 is used to encode e-mail atachedments …
Base64 encodes six bits to the byte,for an expantion of 33 percent
over the un-enclosed data.
The second encoding,known as Z64,
first compresses the data using the LZ77 algorithm to reduce its size.
(This algorithm is used by the PKZIP and is intergral to the PNG
graphics format.)
The compressed data is then encoded using the
MIME Base64 scheme as described above.
A CRC will be calculated
accross the Base64-encoded data.
但它没有很多更多的信息.
基本上我正在尝试编码
private byte[] GetItemFromPath(string filepath) { using (MemoryStream ms = new MemoryStream()) { using (Image img = Image.FromFile(filepath)) { img.Save(ms,ImageFormat.Png); return ms.ToArray(); } } }
然后尝试打印如下:
var initialArray = GetItemFromPath("C:\\RED.png"); string converted = Convert.ToBase64String(b); PrintThis(string.Format(@"~DYRED.PNG,P,{1},:B64: {0} ^XA ^F0200,200^XGRED.PNG,1,1^FS ^XZ",converted .ToString(),initialArray.Length));
从它的声音,B64或Z64都被接受.
我已经尝试了几个变体,以及用于生成CRC和计算“大小”的几种方法.
但是似乎没有任何工作,图形下载到打印机总是被中止.
有没有人设法完成这样的事情?还是知道我在哪里错了?
解决方法
我已经浏览了许多,许多,许多论坛和Stackoverflow帖子试图弄清楚,因为它似乎是一个这么简单的事情.我已经尝试过在其他地方发布的每一个解决方案,但我真的只想打印一个.PNG,因为我的打印机手册(Mobile QLN320)支持它内置.它说要发送它在Base64或十六进制,我试过两者都无济于事对于任何想要做Base64的人,我发现在旧的手册中,您需要手动计算发送的每个数据包的CRC码,所以我选择使用更简单的十六进制路由.所以这里是我工作的代码!
string ipAddress = "192.168.1.30"; int port = 6101; string zplImageData = string.Empty; //Make sure no transparency exists. I had some trouble with this. This PNG has a white background string filePath = @"C:\Users\Path\To\logo.png"; byte[] binaryData = System.IO.File.ReadAllBytes(filePath); foreach (Byte b in binaryData) { string hexRep = String.Format("{0:X}",b); if (hexRep.Length == 1) hexRep = "0" + hexRep; zplImageData += hexRep; } string zplToSend = "^XA" + "^MNN" + "^LL500" + "~DYE:logo," + binaryData.Length + "," + zplImageData+"^XZ"; string printImage = "^XA^FO115,50^IME:logo.PNG^FS^XZ"; try { // Open connection System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient(); client.Connect(ipAddress,port); // Write ZPL String to connection System.IO.StreamWriter writer = new System.IO.StreamWriter(client.GetStream(),Encoding.UTF8); writer.Write(zplToSend); writer.Flush(); writer.Write(printImage); writer.Flush(); // Close Connection writer.Close(); client.Close(); } catch (Exception ex) { // Catch Exception }