PrinterBean.cs
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OnlineStore.Common;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
using Application = System.Windows.Forms.Application;
namespace OnlineStore.DeviceLibrary
{
/// <summary>
/// 打印机类
/// </summary>
public class PrinterBean
{
/// <summary>
/// 料盘移动参数,key=料盘位置名,value=对应位置料盘移动参数
/// </summary>
public Dictionary<string, LabelParam> LabelParams = new Dictionary<string, LabelParam>();
private Asa.PrintLabel print;
ZebraPrinterHelper.ZebraManger zebraManger;
public string IP { get; private set; }
string labelName;
public PrinterBean()
{
print = new Asa.PrintLabel(Application.StartupPath + "\\Label");
labelName = ConfigAppSettings.GetValue("LabelName", "lizhen");
//string[] names= print.GetLabelName();
IP = ConfigAppSettings.GetValue("PrinterIP", "192.168.103.21");
zebraManger = new ZebraPrinterHelper.ZebraManger(IP, ZebraPrinterHelper.ConnectionType.Network);
Connect(out string msg);
}
/// <summary>
/// 打印
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public bool Print(Label_LZ content, out string msg)
{
msg = "";
bool rtn=false;
try
{
//Connect(out msg);
if (content == null)
{
msg = "无贴标信息";
return false;
}
var bmp = PrintView(content);
rtn = zebraManger?.PrintImage(bmp, out msg) ?? false;
bmp.Dispose();
}
catch { }
//finally
//{
// Close();
//}
return rtn;
}
public Bitmap PrintView(Label_LZ content)
{
print.LoadLabel(labelName);
Dictionary<string, string> text = new Dictionary<string, string>();
text.Add("lh", content.pn);
text.Add("mb", content.side);
text.Add("jt", content.station);
text.Add("sj", content.shijian);
text.Add("xb", content.line);
text.Add("zb", content.slot);
return print.PrintPreview(text);
}
public bool IsReadyPrint(out string msg)
{
msg = "";
try
{
return zebraManger?.IsPrinterReady(out msg) ?? false;
}
catch (Exception ex)
{
msg = $"IsReadyPrint Error:{ex.StackTrace}";
return false;
}
}
public void Close()
{
zebraManger?.Close();
}
/// <summary>
/// 连接打印机
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <returns></returns>
public bool Connect(out string msg)
{
msg = "";
return zebraManger?.Connection(out msg) ?? false;
}
public void Connect()
{
zebraManger?.Connection(out string msg);
}
/// <summary>
/// 剥纸器上是否有标签
/// </summary>
/// <returns>true:有标签</returns>
public bool IsLabelOnPeeler()
{
return zebraManger?.IsLabelOnPeeler ?? true;
}
}
}