ESC指令打印图片

来源:互联网 发布:消灭都市wiki排位数据 编辑:程序博客网 时间:2024/05/22 14:19
            Bitmap bmp = bmp = new Bitmap(500, 500);            Graphics g = Graphics.FromImage(bmp);            g.FillRectangle(Brushes.White, 0, 0, 580, 580);            Font f1 = new Font("黑体", 16, FontStyle.Regular);            g.DrawString("3941310", f1, Brushes.Black, 400, 0, new StringFormat());            g.DrawString("028208", f1, Brushes.Black, 400,20, new StringFormat());            pictureBox1.Image = bmp;                        int escType = 1;            byte[] data = new byte[] { 0x1B, 0x33, 0x00 };            lc.Write(data);            data[0] = (byte)'\x00';            data[1] = (byte)'\x00';            data[2] = (byte)'\x00';    // Clear to Zero.            Color pixelColor;            // ESC * m nL nH            byte[] escBmp = new byte[] { 0x1B, 0x2A, 0x00, 0x00, 0x00 };            escBmp[2] = (byte)'\x21';            escType = 3;            //nL, nH            escBmp[3] = (byte)(bmp.Width % 256);            escBmp[4] = (byte)(bmp.Width / 256);            // data            for (int i = 0; i < (bmp.Height / 24) + 1; i++)            {                lc.Write(escBmp);                for (int j = 0; j < bmp.Width; j++)                {                    for (int k = 0; k < 24; k++)                    {                        if (((i * 24) + k) < bmp.Height)   // if within the BMP size                        {                            pixelColor = bmp.GetPixel(j, (i * 24) + k);                            if (pixelColor.R == 0)                            {                                data[k / 8] += (byte)(128 >> (k % 8));                            }                        }                    }                    lc.Write(data);                    data[0] = (byte)'\x00';                    data[1] = (byte)'\x00';                    data[2] = (byte)'\x00';    // Clear to Zero.                }                lc.Write("\n");            } // data            //lc.Write("\n完成<GS *>方式的位图下载!\n");            lc.Write(cutPaper);