By default, bar code printers cannot print Chinese characters, but bar code printers support uploading custom fonts. However, this library is definitely not the same as the font library in windows, at first, I thought it was best to print Chinese characters by uploading fonts. But after a rough research, I found this was too complicated and the library files were not available, so I changed the direction, you can use Chinese characters to print Chinese characters.
Therefore, the general process is: Draw the Chinese characters to be printed on the server through GDI into an image, and then serialize the image into a string according to the requirements of the barcode printer and upload it to the printer, finally, print the image by using the Print Image command.
The specific implementation method is as follows:
1. Drawing.
Protected bitmap createimage (string data, font f) {If (string. isnullorempty (data) return NULL; var TXT = new Textbox (); TXT. TEXT = data; TXT. font = f; // TXT. preferredsize. height can only get the height of a row (margin) // Therefore, multiply the number of rows, but you must first subtract the margin and then add the margin. // 5 is the margin var image = new Bitmap (txt. preferredsize. width, (txt. preferredsize. height-5) * TXT. lines. length + 5); var G = graphics. fromimage (image); var B = new system. drawing. drawing2d. lineargradientbrush (New rectangle (0, 0, image. width, image. height), color. black, color. black, 1.2f, true); G. clear (system. drawing. color. white); G. drawstring (data, F, B, 1, 1); Return image ;}
Input the Chinese characters and fonts to be printed as parameters to obtain an image.
2. Convert and serialize the image.
protected string ConvertImageToCode(Bitmap img) { var sb = new StringBuilder(); long clr = 0, n = 0; int b = 0; for (int i = 0; i < img.Size.Height; i++) { for (int j = 0; j < img.Size.Width; j++) { b = b * 2; clr = img.GetPixel(j, i).ToArgb(); string s = clr.ToString("X"); if (s.Substring(s.Length - 6, 6).CompareTo("BBBBBB") < 0) { b++; } n++; if (j == (img.Size.Width - 1)) { if (n < 8) { b = b * (2 ^ (8 - (int)n)); sb.Append(b.ToString("X").PadLeft(2, '0')); b = 0; n = 0; } } if (n >= 8) { sb.Append(b.ToString("X").PadLeft(2, '0')); b = 0; n = 0; } } sb.Append(System.Environment.NewLine); } return sb.ToString();}
This is a bitmap supported by converting an image to a bar code printer.
3. upload images through zpl:
var t = ((img.Size.Width / 8 + ((img.Size.Width % 8 == 0) ? 0 : 1)) * img.Size.Height).ToString(); var w = (img.Size.Width / 8 + ((img.Size.Width % 8 == 0) ? 0 : 1)).ToString(); string zpl = string.Format("~DGR:imgName.GRF,{0},{1},{2}", t, w, imgCode);
Here, IMG is the result returned by the createimage function. imgcode is the result returned by the convertimagetocode function. imgname is the name of the image.
In this way, you can print Chinese characters by adding a drawing command to zpl.
For example, the following sample code:
^ XA
^ Fo10, 10
^ Xgr: imgname. GRF, 1, 1 ^ FS
^ Xz
For more information about how to send zpl commands to a printer, see my previous article:
Http://www.cnblogs.com/Moosdau/archive/2009/10/16/1584627.html