The project needs to use a serial thermal printer, so study the serial printing.
Using system;using system.linq;using system.collections.generic;using system.text;using System.IO.Ports;using System.collections;using system.windows.forms;using system.drawing;namespace printertest{public class Printer { private static SerialPort SerialPort; Serial port Public Printer () {try {////serial Port initialization SerialPort = new SerialPort (); Serialport.portname = "COM2"; Serialport.baudrate = 9600;//Baud rate serialport.parity = system.io.ports.parity.odd;//parity check serial Port.stopbits = system.io.ports.stopbits.one;//Stop bit openport (); } catch {MessageBox.Show (e.tostring ()); }} private void Openport () {if (null! = SerialPort) {Try {if (!serialport.isopen) Serialport.open (); } catch {MessageBox.Show (e.tostring ()); }}} private void Closeport () {try {if (serialPort . IsOpen) Serialport.close (); Serialport.dispose (); } catch {MessageBox.Show (e.tostring ()); }} public void Write (byte[] data,int len) {try {SERIALPORT.WR ITE (data, 0, Len); } catch (Exception e) {MessageBox.Show (e.tostring ()); }}///<summary> send commands to the printer///</summary>//<param name= "Data" > Command data </param> public void Write (params byte[] data) {try {seria Lport.write (data, 0,data. Length); } catch (Exception e) {MessageBox.Show (e.tostring ()); }}///<summary>///Send string///</summary>//<param name= "Strbuf" > To String sent </param> public void Write (string strbuf) {try {byte[] da Ta = Tohex (strbuf, "GB2312"); Serialport.write (data, 0, data. Length); } catch (Exception e) {MessageBox.Show (e.tostring ()); }}///<summary>//Convert string to byte[]///</summary>//<param name= "str" > Target string </param>//<param name= "charset" > Encoding mode </param>//<returns> converted Byte[ ]</returns> Private byte[] Tohex (String str, string charset)//{if (str. Length% 2 = 0) str + = ""; Encoding enc = encoding.getencoding (charset); Return ENC. GetBytes (str); }///<summary>///Picture taking///</summary>//<param name= "BMP" ></param> <returns></returns> private byte[,] getbytesbybmp (Bitmap bmp) {//bitarra Y storage pixel point bool[,] BitArray = new bool[bmp. Height% 8 = = 0? Bmp. Height: (BMP. HEIGHT/8 + 1) * 8, BMP. Width]; for (int i = 0; i < BMP. Width; ++i)//Gets the picture dot matrix {for (int j = 0; J < BMP. Height; ++J) {//Get point ARGB Color pixel = bmp. GetPixel (i, j); Double Gray = pixel. R * 0.299 + pixel. G * 0.587 + pixel. B * 0.114; if (Gray < 192)//Is Dark bitarray[j, i] = true; else//non-Dark (light) bitarray[j, i] = false; }} byte[,] res =//storage font New Byte[bitarray.getlength (0)/8, Bitarray.getlength (1)]; int resrow = 0, rescol = 0; for (int i = 0; i < BMP. Height; i = i + 8) {rescol = 0; for (int j = 0; J < BMP. Width; ++J) {byte b = 0; Binary Turn decimal for (int k = (i + 7), p = 0; k > i-1;--k) {b + = (byte) ((Bitarray[k, j]? 1:0) * MATH.POW (2, p++)); } res[resrow, rescol++] = b; } ++resrow; } return res; }///<summary>///print Pictures///</summary>//<param name= "BMP" > Picture </PARAM&G T <param name= "Enlarge" > Magnification </param> public void printimage (Bitmap bmp) {byte[,] g Image = getbytesbybmp (BMP); for (int i = Gimage.getlengtH (0)-1; I >= 0; -i) {byte Lowlen = (Byte) ((Gimage.getlength (1) << 8) >> 8); byte Highlen = (byte) (Gimage.getlength (1) >> 8); Write (0x1B, 0x4B, Lowlen, Highlen); for (int j = 0; J < gimage.getlength (1); j + +) Write (Gimage[i, j]); Write (0x0D); } } }}
Call Method:
Printer p = new Printer ();//Generate Instance P. Write (myarray,myarray.lenth); Send fixed-length array p. Write (0x1B, 0x40);//Send indefinite long number string p. Write ("Send string");//Send String P.printimage (new Bitmap ());//Send picture
C # serial Serial printing