C # print pictures of the printer with a small ticket

Source: Internet
Author: User

These two days I have been studying the problem of printing pictures on the thermal invoice printer. After the painful process of searching for information, I finally succeeded in the experiment. After some tests and modifications, the code is finally released. This time, it is much easier to print the barcode, QR code, and logo.

This code was changed on Epson's code, and the experiment was successful on the heat-sensitive small pass printer of the SPRT SP-POS58III, and there was no test successful on the Jee GS-58 series. Weituo VT-58G, Jia Bo GP-80XXX, etc., because of the lack of these printers, so did not test in time. The issues on the Jicheng GS-58 series are temporarily unknown. Therefore, if you are a small invoice machine of Jicheng, you may refer to the reference code, but it is not guaranteed to be available.



This code is printed by connecting to the network printer through a socket. It is not tested locally, so it is not sent. The printline method prints characters, while the printpic method prints images. The IP address of the network printer must be specified during initialization.


After testing, it is best to use X for the image of the QR code, and X for the barcode.


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> /// abstract description of posprinter. /// This type of processing network printing uses the IP port. /// </Summary> public class netposprinter {string ipport = "127.0.0.1"; Public netposprinter () {} public netposprinter (string ipport) {This. ipport = ipport; // printer port} /// <summary> // output text to the printer // </Summary> /// <Param name = "str"> content to be printed </param> Public void printline (string Str) {// establish connection IPaddress IPA = IPaddress. parse (ipport); ipendpoint IPE = new ipendpoint (IPA, 9100); // 9100 specifies the 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) {// convert the IP address and port 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); // The method to cope with the synchronization connect timeout is too long. It is suggested that a connection should be established in asynchronous mode and then, // confirm whether the connection is available, and then report an error or close, re-establish a synchronous connection // socket. sendtimeout = 1000; // initialize the printer and print byte [] byte_send = encoding. getencoding ("gb18030 "). getbytes ("\ x1b \ X40"); // send the test information 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 dot matrix 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 <24; k ++) {If (I * 24) + k) <BMP. height) // if within the BMP size {pixelcolor = BMP. getpixel (J, (I * 24) + k); If (pixelcolor. R = 0) {data [k/8] + = (byte) (128 >>> (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 socket. send (byte_send, byte_send.length, 0);} // data byte_send = encoding. getencoding ("gb18030 "). getbytes ("\ n"); // send the test information socket. send (byte_send, byte_send.length, 0); socket. close () ;}/// <summary> /// Open the cash box // </Summary> Public void opencashbox () {IPaddress IPA = IPaddress. parse (ipport); ipendpoint IPE = new ipendpoint (IPA, 9100); // 9100 specify the port socket SOC = new socket (addressfamily. interNetwork, sockettype. stream, protocoltype. TCP); Soc. connect (IPE); char [] C = {convert. tochar (27), 'P', convert. tochar (0), convert. tochar (60), convert. tochar (255)}; byte [] B = system. text. encoding. getencoding ("gb2312 "). getbytes (c); Soc. send (B); Soc. close ();}}}

(If the code prompts that bitmap cannot be recognized, add references for system. Windows. Forms and system. Drawing .)


Call method:

        NetPOSPrinter posPrinter = new NetPOSPrinter(txtIP.Text);        posPrinter.PrintPic(new Bitmap(pictureBox1.Image));

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.