C # parallel port thermal invoice printer print bitmap including chip writing,

Source: Internet
Author: User

C # parallel port thermal invoice printer print bitmap including chip writing,

 

The following is the code to be called:

Class LptControl {private string LptStr = "lpt1"; public LptControl (string l_LPT_Str) {LptStr = l_LPT_Str;} [StructLayout (LayoutKind. sequential)] private struct OVERLAPPED {int Internal; int InternalHigh; int Offset; int OffSetHigh; int hEvent;} // Call DLL. [DllImport ("kernel32.dll")] private static extern int CreateFile (string lpFileName, uint dwDesiredAccess, int dw1_mode, int lpSecurityAttr Ibutes, int dwCreationDisposition, int interval, int hTemplateFile); [DllImport ("kernel32.dll")] private static extern bool WriteFile (int hFile, byte [] lpBuffer, int nNumberOfBytesToWrite, ref int lpNumberOfBytesWritten, ref OVERLAPPED lpOverlapped); [DllImport ("kernel32.dll")] private static extern bool CloseHandle (int hObject); private int iHandle; /// <summary> /// open the port /// </summar Y> // <returns> </returns> public bool Open () {iHandle = CreateFile (LptStr, 0x40000000, 0, 0, 3, 0, 0 ); // iHandle = CreateFile (LptStr, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); if (iHandle! =-1) {return true;} else {return false ;}/// <summary> // print the string, you can call this method to print the required string /// </summary> /// <param name = "Mystring"> </param> /// <returns> </returns> public bool Write (String Mystring) {// if the port is opened, a prompt is displayed, and if (iHandle! =-1) {OVERLAPPED x = new OVERLAPPED (); int I = 0; // byte [] mybyte = System. text. encoding. default. getBytes (Mystring); byte [] mybyte = Encoding. getEncoding ("GB2312 "). getBytes (Mystring); bool B = WriteFile (iHandle, mybyte, mybyte. length, ref I, ref x); return B;} else {throw new Exception ("cannot connect to the printer! ") ;}//< Summary> // print command. You can use parameters to print some commands of the ticket printer, such as line feed, line spacing, and print bitmap. /// </Summary> /// <param name = "mybyte"> </param> /// <returns> </returns> public bool Write (byte [] mybyte) {// if the port is opened, a prompt is displayed, and if (iHandle! =-1) {OVERLAPPED x = new OVERLAPPED (); int I = 0; return WriteFile (iHandle, mybyte, mybyte. length, ref I, ref x);} else {throw new Exception ("cannot connect to the printer! ") ;}/// <Summary> // Close the port /// </summary> /// <returns> </returns> public bool Close () {return CloseHandle (iHandle );}}


 

Because we mainly print the barcode and QR code here, we used the barcode and QR code as an example to write a small calling program (Here we paste the method of printing the image ):

 

/// <Summary> /// Method for printing the image /// </summary> public void PrintOne () {// obtain the image Bitmap bmp = new Bitmap (pictureBox1.Image ); // set the line spacing of characters to n-point rows // byte [] data = new byte [] {0x1B, 0x33, 0x00 }; string send = "" + (char) (27) + (char) (51) + (char) (0); byte [] data = new byte [send. length]; for (int I = 0; I <send. length; I ++) {data [I] = (byte) send [I];} lc. write (data); data [0] = (byte) '\ x00'; data [1] = (byte)' \ x00'; Data [2] = (byte) '\ x00'; // Clear to Zero. Color pixelColor; // ESC * m nL nH d1... Dk select bitmap mode // ESC * m nL nH 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); // print the image in pixels after the loop. // for (int I = 0; I <(bmp. height/24 + 1); I ++ {// sets the bitmap mode to lc. write (escBmp); // loop width 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) ;}}// write a data at a time, 24-pixel lc. write (data); data [0] = (byte) '\ x00'; data [1] = (byte)' \ x00'; data [2] = (byte) '\ x00'; // Clear to Zero .} // line feed, print the second line byte [] data2 = {0xA}; lc. write (data2);} // data lc. write ("\ n ");}


 

During the printing process, a relatively low-level error occurs. Because the ticket printer is connected while my computer is a serial port, I have been remotely testing on another computer, therefore, a horizontal line exists in the middle of the printed image. This problem has been solved for more than half a day, because I have always considered the possibility of a loop in the printed image, so by the way, I sorted out the principles of printing images (the previous cycle was found on the Internet, so I felt that there was no problem and I didn't study it in detail ). Here is my understanding:

This is the command to print the bitmap (each printer will provide such instructions, which can be downloaded directly ):

1. ESC * m nL nH d1... Dk select bitmap mode

Format: ASCII: ESC * m nL nH d1... Dk

Decimal: [27] [42] m nL nH d1... Dk

Hexadecimal: [1BH] [2AH] m nL nH d1... Dk

Note:

. Set the bitmap method (m), points (nL, nH), and bitmap content (dk ).

. M = 255, 32, 33; 0 ≤ nL ≤ 255, 0 ≤ nH ≤ 3, 0 ≤ d ≤.

K = nL + nH X 256 (m = 256); k = (nL + nH X) x 3 (m = 32, 33 ).

. The number of points in the horizontal direction is (nL + nH × 256 ).

. If the number of points exceeds one row, the part that exceeds the maximum number of points (related to the selected bitmap method, see the table below) is ignored.

. D is the bitmap Data byte. If the corresponding bit is 1, The point is printed. If the corresponding bit is 0, the point is not printed. (K indicates the number of data records)

. M is used to select the bitmap method.

 

M

Mode

Vertical

Horizontal

Points

Resolution

Resolution

Data Count (k)

0

8-Point single Density

8

67 DPI

100 DPI

NL + nH ×256

1

8-point dual density

8

67 DPI

200 DPI

NL + nH ×256

32

24-Point single Density

24

200 DPI

100 DPI

(NL + nH X 256) x 3

33

24-point dual-density

24

200 DPI

200 DPI

(NL + nH X 256) x 3

 

 

 

 

 

 

 

The printer used this time is printed at a dual density of 24 points, so I will only explain the situation of m = 33 here.

From the code, we can see that the process of printing images is mainly through loop printing, through

Lc. Write (data );

Loop writing, of course, the above lc. Write (escBmp) is mainly the ESC * m three parameters which are easy to understand and will not be explained much. Data is a 3-byte array. What does data play in printing?

In the printer m = 33 mode, 24 dots are printed vertically each time, that is, byte is 8 bytes, therefore, three byte trees are required to print bitmap in the 24-point double-Password mode. The three characters are used to combine an image with a width of 24 pixels and a length of 24 pixels, then, the cycle width is used to print the image with a width of 24 pixels and a height of 24 pixels at a time. Then, the completed image is printed.

Image to be printed:

 

The first cycle is a 24-pixel high

 

Split the width, cycle the width of each pixel, and print the image with the width of each pixel:

For example, assume that the array data [d1, d2, d3], d1 = 00000111, d2 = 11111111, d3 = 11111111, so the printed pixel width is, the 24-pixel high image is:

 

Finally, the final bitmap is shown through the cycle width and height.

Here is an example of the 24-point density. If you are interested in research, you will often see this Code:

 

 

for (int i = 0; i < ((bmp.Height + 7) / 8); i++) { _serialPort.Write(escBmp, 0, escBmp.Length);  for (int j = 0; j < bmp.Width; j++) { for (int k = 0; k < 8; k++) { if (((i * 8) + k) < bmp.Height) // if within the BMP size { pixelColor = bmp.GetPixel(j, (i * 8) + k); if (pixelColor.R == 0) { data[0] += (byte)(128 >> k); } } }  _serialPort.Write(data, 0, 1); data[0] = (byte)'\x00'; // Clear to Zero. } 


 

This is obviously the 8-point density mode, so its data length is 1, that is, 8 bytes is enough.

The output is quite good.

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.