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

最新文章

  1. 数据库模型设计PowerDesigner
  2. 数据库使用数据泵迁移遇到LOB字段
  3. Spring AOP基本概念
  4. 条件随机场matlab程序下载
  5. 代码创建数据库_表--SqlServer数据库
  6. 20Spring_JdbcTemplatem模板工具类
  7. [CareerCup] 5.2 Binary Representation of Real Number 实数的二进制表示
  8. Json.Net学习(1) 实现简单的序列化和反序列化
  9. hdu 4009 最小树形图
  10. HTML5学习(四)---Canvas绘图
  11. poj~1236 Network of Schools 强连通入门题
  12. [模板] 数位dp
  13. [转]Python 的列表解析式,集合解析式,字典解析式
  14. 六.HashMap HashTable HashSet区别剖析总结
  15. php 压缩文件 zip
  16. Kubernetes的ConfigMap解析
  17. sql执行顺序与性能优化小技巧(一)
  18. 【LeetCode】120. Triangle (3 solutions)
  19. leetcode coding base
  20. Spring和MyBatis整合

热门文章

  1. 嵌入式的重要平台 .NET Micro Framework
  2. Javascript判断两个日期是否相等
  3. 基础知识漫谈(2):从设计UI框架开始
  4. 剑指Offer面试题:24.复杂链表的复制
  5. ASP.Net MVC开发基础学习笔记:五、区域、模板页与WebAPI初步
  6. 企业IT管理员IE11升级指南【4】—— IE企业模式介绍
  7. 利用gulp解决前后端分离的header/footer引入问题
  8. 分析nuget源码,用nuget + nuget.server实现winform程序的自动更新
  9. EF连接PostgreSql
  10. Rxjava入门