微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 将PNG图像打印到斑马网络打印机

我正在寻找一种将图像打印到斑马上并有很多麻烦的方式.

根据文档:

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和计算“大小”的几种方法.
但是似乎没有任何工作,图形下载到打印机总是被中止.

有没有人设法完成这样的事情?还是知道我在哪里错了?

解决方法

对于我来说这个答案的所有信用来自 LabView Forum用户Raydur.他发布了可以在LabView中打开的LabView解决方案,以发送图像.我个人没有用我的打印机运行它,我只是用它来找出正确的图像代码,所以我可以在我的代码中复制它.我错过的大事情是填充我的十六进制代码.例如:1A是好的,但如果你只有A,你需要在它前面填0,发送0A.您发送的ZPL中的文件大小也是字节数组的原始大小,而不是数据的最终字符串表示形式.

我已经浏览了许多,许多,许多论坛和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
        }

原文地址:https://www.jb51.cc/csharp/94381.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐