【问题标题】:Printing a Bit map image to pos printer via comport in C# [closed]通过 C# 中的 comport 将位图图像打印到 pos 打印机 [关闭]
【发布时间】:2012-12-15 11:22:07
【问题描述】:

我是通过串口直接将收据打印到POS打印机上,

        SerialPort port = new SerialPort("com6", 9100, Parity.None, 8, StopBits.One);
        port.Open();
        port.Write("Some Text");
        port.Close();

我的问题是如何使用上述方法打印位图图像?任何帮助将不胜感激。

我还没有决定使用 Microsoft POS for.net,因为它很慢并且需要时间来初始化打印机,客户不喜欢等待。

谢谢。

【问题讨论】:

  • 您需要阅读打印机制造商的编程手册,以便了解要发送到打印机的 字节。会很慢。
  • 这里有一个非常相似的问题stackoverflow.com/questions/14530058/…。因此,我提名这个问题重新开放,因为它显然不是太本地化
  • 希望我能投票支持重新提出这个问题...过于本地化听起来像个笑话:o
  • 我相信这也是一个有效的问题

标签: c#


【解决方案1】:

此时可能没有用,但我认为要直接打印到打印机,您必须找到编程手册并发送命令转义。

这种打印机有自己的一套命令等等,可以格式化、旋转文本、打印条形码、上传和打印图像等等。

除非您不了解该语言并正确使用它,否则打​​印机的行为可能无法预测

【讨论】:

  • 是的,你说的是正确的。但正如我观察到的,ESC|Pos 命令对于大多数 POS 设备 VIA com 端口来说几乎是相同的。我在 Epson、Bixolon 和 E-Pos 打印机上进行了测试。命令是一样的。
  • 有些命令是一样的有些是不同的。大约有20个不同的“打开抽屉”命令
【解决方案2】:

我找到了另一种方式,Guyz 请传播这个消息!

第一步:

下载 NV 图像软件 (nv_image_tool_v3.1.6) 并将图像设置为打印机 VIA 端口 如图所示执行这些步骤

第 2 步:您最喜欢的按钮的代码:

 private void button1_Click(object sender, EventArgs e)
{
 SerialPort port = new SerialPort("com6", 9100, Parity.None, 8, StopBits.One);
 port.Open();
 ASCIIEncoding ascii = new ASCIIEncoding();
 port.Write(ascii.GetString(new byte[] { 28, 112, 1, 0})); //Printing the Above uploaded Logo
 port.WriteLine("Your Text");
 port.Close();
}

【讨论】:

  • 是的,这个工具使工作变得简单,因为我们不需要编写代码来将徽标注册到打印机中。
  • ..是的,但这是编程的 QA.. 不是 3rd 方软件的教程网站..
  • 感谢您的评论好友。如果您知道比这更好的方法,请在此处发布。无论如何,这是大多数打印机制造商提供的标准软件,以简化工作。特别是bixolon。
  • 这不是一个非常有用的答案,如果您使用的是 linux 或 MacOS,很多打印机制造商不支持非 Windows 的其他操作系统,如果您使用的是 Android 或 iOS 设备怎么办?
  • 如果你不使用windows,你仍然可以使用windows pc设置一次图像,然后永远从linux或任何其他操作系统打印它
【解决方案3】:

这应该会从位图中为您提供一个字符串,您可以将其发送到打印机:

    public string GetLogo()
    {
        string logo = "";
        if (!File.Exists(@"C:\bitmap.bmp"))
            return null;
         BitmapData data = GetBitmapData(@"C:\bitmap.bmp");
         BitArray dots = data.Dots;
         byte[] width = BitConverter.GetBytes(data.Width);

         int offset = 0;
         MemoryStream stream = new MemoryStream();
         BinaryWriter bw = new BinaryWriter(stream);

         bw.Write((char)0x1B);
         bw.Write('@');

         bw.Write((char)0x1B);
         bw.Write('3');
         bw.Write((byte)24);

         while (offset < data.Height)
         {
             bw.Write((char)0x1B);
             bw.Write('*');         // bit-image mode
             bw.Write((byte)33);    // 24-dot double-density
             bw.Write(width[0]);  // width low byte
             bw.Write(width[1]);  // width high byte

             for (int x = 0; x < data.Width; ++x)
             {
                 for (int k = 0; k < 3; ++k)
                 {
                     byte slice = 0;
                     for (int b = 0; b < 8; ++b)
                     {
                         int y = (((offset / 8) + k) * 8) + b;
                         // Calculate the location of the pixel we want in the bit array.
                         // It'll be at (y * width) + x.
                         int i = (y * data.Width) + x;

                         // If the image is shorter than 24 dots, pad with zero.
                         bool v = false;
                         if (i < dots.Length)
                         {
                             v = dots[i];
                         }
                         slice |= (byte)((v ? 1 : 0) << (7 - b));
                     }

                     bw.Write(slice);
                 }
             }
             offset += 24;
             bw.Write((char)0x0A);
         }
         // Restore the line spacing to the default of 30 dots.
         bw.Write((char)0x1B);
         bw.Write('3');
         bw.Write((byte)30);

         bw.Flush();
         byte[] bytes = stream.ToArray();
         return logo + Encoding.Default.GetString(bytes);
    }

    public BitmapData GetBitmapData(string bmpFileName)
    {
        using (var bitmap = (Bitmap)Bitmap.FromFile(bmpFileName))
        {
            var threshold = 127;
            var index = 0;
            double multiplier = 570; // this depends on your printer model. for Beiyang you should use 1000
            double scale = (double)(multiplier/(double)bitmap.Width);
            int xheight = (int)(bitmap.Height * scale);
            int xwidth = (int)(bitmap.Width * scale);
            var dimensions = xwidth * xheight;
            var dots = new BitArray(dimensions);

            for (var y = 0; y < xheight; y++)
            {
                for (var x = 0; x < xwidth; x++)
                {
                    var _x = (int)(x / scale);
                    var _y = (int)(y / scale);
                    var color = bitmap.GetPixel(_x, _y);
                    var luminance = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
                    dots[index] = (luminance < threshold);
                    index++;
                }
            }

            return new BitmapData()
            {
                Dots = dots,
                Height = (int)(bitmap.Height*scale),
                Width = (int)(bitmap.Width*scale)
            };
        }
    }

    public class BitmapData
    {
        public BitArray Dots
        {
            get;
            set;
        }

        public int Height
        {
            get;
            set;
        }

        public int Width
        {
            get;
            set;
        }
    }

【讨论】:

  • Thanx Buddy 成功了! :)
  • 这个真的很管用……
  • 必须是位图,还是我也可以用它来打印jpeg和png?
  • 这个解决方案是救命稻草!谢谢!
  • @blue,我不记得它是什么意思,也不记得我为什么这样称呼它。已经很长时间了 :) 如果我理解正确 - 这是打印机输出的宽度(以像素为单位)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-13
  • 1970-01-01
  • 2010-09-16
  • 1970-01-01
  • 2011-12-28
  • 1970-01-01
  • 2018-05-13
相关资源
最近更新 更多