通过C#中的comport将位图图像打印到pos打印机

通过C#中的comport将位图图像打印到pos打印机,第1张

概述我是通过以下方式通过串口直接将收据打印到POS打印机, SerialPort port = new SerialPort("com6", 9100, Parity.None, 8, StopBits.One); port.Open(); port.Write("Some Text"); port.Close(); 我的问题是我将如何使用上述方法打印位 我是通过以下方式通过串口直接将收据打印到POS打印机,
SerialPort port = new SerialPort("com6",9100,Parity.None,8,StopBits.One);        port.open();        port.Write("Some Text");        port.Close();

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

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

谢谢.

解决方法 这应该会从位图中获取一个您可以发送到打印机的字符串:
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;        }    }
总结

以上是内存溢出为你收集整理的通过C#中的comport将位图图像打印到pos打印机全部内容,希望文章能够帮你解决通过C#中的comport将位图图像打印到pos打印机所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1259457.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存