private string PrintNewList(List <PrintLineModel> lines) { if (lines == null || lines.Count == 0) { throw new Exception("[Pos 1000] 无打印数据!"); } string appConfig = Utils.GetAppConfig("LptStr"); this.lc = new LptControl(appConfig); if (!this.lc.Open()) { throw new Exception("[Pos 1001] 设备驱动失败!"); } try { LogTools.Info("开始打印"); #region 循环打印 for (int i = 0; i < lines.Count; i++) { PrintLineModel m = lines[i]; ////LPT打印,不用手动隔行,因为它不会重叠 switch (m.type) { case 1: //图片和条形码都视作图片打印 case 2: this.lc.Write(new byte[] { 27, 64 }); this.lc.Write(new byte[] { 27, 33, 48 }); this.lc.Write(new byte[] { 27, 97, 1 }); //打印图片或条形码 try { this.Print2Bitmap(m.content, "tmpBmp.bmp", m.splitLine); } catch (Exception ex) { string sErr = string.Format("[Pos 1003] 打印图片{0},下载地址:{2}。\r\n异常:{1}" , "tmpBmp.bmp", ex.ToString(), m.content); //throw new Exception(); LogTools.Debug(sErr); } LptControl arg_164_0 = this.lc; byte[] array = new byte[3]; array[0] = 27; array[1] = 33; arg_164_0.Write(array); LptControl arg_186_0 = this.lc; byte[] array2 = new byte[3]; array2[0] = 27; array2[1] = 97; arg_186_0.Write(array2); //this.lc.Write(Environment.NewLine); break; case 3: StringBuilder sb = new StringBuilder(); switch (m.fontSize) { case 1: sb.Append(_1w1h); break; case 2: sb.Append(_1w2h); break; case 3: sb.Append(_2w2h); break; default: break; } sb.Append(m.content + Environment.NewLine); this.lc.Write(sb.ToString()); //LogTools.Info(string.Format("打印第{0}行,内容:{1}", i.ToString(), sb.ToString())); for (int j = 0; j < m.splitLine - 2; i++) { this.lc.Write(" " + Environment.NewLine); } break; default: break; } } #endregion #region 打印尾巴 this.lc.Write(" " + Environment.NewLine); this.lc.Write(" " + Environment.NewLine); this.lc.Write(" " + Environment.NewLine); this.lc.Write(" " + Environment.NewLine); this.lc.Write(" " + Environment.NewLine); this.lc.Write(" " + Environment.NewLine); this.lc.Write(new byte[] { 27, 100, 3 }); this.lc.Write(new byte[] { 27, 109 }); LogTools.Info("打印完毕,切纸"); #endregion return(""); } catch (Exception ex4) { throw new Exception("[Pos 1005] 打印失败," + ex4.Message); } finally { this.lc.Close(); } }
public void PrintCode(Bitmap bmp) { if (true) { //设置字符行间距为n点行 //byte[] data = new byte[] { 0x1B, 0x33, 0x00 }; string send = "" + (char)(27) + (char)(51) + (char)(0); byte[] data = new byte[send.Length]; for (int i = 0; i < send.Length; i++) { data[i] = (byte)send[i]; } lc.Write(data); data[0] = (byte)'\x00'; data[1] = (byte)'\x00'; data[2] = (byte)'\x00'; // Clear to Zero. Color pixelColor; //ESC * m nL nH d1…dk 选择位图模式 // ESC * m nL nH byte[] escBmp = new byte[] { 0x1B, 0x2A, 0x00, 0x00, 0x00 }; escBmp[2] = (byte)'\x21'; //nL, nH escBmp[3] = (byte)(bmp.Width % 256); escBmp[4] = (byte)(bmp.Width / 256); //循环图片像素打印图片 //循环高 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)); } } } //一次写入一个data,24个像素 lc.Write(data); data[0] = (byte)'\x00'; data[1] = (byte)'\x00'; data[2] = (byte)'\x00'; // Clear to Zero. } //换行,打印第二行 byte[] data2 = { 0xA }; lc.Write(data2); } // data lc.Write("\n\n"); } else { byte[] array = new byte[3]; array[0] = 27; array[1] = 51; byte[] array2 = array; this.lc.Write(array2); array2[0] = 0; array2[1] = 0; array2[2] = 0; byte[] array3 = new byte[] { 27, 42, 33, 0, 0 }; array3[3] = (byte)(bmp.Width % 256); array3[4] = (byte)(bmp.Width / 256); for (int i = 0; i < bmp.Height / 24 + 1; i++) { this.lc.Write(array3); for (int j = 0; j < bmp.Width; j++) { for (int k = 0; k < 24; k++) { if (i * 24 + k < bmp.Height && bmp.GetPixel(j, i * 24 + k).R == 0) { byte[] expr_B2_cp_0 = array2; int expr_B2_cp_1 = k / 8; expr_B2_cp_0[expr_B2_cp_1] += (byte)(128 >> k % 8); } } this.lc.Write(array2); array2[0] = 0; array2[1] = 0; array2[2] = 0; } byte[] mybyte = new byte[] { 10 }; this.lc.Write(mybyte); } } }