由于項(xiàng)目需要使用EPSON微型打印機(jī)打印LOGO,在網(wǎng)上查看了很多都是安裝驅(qū)動(dòng)然后設(shè)置打印機(jī)圖片。
項(xiàng)目需要使用Bytes發(fā)送流,最后在網(wǎng)上找到了一些資料,記錄下來(lái)方便自己和其他人使用。
public byte[] GetLogo(string LogoPath) { List<byte> byteList = new List<byte>(); if (!File.Exists(LogoPath)) return null; BitmapData data = GetBitmapData(LogoPath); BitArray dots = data.Dots; byte[] width = BitConverter.GetBytes(data.Width); int offset = 0; MemoryStream stream = new MemoryStream(); //for (int i = 0; i < 12; i++) //{ // byteList.Add(32); // 32 空格 //} // BinaryWriter bw = new BinaryWriter(stream);
// 初始化指令 byteList.Add(Convert.ToByte(Convert.ToChar(0x1B)));
// 圖片居中指令 byteList.Add(97); byteList.Add(1); //bw.Write((char)); byteList.Add(Convert.ToByte('@')); //bw.Write('@'); byteList.Add(Convert.ToByte(Convert.ToChar(0x1B))); // bw.Write((char)0x1B); byteList.Add(Convert.ToByte('3')); //bw.Write('3'); //bw.Write((byte)24); byteList.Add((byte)24); while (offset < data.Height) { byteList.Add(Convert.ToByte(Convert.ToChar(0x1B))); byteList.Add(Convert.ToByte('*')); //bw.Write((char)0x1B); //bw.Write('*'); // bit-image mode byteList.Add((byte)33); //bw.Write((byte)33); // 24-dot double-density byteList.Add(width[0]); byteList.Add(width[1]); //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)); } byteList.Add(slice); //bw.Write(slice); } } offset += 24; byteList.Add(Convert.ToByte(0x0A)); //bw.Write((char)); } // Restore the line spacing to the default of 30 dots. byteList.Add(Convert.ToByte(0x1B)); byteList.Add(Convert.ToByte('3')); //bw.Write('3'); byteList.Add((byte)30); return byteList.ToArray(); //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 = 200;//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; } }
使用以上代碼就可以在EPSON等微型打印機(jī)上打印LOGO。
可以參考一下博客的打印指令,根據(jù)實(shí)際情況進(jìn)行調(diào)整。
https://blog.csdn.net/kenneth95/article/details/54341887