///   <summary>
/// POSPrinter的摘要说明。
/// 此类处理网络打印,使用了IP端口.
/// </summary>
public class NetPOSPrinter
{
string ipPort = "192.168.1.212"; public NetPOSPrinter()
{ } public NetPOSPrinter(string IpPort)
{
this.ipPort = IpPort;//打印机端口
} /// <summary>
/// 输出文字到打印机
/// </summary>
/// <param name= "str "> 要打印的内容 </param>
public void PrintLine(string str)
{
//建立连接
IPAddress ipa = IPAddress.Parse(ipPort);
IPEndPoint ipe = new IPEndPoint(ipa, );//9100为小票打印机指定端口
Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
soc.Connect(ipe); //string str= "hello,123456789,大家好! "; byte[] b = System.Text.Encoding.GetEncoding("GB2312").GetBytes(str);
soc.Send(b);
soc.Close();
} public void PrintPic(Bitmap bmp)
{
//把ip和端口转化为IPEndPoint实例
IPEndPoint ip_endpoint = new IPEndPoint(IPAddress.Parse(ipPort), );
//创建一个Socket
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//连接到服务器
socket.Connect(ip_endpoint);
//应对同步Connect超时过长的办法,猜测应该是先用异步方式建立以个连接然后,
//确认连接是否可用,然后报错或者关闭后,重新建立一个同步连接
//socket.SendTimeout = 1000; //初始化打印机,并打印
Byte[] byte_send = Encoding.GetEncoding("gb18030").GetBytes("\x1b\x40");
//发送测试信息
socket.Send(byte_send, byte_send.Length, ); byte[] data = new byte[] { 0x1B, 0x33, 0x00 };
socket.Send(data, data.Length, );
data[] = (byte)'\x00';
data[] = (byte)'\x00';
data[] = (byte)'\x00'; // Clear to Zero. Color pixelColor;
// ESC * m nL nH 点阵图
byte[] escBmp = new byte[] { 0x1B, 0x2A, 0x00, 0x00, 0x00 };
escBmp[] = (byte)'\x21';
//nL, nH
escBmp[] = (byte)(bmp.Width % );
escBmp[] = (byte)(bmp.Width / );
// data
for (int i = ; i < (bmp.Height / ) + ; i++)
{
socket.Send(escBmp, escBmp.Length, ); for (int j = ; j < bmp.Width; j++)
{
for (int k = ; k < ; k++)
{
if (((i * ) + k) < bmp.Height) // if within the BMP size
{
pixelColor = bmp.GetPixel(j, (i * ) + k);
if (pixelColor.R == )
{
data[k / ] += (byte)( >> (k % ));
}
}
}
socket.Send(data, , );
data[] = (byte)'\x00';
data[] = (byte)'\x00';
data[] = (byte)'\x00'; // Clear to Zero.
}
byte_send = Encoding.GetEncoding("gb18030").GetBytes("\n");
//发送测试信息
socket.Send(byte_send, byte_send.Length, );
} // data byte_send = Encoding.GetEncoding("gb18030").GetBytes("\n");
//发送测试信息
socket.Send(byte_send, byte_send.Length, );
socket.Close();
} /// <summary>
/// 打开钱箱
/// </summary>
public void OpenCashBox()
{
IPAddress ipa = IPAddress.Parse(ipPort);
IPEndPoint ipe = new IPEndPoint(ipa, );//9100为小票打印机指定端口
Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
soc.Connect(ipe);
char[] c = { Convert.ToChar(), 'p', Convert.ToChar(), Convert.ToChar(), Convert.ToChar() };
byte[] b = System.Text.Encoding.GetEncoding("GB2312").GetBytes(c);
soc.Send(b);
soc.Close();
}
}

C# 使用IP端口网络打印图片的更多相关文章

  1. android 登录和设置IP/端口功能

    本人第一个Android开发功能:登录以及设置IP/端口. 本人是j2ee开发工程师,所以这个可能有一些处理不太完善的地方,欢迎评论在下面,我会认真改进的. 首先是配置strings.xml文件添加用 ...

  2. 本机ip+端口不能访问web server,外部却可以访问

    本机ip+端口不能访问web server,外部却可以访问! 这个奇葩的问题困扰了我好久,别人通过ip访问我的server一切正常,自己却访问不了,一度怀疑win10的问题,久寻无果! 最后关闭ads ...

  3. Tomcat8 localhost+端口可以访问Manager APP,而IP+端口不可以访问 解决办法

    localhost + 端口可以正常访问Manager APP,而IP + 端口不能访问Manager APP,报403错误.(我的主机环境是Ubuntu16.04) 前提是你已经配好了tomcat_ ...

  4. (转)建站知识:域名/ 空间/ IP/ 端口之间的关系

    先说域名解析吧,比如说你的域名是 www.sunnymould.com,这个域名对应着一个IP地址,域名解析就是把上面的域名转换成这个IP地址的过程,这样你就可以域名访问了上面地址上的内容了. 端口映 ...

  5. 避免 TCP/IP 端口耗尽

    转载:http://www.cnblogs.com/tianzhiliang/archive/2011/06/27/2091214.html 当客户端启动到服务器的 TCP/IP 套接字连接时,客户端 ...

  6. nginx代理tomcat后,tomcat获取真实(非proxy,非别名)nginx服务端ip端口的解决方案

    nginx代理tomcat后,tomcat获取服务端ip端口的解决方案 1.注意修改nginx配置代理,标红地方 #user nginx; worker_processes ; error_log l ...

  7. Nginx配置二级目录/路径 映射不同的反向代理和规避IP+端口访问

       当配置Nginx来映射不同的服务器 可以通过二级路径来反向代理 来解决一个外网端口实现多个服务访问. 配置如下: server { listen ; server_name demo.domai ...

  8. nginx配置二级目录,反向代理不同ip+端口

    场景描述: 通过二级目录(虚拟目录,应用程序)的方式访问同一ip+端口的不同应用,例如location是用户使用页面,location/admin/是管理页面,location部署在192.168.1 ...

  9. linux下使用iptables统计ip/端口流量

    1.添加ip/端口的流量统计 入网流量: iptables -A INPUT -d 出网流量: iptables -A OUTPUT -s 2.查看流量统计信息 iptables -L -v -n - ...

随机推荐

  1. 【mysql】更新“”空字符串为NULL

    mysql中NULL和“”是完全不同的概念,因此出现了“”导致NULL不可查的问题出现,那怎么能将一个字段设置为NULL呢? update goods_type gt set gt.parent_ui ...

  2. 【JSP EL】<c:if> <c:foreach >EL表达式 获取list长度/不用循环,EL在List中直接获取第一项的内容/EL获取Map的键,Map的值

    1.EL表达式 获取list长度 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" ...

  3. Tile based Rendering 二 tbr and tbdr 优化建议tiled based deferred rendering

    http://www.seas.upenn.edu/~pcozzi/OpenGLInsights/OpenGLInsights-TileBasedArchitectures.pdf tbr 和tbdr ...

  4. 【Web】Django OAuth invalid_grant error

    This can happen if the code is expired. They expire pretty quickly by default. You can look up your ...

  5. promise对象解决回调地狱

    先放一张图片感受一下回调地狱 看起来是真的很让人头痛的东西 而现在我这里使用promise对象来解决回调地狱 采用链式的 then,可以指定一组按照次序调用的回调函数. 这时,前一个 then 里的一 ...

  6. Redis Cluster集群的搭建

    redis集群搭建原理: redis是单线程,但是一般的作为缓存使用的话,redis足够了,因为它的读写速度太快了.   官方的一个简单测试: 测试完成了50个并发执行100000个请求. 设置和获取 ...

  7. Fireworks层与蒙版的概念和用法

    添加热点也是可以嵌套的 切片工具将自动保存在网页图层,并且可以导出为图像 组合为蒙版就是让一部分图形显示的填充为一幅图片的东西,删除蒙版即可将其转换为一个普通的图层,否则还可以移动位置

  8. 使用Firefly编写简易聊天室

    1.创建工程命令行下输入firefly-admin.py createproject chat_rooms(linux在终端输入),<ignore_js_op> firefly会在C盘Us ...

  9. Why do Antennas Radiate?

    Obtaining an intuitive idea for why antennas radiate is helpful in understanding the fundamentals of ...

  10. 操作系统CPU调度知识点

    1.进程基本概念:进程是程序的一次运行. 是系统进程资源分配和调度的基本单位. 2.进程三态:运行状态.就绪状态.堵塞状态.三态转换规则,就绪状态的进程因为调度进程运行状态,运行状态因为时间片用完而进 ...