using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; using System.Drawing.Printing; using System.Threading; using terminal2.Code; namespace terminal2.Code { public class Printer { private Image img; private PrintDocument document; public Printer(Image _img,PrintDocument _document) { img = _img; document = _document; } public void Print() { float m_pageWidth = 80;//纸张宽度 mm单位 float m_pageHeight = 80;//纸张高度 mm单位 document.DefaultPageSettings.PaperSize = new PaperSize("newPage70X40", (int)(m_pageWidth / 25.4 * 100), (int)(m_pageHeight / 25.4 * 100)); //自定义图片内容整体上间距/左间距 document.OriginAtMargins = false; document.DefaultPageSettings.Margins.Top = (int)(0.1 / 25.4 * 100); document.DefaultPageSettings.Margins.Left = (int)(0.1 / 25.4 * 100); document.PrintPage += new PrintPageEventHandler(this.printDocument1_PrintPage); try { document.Print(); Thread.Sleep(1000); } catch (Exception) { document.PrintController.OnEndPrint(document, new PrintEventArgs()); } } /// /// 打印事件 /// /// /// private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { e.Graphics.DrawImage(img, 46, -40); e.Graphics.DrawString("请妥善保存取车码", new Font("宋体", 9), Brushes.Black, 95, 150); e.Graphics.DrawString(".", new Font("宋体", 9), Brushes.Black, 0, 190); } } }