C#热敏打印图片 串口打印图片

如图,一步一步慢慢调出来的

       //串口通信类
public System.IO.Ports.SerialPort serialPort = null;
serialPort = new System.IO.Ports.SerialPort("COM1");
serialPort.BaudRate =19200;
serialPort.DataBits = 8;
serialPort.StopBits = System.IO.Ports.StopBits.One;
serialPort.Parity = System.IO.Ports.Parity.None;
serialPort.Open();
        /// <summary>
        /// 打印位图logo
        /// </summary>
        /// <param name="bitmap"></param>
        public void PrintBitmap2(Bitmap bitmap)
        { 
            int Width=bitmap.Width-1;
            int Height =(int)(bitmap.Height)/8 ;
            int SpaceNum=(int)(244-Width)/2;
            int x0,x1, x2=0,x3=0,x4=0,x5=0,x6=0,x7=0;
            byte[] PBuf = new byte[1024*1024];
            int point = 0;
            PBuf[point++] = 27;
            PBuf[point++] = (byte)'3';
            PBuf[point++] = 16;
            PBuf[point++] = 27;
            PBuf[point++] = (byte)'r';
            PBuf[point++] = 0;
            Func<Color, bool> IsNearWhite = color => {
                if (color.A + color.B + color.G + color.R >= 255 + 250 + 250 + 250)
                {
                    return false;
                }
                return true;
            };
            for (int n = 0; n < Height; n++)
            {
               PBuf[point++] = 27;
               PBuf[point++] = (byte)'*';
               PBuf[point++] = 0;
               PBuf[point++] = (byte)(Width+SpaceNum);
               PBuf[point++] = 0;
               for (int i = 0; i< SpaceNum; i++)
               {
                  PBuf[point++] = 0;
               }
               for (int i = 0; i < Width; i++)
               {
                  x0=0;
                  x1=0;
                  x2=0;
                  x3=0;
                  x4=0;
                  x5=0;
                  x6=0;
                  x7=0;
                  
                  if(IsNearWhite(bitmap.GetPixel(i,0+(n*8))))
                    x0 = 128;
                  if (IsNearWhite(bitmap.GetPixel(i, 1 + (n * 8))))
                    x1 = 64;
                  if (IsNearWhite(bitmap.GetPixel(i, 2 + (n * 8))))
                    x2 = 32;
                  if (IsNearWhite(bitmap.GetPixel(i, 3 + (n * 8))))
                    x3 = 16;
                  if (IsNearWhite(bitmap.GetPixel(i, 4 + (n * 8))))
                    x4 = 8;
                  if (IsNearWhite(bitmap.GetPixel(i, 5 + (n * 8))))
                    x5 = 4;
                  if (IsNearWhite(bitmap.GetPixel(i, 6 + (n * 8))))
                    x6 = 2;
                  if (IsNearWhite(bitmap.GetPixel(i, 7 + (n * 8))))
                    x7 = 1;
                  PBuf[point++]=(byte)(x0 ^ x1 ^ x2 ^ x3 ^ x4 ^ x5 ^ x6 ^x7);
               }

               PBuf[point++] = 13;
               PBuf[point++] = 10;
            }
            serialPort.Write(PBuf, 0, point);
       }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值