1. 程式人生 > >C#熱敏列印圖片 串列埠列印圖片

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);
       }