C# 热敏打印机 Socket 网络链接 打印 图片 (二)

 IPAddress ip = IPAddress.Parse("192.168.1.212");
IPEndPoint iport = new IPEndPoint(ip, );//9100为小票打印机指定端口
Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
soc.Connect(iport);
bitmap = new Bitmap(@"D:\300X200.bmp");
soc.Send(bmpToByte(bitmap));
soc.Close();
 public static byte[] bmpToByte(Bitmap bmp)
{
int h = bmp.Height / + ;
int w = bmp.Width;
byte[][] all = new byte[ + * h + h * w][]; all[] = new byte[] { 0x1B, 0x33, 0x00 }; Color pixelColor;
// ESC * m nL nH 点阵图
byte[] escBmp = new byte[] { 0x1B, 0x2A, 0x21, (byte)(w % ), (byte)(w / ) }; // 每行进行打印
for (int i = ; i < h; i++)
{
all[i * (w + ) + ] = escBmp;
for (int j = ; j < w; j++)
{
byte[] data = new byte[] { 0x00, 0x00, 0x00 };
for (int k = ; k < ; k++)
{
if (((i * ) + k) < bmp.Height)
{
pixelColor = bmp.GetPixel(j, (i * ) + k);
if (pixelColor.R == )
{
data[k / ] += (byte)( >> (k % ));
}
}
}
all[i * (w + ) + j + ] = data;
}
//换行
all[(i + ) * (w + )] = PrinterCmdUtils.nextLine();
}
all[h * (w + ) + ] = PrinterCmdUtils.nextLine(); return byteMerger(all);
}
 public static byte[] byteMerger(byte[][] byteList)
{
int Length = ;
for (int i = ; i < byteList.Length; i++)
{
Length += byteList[i].Length;
}
byte[] result = new byte[Length]; int index = ;
for (int i = ; i < byteList.Length; i++)
{
byte[] nowByte = byteList[i];
for (int k = ; k < byteList[i].Length; k++)
{
result[index] = nowByte[k];
index++;
}
}
return result;
}

转自:http://www.cnblogs.com/rinack/p/4838963.html

上一篇:#Java学习之路——基础阶段二(第三篇)


下一篇:MySQL时间戳相互转换