Using system;using system.collections.generic;using system.drawing;using system.linq;using System.Text;using System.net;using system.net.sockets;namespace posprinter{///<summary>///Posprinter Summary description. This class handles network printing and uses an IP port. </summary> public class Netposprinter {string ipport = "127.0.0.1"; Public Netposprinter () {} public Netposprinter (string ipport) {This.ipport = Ippo rt;//Printer Port}///<summary>///output text to printer///</summary>//< param name= "str" > content to print </param> public void PrintLine (string str) {//Establish connection IPAddress IPA = ipaddress.parse (ipport); IPEndPoint ipe = new IPEndPoint (IPA, 9100);//9100 specifies the port for the small ticket printer socket SOC = new socket (AddressFamily.InterNetwork, SocketType.Stream, PROTOCOLTYPE.TCP); Soc. Connect (IPE); String Str= "hello,123456789, Hello! "; Byte[] B = System.Text.Encoding.GetEncoding ("GB2312"). GetBytes (str); Soc. Send (b); Soc. Close (); } public void Printpic (Bitmap bmp) {//IP and port converted to IPEndPoint instance IPEndPoint Ip_endpoint = New IPEndPoint (Ipaddress.parse (ipport), 9100); Create a socket socket socket = new socket (addressfamily.internetwork, SocketType.Stream, protocoltype.tcp); Connect to the server socket. Connect (Ip_endpoint); Should the synchronous connect timeout be too long, guess should be the asynchronous way to establish a connection then,//Confirm the connection is available, then error or shut down, re-establish a synchronization connection//sock Et. sendtimeout = 1000; Initialize the printer and print byte[] byte_send = encoding.getencoding ("GB18030"). GetBytes ("\x1b\x40"); Send the test information to the socket. Send (Byte_send, Byte_send. Length, 0); byte[] data = new byte[] {0x1B, 0x33, 0x00}; Socket. Send (data, data. Length, 0); Data[0] = (byte) ' \x00 '; DatA[1] = (byte) ' \x00 '; DATA[2] = (byte) ' \x00 '; Clear to Zero. Color Pixelcolor; ESC * M NL nH bitmap byte[] escbmp = new byte[] {0x1B, 0x2A, 0x00, 0x00, 0x00}; ESCBMP[2] = (byte) ' \x21 '; NL, NH escbmp[3] = (byte) (BMP. Width% 256); ESCBMP[4] = (byte) (BMP. WIDTH/256); data for (int i = 0; i < BMP. HEIGHT/24) + 1; i++) {socket. Send (escbmp, escbmp.length, 0); for (int j = 0; J < BMP. Width; J + +) {for (int k = 0; k <; k++) {if ( (i *) + K) < BMP. Height)//If within the BMP size {pixelcolor = bmp. GetPixel (J, (I *) + K); if (PIXELCOLOR.R = = 0) {DATA[K/8] + = (byte) (>> K 8)); }}} socket. Send (data, 3, 0); Data[0] = (byte) ' \x00 '; DATA[1] = (byte) ' \x00 '; DATA[2] = (byte) ' \x00 '; Clear to Zero. } byte_send = encoding.getencoding ("GB18030"). GetBytes ("\ n"); Send the test information to the socket. Send (Byte_send, Byte_send. Length, 0); }//Data Byte_send = encoding.getencoding ("GB18030"). GetBytes ("\ n"); Send the test information to the socket. Send (Byte_send, Byte_send. Length, 0); Socket. Close (); }//<summary>//Open cash drawer///</summary> public void Opencashbox () {IPAddress IPA = ipaddress.parse (ipport); IPEndPoint ipe = new IPEndPoint (IPA, 9100);//9100 specifies the port for the small ticket printer socket SOC = new socket (AddressFamily.InterNetwork, SocketType.Stream, Protocoltype.tcp); Soc. Connect (IPE); Char[] C = {convert.tochar, ' P ', Convert.tochar (0), Convert.tochar, Convert.tochar (255)}; Byte[] B = System.Text.Encoding.GetEncoding ("GB2312"). GetBytes (c); Soc. Send (b); Soc. Close (); } }}
Transferred from: http://www.cnblogs.com/rinack/p/4838211.html
C # Thermal Printer Socket network link print picture (i)