Based on C # printing with a zebra printer in WPF,
Recently, I took over a challenging module in the project-printing the content to be printed with a zebra printer. After two days of hard work, I finally got some gains and sent them to the Internet to cheat the score-_-|
The model of the printer used in the project is gxw.t. when I took over the printer, I knew nothing about it. There is no way to step by step.
First, try to use the PrintVisual and PrintDocument methods in the PrintDialog in WPF. The printer does not respond at all. The final conclusion is that the zebra printer does not support the ms xps document format, therefore, do not think about the driver printing after using WPF for typographical layout. It is impossible !!! This road is broken here.
Then I thought about whether the SDK can be printed directly. I found the technical support for the zebra printer. I was a girl. I mentioned it. The girl said that no development kit was available, then I posted a Website: http://blog.csdn.net/ldljlq/article/details/7338772. Maybe my level is limited. Anyway, I feel that there are not many things, but the key point is to use images or instructions for printing. The first thing I thought of was to use instructions for printing. I went to my sister and asked for the Zebra Technical Manual. When I opened it, I was so scared that Nima had more than 1000 pages, I just used the printer and didn't help you develop the driver for your printer. At that time, I was in conflict. But I couldn't help it. I had to take a look at it. I tried to find the printed command and tried it. I felt quite satisfied when something came out. The only Tangle is some settings of the font and font supported by the printer, such as bold and italic. The information is not found, and I asked the technical support of zebra, you can use italics, but it is unclear whether there are corresponding commands. No way, there are many fonts in the project, and there are all kinds of italics. Anyone who knows how to play WPF, so many attributes are set one by one, so the light font type banma will be confused.
Since direct instruction printing does not work, we need to consider image printing and the image is connected to multimedia. Nima is enough. This is because I used the method mentioned by my brother Ren in the link to obtain the print template command. After the printer driver is installed, manually create a new local port and set the printer port to the new port in the printer settings. Use the software used to create the template of Zebra to create what you want and print it to get the command sequence you want in the port file you created (in fact, the template uses image printing ). When the command sequence is available, you can query the corresponding commands in the reference manual to get what you want.
The image printing mentioned here is not a bitmap or vector image we usually talk about. In the manual, it is an image in GRF format. After carefully studying it, it is actually an image with such a name, the required data is actually an image matrix. In addition, the pixel representation in the image matrix is: one byte represents eight pixels, that is, a bit (0 or 1) represents the color of a pixel (black or white ). Here, I came up with the idea: layout the content to be printed-> convert the typographical data into a bitmap-> convert the data in the bitmap, convert to the format required in the instruction as needed-> Print it to the printer. In this way, there are no printer restrictions on the font itself. There is a way of thinking, and the rest is the method.
The layout is relatively simple. Anyone who has used custom controls knows that DrawingVisual can be used to build the desired Visual. However, converting the Visual into a bitmap makes it difficult for me. After an afternoon, I finally found something on the Internet-RenderTargetBitmap. This class can convert your Visual into a bitmap.
The following is how to convert bitmap data into the image data in the instruction. It is not good at Keke and mathematics. When I analyze the data, I am confused about it for a long time, however, the problem is solved in general. Let's talk about the following ideas:
Speaking of this, it is almost the same. By the way, the printing support in WPF is really powerful, and the operation to pass commands to the printer is also very simple. For details, see the following code.
Here is the source code:
/// <Summary>
/// Obtain the command to draw the Visual
/// </Summary>
/// <Param name = "visual"> obtain the Visual </param>
/// <Param name = "pixelWidth"> pixel width </param>
/// <Param name = "pixelHeight"> pixel height </param>
/// <Param name = "dpiX"> horizontal dpi </param>
/// <Param name = "dpiY"> vertical dpi </param>
/// <Param name = "offsetX"> X-axis offset, in bytes </param>
/// <Param name = "offsetY"> ordinate offset, measured in bytes </param>
/// <Returns> </returns>
Private string GetPrintZPL (Visual visual, int pixelWidth, int pixelHeight, double dpiX, double dpiY, int offsetX, int offsetY)
{
String ret = string. Empty;
// Create an image
RenderTargetBitmap bmp = new RenderTargetBitmap (pixelWidth, pixelHeight, dpiX, dpiY, PixelFormats. Pbgra32 );
# If TEST // test
DrawingVisual newVisual = new DrawingVisual ();
DrawingContext dc = newVisual. RenderOpen ();
Dc. drawEllipse (Brushes. black, new Pen (), new Point (bmp. width/2, bmp. height/2), bmp. width/2, bmp. height/2 );
Dc. Close ();
Visual = null;
Bmp. Render (newVisual );
# Else
Bmp. Render (visual );
# Endif
Byte [] datas = new byte [bmp. PixelWidth * bmp. PixelHeight * 4];
Bmp. CopyPixels (datas, bmp. PixelWidth * 4, 0); // obtain image data
Int rowBytes = (pixelWidth + 7)/8;
Byte [] targetDatas = new byte [rowBytes * bmp. PixelHeight];
For (int I = 0; I <bmp. PixelHeight; I ++) // adjust the data and
{
For (int j = 0; j <bmp. PixelWidth; j ++)
{
Byte blue = datas [I * bmp. PixelWidth * 4 + j * 4 + 0];
Byte green = datas [I * bmp. PixelWidth * 4 + j * 4 + 1];
Byte red = datas [I * bmp. PixelWidth * 4 + j * 4 + 2];
Byte alpha = datas [I * bmp. PixelWidth * 4 + j * 4 + 3];
If (blue = 0 & green = 0 & red = 0)
{
If (alpha = 255) // if alpha is 0, it is transparent.
{
Byte cur = 1;
Cur = (byte) (cur <(7-j % 8 ));
TargetDatas [I * rowBytes + j/8] | = cur;
}
}
Else
{
If (! (Blue = 255 & green = 255 & red = 255 & alpha = 255) // if the value is 255, the color is white.
{
Byte cur = 1;
Cur = (byte) (cur <(7-j % 8 ));
TargetDatas [I * rowBytes + j/8] | = cur;
}
}
}
}
Ret = string. Format ("^ XA ~ TA000 ~ JSN ^ LT0 ^ MNW ^ MTT ^ PON ^ PMN ^ LH0, 0 ^ JMA ^ PR3, 3 ~ SD29 ^ JUS ^ LRN ^ CI0 ^ XZ ~ DG000.GRF, {0}, {1}, {2} ^ XA ^ Mt ^ PW260 ^ LL0189 ^ LS0 ^ FT0, 192 ^ FO {3}, {4}, ^ XG000.GRF, 1, 1 ^ FS ^ PQ1, 0, 1, Y ^ XZ ^ XA ^ ID000.GRF ^ FS ^ XZ ", targetDatas. length, rowBytes, BitConverter. toString (targetDatas ). replace ("-", string. empty), offsetX, offsetY );
Return ret;
}
Reprinted please indicate the source