Epson 微型打印机打印 LOGO C#

时间:2022-07-11 14:42:56

  由于项目需要使用EPSON微型打印机打印LOGO,在网上查看了很多都是安装驱动然后设置打印机图片。

项目需要使用Bytes发送流,最后在网上找到了一些资料,记录下来方便自己和其他人使用。

        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 = ;
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();
byteList.Add();
//bw.Write((char));
byteList.Add(Convert.ToByte('@'));
//bw.Write('@');
byteList.Add(Convert.ToByte(Convert.ToChar(0x1B)));
// bw.Write((char)0x1B);
byteList.Add(Convert.ToByte(''));
//bw.Write('3');
//bw.Write((byte)24);
byteList.Add((byte));
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));
//bw.Write((byte)33); // 24-dot double-density
byteList.Add(width[]);
byteList.Add(width[]);
//bw.Write(width[0]); // width low byte
//bw.Write(width[1]); // width high byte for (int x = ; x < data.Width; ++x)
{
for (int k = ; k < ; ++k)
{
byte slice = ;
for (int b = ; b < ; ++b)
{
int y = (((offset / ) + k) * ) + 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 ? : ) << ( - b));
}
byteList.Add(slice);
//bw.Write(slice);
}
}
offset += ;
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(''));
//bw.Write('3');
byteList.Add((byte));
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 = ;
var index = ;
double multiplier = ;//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 = ; y < xheight; y++)
{
for (var x = ; 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等微型打印机上打印LOGO。

可以参考一下博客的打印指令,根据实际情况进行调整。

https://blog.csdn.net/kenneth95/article/details/54341887