2021斑马打印机打印自定义图片思路:
1.
> 利用C#的Graphic类在自己程序中绘制出自己想要的打印效果图【简单粗暴,就是繁琐,要一直调整图片的样式,发布IIS可用】 或者使用CopyFromScreen()调用windows GDI32 API函数进行屏幕截图【有个问题是发布IIS 客户端截图失败,不能指定句柄】,再进行图片转16进制(斑马打印机能识别的机器码),然后用指令集中的~DG指令将打印效果图整张通过TCP/IP或者串口传输到打印机作为模板,然后打印时利用^XG指令调用该模板进行打印;
2.
> 2.利用ZPLII指令集编写带有位置信息,字体大小,打印内容等信息的指令,利用TCP/IP或者串口传输到打印机打印;
3.
> 3.利用NiceLabel、BarTender等第三方打印编辑软件,利用可视化界面(托拉拽)编辑好要打印的效果,然后调用打印机驱动进行打印;
开发案例:开发100mm*70mm的标签
知识点1.Graphic类绘制图片
#region Graphic类绘制图片打印
//开始绘制图片
int initialWidth = 580, initialHeight = 392;
//设置长度宽度
Bitmap theBitmap = new Bitmap(initialWidth, initialHeight);
Graphics theGraphics = Graphics.FromImage(theBitmap);
//填充的颜色
Brush bush = new SolidBrush(System.Drawing.Color.Black);
//呈现质量
theGraphics.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//背景色
theGraphics.Clear(System.Drawing.Color.White);
string urlImage = System.AppDomain.CurrentDomain.BaseDirectory;
System.Drawing.Font myFont;
System.Drawing.Font myFontT;
System.Drawing.Font myFontUnder;
System.Drawing.Font myFontBigUnder;
myFontT = new System.Drawing.Font("宋体", 9, FontStyle.Regular);
myFont = new System.Drawing.Font("宋体", 14, FontStyle.Regular);
myFontUnder = new System.Drawing.Font("宋体", 15, FontStyle.Regular);
myFontBigUnder = new System.Drawing.Font("Century Schoolbook", 8, FontStyle.Regular);
StringFormat geshi = new StringFormat();
geshi.Alignment = StringAlignment.Center; //居中
System.Drawing.Image img = new Bitmap(urlImage + "\Image\TPK.jpg");
//TPKimg
theGraphics.DrawImage(img, 5, 0, (float)(190), (float)(52.5));
theGraphics.DrawString("touching tomorrow,today!", myFontT, bush, 15, 55);
//Conimg
System.Drawing.Image img2 = new Bitmap(urlImage + "\Image\consignmentLabel.jpg");
theGraphics.DrawImage(img2, 196, 0, (float)(287), (float)(75));
//QRCodeimg
Image img3 = new Bitmap(urlImage + "\Image\labelQRCode.jpg");
theGraphics.DrawImage(img3, 490, 11, (float)(67), (float)(67));
int hei = 75;
int mul = 45;
int txtUnder = 20;
int txt = 15;
//txt
theGraphics.DrawString("供 应 商:", myFont, bush, 5, hei);
theGraphics.DrawString("Vendor :", myFont, bush, 5, hei + txtUnder);//+10
theGraphics.DrawString("Darwin", myFontUnder, bush, 140, hei + txt);//+5
theGraphics.DrawLine(new Pen(Color.Black), new Point(100, hei + txtUnder \* 2), new Point(250, hei + txtUnder \* 2));
theGraphics.DrawString("订单号码:", myFont, bush, 255, hei);
theGraphics.DrawString("PO NO :", myFont, bush, 255, hei + txtUnder);
theGraphics.DrawString(txtNum.Text, myFontUnder, bush, 365, hei + txt);
theGraphics.DrawLine(new Pen(Color.Black), new Point(350, hei + txtUnder * 2), new Point(500, hei + txtUnder * 2));
theGraphics.DrawString("TPK料号:", myFont, bush, 5, hei + mul);
theGraphics.DrawString("TPK P/N:", myFont, bush, 5, hei + mul + txtUnder);
theGraphics.DrawString(txtPart.Text, myFontUnder, bush, 220, hei + mul + txt);
theGraphics.DrawLine(new Pen(Color.Black), new Point(100, hei + mul + txtUnder * 2), new Point(500, hei + mul + txtUnder * 2));
theGraphics.DrawString("品名规格:", myFont, bush, 5, hei + mul * 2);
theGraphics.DrawString("Product Specs:", myFontBigUnder, bush, 5, hei + mul * 2 + txtUnder);
theGraphics.DrawString("BLU", myFontUnder, bush, 160, hei + mul * 2 + txt);
theGraphics.DrawLine(new Pen(Color.Black), new Point(100, hei + mul * 2 + txtUnder * 2), new Point(250, hei + mul * 2 + txtUnder * 2));
theGraphics.DrawString("生产批号:", myFont, bush, 255, hei + mul * 2);
theGraphics.DrawString("Lot No:" , myFont, bush, 255, hei + mul * 2 + txtUnder);
theGraphics.DrawString("SAMPLE", myFontUnder, bush, 390, hei + mul * 2 + txt);
theGraphics.DrawLine(new Pen(Color.Black), new Point(350, hei + mul * 2 + txtUnder * 2), new Point(500, hei + mul * 2 + txtUnder * 2));
theGraphics.DrawString("生产日期:", myFont, bush, 5, hei + mul*3);
theGraphics.DrawString("Produce date:", myFontBigUnder, bush, 5, hei + mul * 3 + txtUnder);
theGraphics.DrawString(txtPD.Text, myFontUnder, bush, 130, hei + mul * 3 + txt);
theGraphics.DrawLine(new Pen(Color.Black), new Point(100, hei + mul * 3 + txtUnder * 2), new Point(250, hei + mul * 3 + txtUnder * 2));
theGraphics.DrawString("失效日期:", myFont, bush, 255, hei + mul * 3);
theGraphics.DrawString("Expired date:", myFontBigUnder, bush, 255, hei + mul * 3 + txtUnder);
theGraphics.DrawString(txtED.Text, myFontUnder, bush, 380, hei + mul * 3 + txt);
theGraphics.DrawLine(new Pen(Color.Black), new Point(350, hei + mul * 3 + txtUnder * 2), new Point(500, hei + mul * 3 + txtUnder * 2));
theGraphics.DrawString("交货总数:", myFont, bush, 5, hei + mul * 4);
theGraphics.DrawString("Lot Qty:", myFont, bush, 5, hei + mul * 4 + txtUnder);
theGraphics.DrawString(txtGoodsQuantity.Text, myFontUnder, bush, 155, hei + mul * 4 + txt);
theGraphics.DrawLine(new Pen(Color.Black), new Point(100, hei + mul * 4 + txtUnder * 2), new Point(250, hei + mul * 4 + txtUnder * 2));
theGraphics.DrawString("本箱数量:", myFont, bush, 255, hei + mul * 4);
theGraphics.DrawString("Qty.Pox Box:", myFontBigUnder, bush, 255, hei + mul * 4 + txtUnder);
theGraphics.DrawString(txtCases.ToString(), myFontUnder, bush, 410, hei + mul * 4 + txt);
theGraphics.DrawLine(new Pen(Color.Black), new Point(350, hei + mul * 4 + txtUnder * 2), new Point(500, hei + mul * 4 + txtUnder * 2));
theGraphics.DrawString("交货箱数:", myFont, bush, 5, hei + mul * 5);
theGraphics.DrawString("Box Qty:", myFont, bush, 5, hei + mul * 5 + txtUnder);
theGraphics.DrawString(txtDeliveryCases.Text, myFontUnder, bush, 160, hei + mul * 5 + txt);
theGraphics.DrawLine(new Pen(Color.Black), new Point(100, hei + mul * 5 + txtUnder * 2), new Point(250, hei + mul * 5 + txtUnder * 2));
theGraphics.DrawString("本箱编号:", myFont, bush, 255, hei + mul * 5);
theGraphics.DrawString("Box No:", myFont, bush, 255, hei + mul * 5 + txtUnder);
theGraphics.DrawString(txtBoxNumber.Text, myFontUnder, bush, 400, hei + mul * 5 + txt);
theGraphics.DrawLine(new Pen(Color.Black), new Point(350, hei + mul * 5 + txtUnder * 2), new Point(500, hei + mul * 5 + txtUnder * 2));
theGraphics.DrawString("储存温度:" , myFont, bush, 5, hei + mul * 6);
theGraphics.DrawString("Stone Temp:", myFontBigUnder, bush, 5, hei + mul * 6 + txtUnder);
theGraphics.DrawString(txtTemperature.Text, myFontUnder, bush, 130, hei + mul * 6 + txt);
theGraphics.DrawLine(new Pen(Color.Black), new Point(100, hei + mul * 6 + txtUnder * 2), new Point(250, hei + mul * 6 + txtUnder * 2));
theGraphics.DrawString("储存湿度:" , myFont, bush, 255, hei + mul * 6);
theGraphics.DrawString("Stone humidity:", myFontBigUnder, bush, 255, hei + mul * 6 + txtUnder);
theGraphics.DrawString(txtHumidity.Text, myFontUnder, bush, 380, hei + mul * 6 + txt);
theGraphics.DrawLine(new Pen(Color.Black), new Point(350, hei + mul * 6 + txtUnder * 2), new Point(500, hei + mul * 6 + txtUnder * 2));
//结束图片绘制
var imgTPK = urlImage + "\\Image\\Label\\TPKLabel" + this.txtBoxNumber.Text.ToString() + ".jpg";
//保存图片
theBitmap.Save(imgTPK);
#endregion
知识点2:PNG 图像转16进制,需借助ZPL帮助类(涉及ZPL打印指令)
#region PNG图像转为16进制
string zplToSend = ConvertImageHasPosition(imgTPK, 0, 0);//生成打印机指令(识别单色)
string printImage = "^XA^^FO10,80^LH10,80^LL2500^PW2500^XGR:ZLOGO.GRF,2,2^FS^XZ";//打印图片命令 //^FO0,0 设置打印内容坐标
#endregion
#region ZPL帮助类//图片转为GRF格式 实现并输出
//不带位置
public string ConvertImageNoPosition(string s_FilePath)
{
int b = 0;
long n = 0;
long clr;
StringBuilder sb = new StringBuilder();
sb.Append("~DGR:ZLOGO.GRF,");
Bitmap bm = new Bitmap(s_FilePath);
int w = ((bm.Size.Width / 8 + ((bm.Size.Width % 8 == 0) ? 0 : 1)) * bm.Size.Height);
int h = (bm.Size.Width / 8 + ((bm.Size.Width % 8 == 0) ? 0 : 1));
sb.Append(w.ToString().PadLeft(5, '0') + "," + h.ToString().PadLeft(3, '0') + ",\n");
using (Bitmap bmp = new Bitmap(bm.Size.Width, bm.Size.Height))
{
for (int y = 0; y < bm.Size.Height; y++)
{
for (int x = 0; x < bm.Size.Width; x++)
{
b = b * 2;
clr = bm.GetPixel(x, y).ToArgb();
string s = clr.ToString("X");
if (s.Substring(s.Length - 6, 6).CompareTo("BBBBBB") < 0)
{
bmp.SetPixel(x, y, bm.GetPixel(x, y));
b++;
}
n++;
if (x == (bm.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("^FO100,540^XGR:ZLOGO.GRF,2,2^FS");
}
return sb.ToString();
}
//带位置 (从左上角开始)到条码字段x,y座标
public string ConvertImageHasPosition(string s_FilePath, int xtop, int ytop)
{
int b = 0;
long n = 0;
long clr;
StringBuilder sb = new StringBuilder();
sb.Append("~DGR:ZLOGO.GRF,");
Bitmap bm = new Bitmap(s_FilePath);
int w = ((bm.Size.Width / 8 + ((bm.Size.Width % 8 == 0) ? 0 : 1)) * bm.Size.Height);
int h = (bm.Size.Width / 8 + ((bm.Size.Width % 8 == 0) ? 0 : 1));
sb.Append(w.ToString().PadLeft(5, '0') + "," + h.ToString().PadLeft(3, '0') + ",\n");
using (Bitmap bmp = new Bitmap(bm.Size.Width, bm.Size.Height))
{
for (int y = 0; y < bm.Size.Height; y++)
{
for (int x = 0; x < bm.Size.Width; x++)
{
b = b * 2;
clr = bm.GetPixel(x, y).ToArgb();
string s = clr.ToString("X");
if (s.Substring(s.Length - 6, 6).CompareTo("BBBBBB") < 0)
{
bmp.SetPixel(x, y, bm.GetPixel(x, y));
b++;
}
n++;
if (x == (bm.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(string.Format("^FO{0},{1}^XGR:ZLOGO.GRF,2,2^FS", xtop, ytop));
}
return sb.ToString();
}
#endregion
知识点3:连接打印机
#region 连接打印机
string ipAddress = ""; //IP地址;
int port = 8000;//端口
try
{
// Open connection
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
client.Connect(ipAddress, port);
// Write ZPL String to connection
System.IO.StreamWriter writer = new System.IO.StreamWriter(client.GetStream(), Encoding.UTF8);
writer.Write(zplToSend);
writer.Flush();
writer.Write(printImage);
writer.Flush();
// Close Connection
writer.Close();
client.Close();
}
catch (Exception ex)
{
// Catch Exception
}
#endregion
#region 删除图片
protected static bool FilePicDelete(string path)
{
bool ret = false;
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{
file.Delete();
ret = true;
}
return ret;
}
#endregion
程序使用系统的命名空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Data;
using System.Text;
using System.Web.UI;
using ThoughtWorks.QRCode;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using PTKLabel.model;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Windows.Forms;
using System.Threading;
using System.Net;
参考资料:
打印案例集锦: https://www.cnblogs.com/lab-zj/p/13957595.html
ZPL帮助类: http://www.voidcn.com/article/p-wuewvqas-bqn.html
斑马打印机打印方式的利弊: https://blog.csdn.net/HorseRoll/article/details/82253434
Graphic类实现案例: https://blog.csdn.net/horseroll/article/details/80496091
ZPL打印指令: https://blog.csdn.net/weixin\_38211198/article/details/95961605
项目感悟:第一次写文章,标签创建失败了,在网上兜兜转转参考了很多例子,才找到适合自己的开发方法实现需求,有参考的文章都有列上去,如有侵,请联系,Thanks♪(・ω・)ノ,写篇文章记录下,加油吧ヾ(◍°∇°◍)ノ゙
番剧分享:入坑 《堀与宫村》,敲好看(^▽^),嘿嘿