protected override void OnOpen() { Console.WriteLine("opened"); WsResponse <string> r = new WsResponse <string>(); r.Data = "连接成功"; string msg = JsonConvert.SerializeObject(r); Send(msg); }
protected void printHtml(string seq, string html) { WsResponse <string> r = new WsResponse <string>(); r.Seq = seq; r.Data = "暂不支持HTML"; string msg = JsonConvert.SerializeObject(r); Send(msg); }
/// <summary> /// 打印 /// </summary> /// <param name="seq">序号,用于区分回调</param> /// <param name="printerName">打印机,如果不指定,则使用画面上的打印机</param> /// <param name="imageList">待打印图片列表</param> protected void printImage(string seq, string printer, List <KeyValuePair <string, string> > imageList) { // 根据名称获取对应打印机 PrinterSetting printerSetting = null; foreach (PrinterSetting ps in PrintSetting.Printers) { if (String.IsNullOrEmpty(printer) && ps.IsDefault) { printerSetting = ps; break; } if (ps.ID == printer || ps.Printer == printer) { printerSetting = ps; break; } } if (printerSetting == null) { // 未找到指定打印机 return; } // 设置打印时不再弹打印进度弹窗 PrintController printController = new StandardPrintController(); PrintDocument printDoc = new PrintDocument(); printDoc.DefaultPageSettings.Landscape = printerSetting.Landscape; printDoc.PrinterSettings.PrinterName = printerSetting.Printer; printDoc.PrinterSettings.Copies = 1; //if (!String.IsNullOrEmpty(seq)) { // printDoc.PrinterSettings.PrintFileName = seq; //} // printDoc.OriginAtMargins = true; printDoc.PrintController = printController; // 开始打印事件 printDoc.BeginPrint += (object sender, PrintEventArgs e) => { Logger.InfoLog("print begin" + seq); WsResponse <string> br = new WsResponse <string>(); br.Seq = seq; br.Data = "begin"; Send(JsonConvert.SerializeObject(br)); }; // 结束打印事件 printDoc.EndPrint += (object sender, PrintEventArgs e) => { Logger.InfoLog("print end " + seq); WsResponse <string> er = new WsResponse <string>(); er.Seq = seq; er.Data = "end"; Send(JsonConvert.SerializeObject(er)); }; int page = 1; // 打印,每一页执行一次该事件 printDoc.PrintPage += (object sender, PrintPageEventArgs e) => { // inch int x = toInchX100(printerSetting.MarginLeft); int y = toInchX100(printerSetting.MarginTop); // inch int xr = toInchX100(printerSetting.MarginRight); int yb = toInchX100(printerSetting.MarginBottom); int w = 300, h = 551; switch (printerSetting.Paper) { case "1": { w = 300; // 285; // 300* 0.95 h = 511; // 485; // 511 * 0.95 break; } case "2": { w = toInchX100(100); h = toInchX100(180); break; } case "3": default: { w = toInchX100(100); h = toInchX100(180); } break; } e.PageSettings.Margins.Left = 0; e.PageSettings.Margins.Top = 0; e.PageSettings.Margins.Bottom = 0; e.PageSettings.Margins.Right = 0; e.PageSettings.PaperSize = new PaperSize("自定义", w, h); //foreach (KeyValuePair<string, string> image in imageList) //{ KeyValuePair <string, string> image = imageList[page - 1]; // Image i = Image.FromFile(image); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(image.Value); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) //获得响应) { if (response.StatusCode != HttpStatusCode.OK) { WsResponse <KeyValuePair <string, string> > rerr = new WsResponse <KeyValuePair <string, string> >(); rerr.Seq = seq; rerr.Code = "NET_ERR"; rerr.Data = new KeyValuePair <string, string>(image.Key, "error:" + response.StatusCode); string msgerr = JsonConvert.SerializeObject(rerr); Send(msgerr); } else { using (Stream stream = response.GetResponseStream()) { Image img = Image.FromStream(stream);///实例化,得到img // Conole.wrimg.Width img.Height Console.WriteLine(img.Width + "x" + img.Height); Console.WriteLine(e.PageSettings.HardMarginX + "x" + e.PageSettings.HardMarginY); if (printerSetting.AutoSize) { // e.MarginBounds e.MarginBounds.Offset(0, 0); //Console.WriteLine(e.MarginBounds); //Rectangle rect = new Rectangle(x, y, w - x - xr, h - y - yb); //e.Graphics.DrawImage(img, rect); e.Graphics.DrawImage(img, e.MarginBounds); } else { e.MarginBounds.Offset(0, 0); e.Graphics.DrawImage(img, e.PageSettings.HardMarginX + x, e.PageSettings.HardMarginY + y, w, h); } } Logger.InfoLog("print printed" + image.Key + " data:" + image.Value); WsResponse <KeyValuePair <string, string> > rimage = new WsResponse <KeyValuePair <string, string> >(); rimage.Seq = seq; rimage.Data = new KeyValuePair <string, string>(image.Key, "printed"); string msgImage = JsonConvert.SerializeObject(rimage); Send(msgImage); } } //} if (page < imageList.Count) { // 表示还有下一页, e.HasMorePages = true; } else { e.HasMorePages = false; } page++; }; printDoc.Print(); WsResponse <string> r = new WsResponse <string>(); r.Seq = seq; r.Data = "printed"; string msg = JsonConvert.SerializeObject(r); Send(msg); }
protected override void OnMessage(MessageEventArgs e) { WsCommand command = JsonConvert.DeserializeObject <WsCommand>(e.Data); if (command.Command == "print") { #region print Logger.InfoLog("print " + command.Seq + " data:" + command.Data); lock (this) { if (command.Type == "image") { if (command.Data.StartsWith("[")) { // 多个图片 List <KeyValuePair <string, string> > imageList = JsonConvert.DeserializeObject <List <KeyValuePair <string, string> > >(command.Data); printImage(command.Seq, command.Printer, imageList); } else { printImage(command.Seq, command.Printer, command.Data); } } else if (command.Type == "html") { if (command.Data.StartsWith("[")) { // 多个html List <KeyValuePair <string, string> > htmlList = JsonConvert.DeserializeObject <List <KeyValuePair <string, string> > >(command.Data); } else { printHtml(command.Seq, command.Data); } } else { string extension = Path.GetExtension(command.Data); if (Regex.IsMatch(extension, @"(jpg|jpeg|png|jfif)", RegexOptions.IgnoreCase)) { printImage(command.Seq, command.Printer, command.Data); } else if (Regex.IsMatch(extension, @"(html|html)", RegexOptions.IgnoreCase)) { printHtml(command.Seq, command.Data); } } } #endregion } else if (command.Command == "getPrinters") { #region getPriters 打印机列表 PrinterSettings.StringCollection printers = PrinterSettings.InstalledPrinters; List <string> result = new List <string>(); foreach (string item in printers) { result.Add(item); } WsResponse <List <string> > chkr = new WsResponse <List <string> >(); chkr.Seq = command.Seq; chkr.Data = result; Send(JsonConvert.SerializeObject(chkr)); #endregion } else if (command.Command == "checkPrinter") { #region checkPrinter 查看打印机是否正常开启 PrinterSettings.StringCollection printers = PrinterSettings.InstalledPrinters; bool hasThePrinter = false; foreach (string item in printers) { if (command.Data == item) { hasThePrinter = true; break; } } WsResponse <string> er = new WsResponse <string>(); er.Seq = command.Seq; if (hasThePrinter) { er.Data = "OK"; } else { er.Code = "NG"; } Send(JsonConvert.SerializeObject(er)); #endregion } else { #region error WsResponse <string> er = new WsResponse <string>(); er.Seq = command.Seq; er.Code = "NG"; er.Data = "命令" + command.Command + "不支持"; Send(JsonConvert.SerializeObject(er)); #endregion } }