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 System.Windows.Forms;

namespace tool
{
    public class Printer
    {
        private string context;
        private Image img;
        private PrintDocument document;
        /// <summary>
        /// 打印机
        /// </summary>
        /// <param name="_img">二维码</param>
        /// <param name="_document">可重用的可发送到打印机上的对象</param>
        /// <param name="_context">取车请求字符串</param>
        /// <returns></returns>
        public Printer(Image _img, PrintDocument _document, string _context)
        {
            img = _img;
            document = _document;
            context = _context;
        }
        public void Print()
        {
            float m_pageWidth = 70;//纸张宽度 mm单位
            float m_pageHeight = 70;//纸张高度 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);
            //实现PrintPageEventHandler委托
            document.PrintPage += new PrintPageEventHandler(this.printDocument1_PrintPage);
            try
            {
                document.Print();
                //Thread.Sleep(200);
            }
            catch (Exception ex)
            {
                document.PrintController.OnEndPrint(document, new PrintEventArgs());
            }
        }
        /// <summary>
        /// 打印事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(img, 56, -20);//二维码中已经存储取车信息
            e.Graphics.DrawString("请妥善保存取车码", new Font("宋体", 9), Brushes.Black, 95, 170);//提示信息
            e.Graphics.DrawString("车牌号：" + context, new Font("宋体", 9), Brushes.Black, 95, 150);            
            e.Graphics.DrawString(DateTime.Now.ToString(), new Font("宋体", 9), Brushes.Black, 90, 190);//时间            
            e.Graphics.DrawString(".", new Font("宋体", 9), Brushes.Black, 0, 210);//
        }
    }

}
