Printer.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7. using System.Drawing.Printing;
  8. using System.Threading;
  9. using terminal2.Code;
  10. namespace terminal2.Code
  11. {
  12. public class Printer
  13. {
  14. private Image img;
  15. private PrintDocument document;
  16. public Printer(Image _img,PrintDocument _document)
  17. {
  18. img = _img;
  19. document = _document;
  20. }
  21. public void Print()
  22. {
  23. float m_pageWidth = 80;//纸张宽度 mm单位
  24. float m_pageHeight = 80;//纸张高度 mm单位
  25. document.DefaultPageSettings.PaperSize = new PaperSize("newPage70X40", (int)(m_pageWidth / 25.4 * 100), (int)(m_pageHeight / 25.4 * 100));
  26. //自定义图片内容整体上间距/左间距
  27. document.OriginAtMargins = false;
  28. document.DefaultPageSettings.Margins.Top = (int)(0.1 / 25.4 * 100);
  29. document.DefaultPageSettings.Margins.Left = (int)(0.1 / 25.4 * 100);
  30. document.PrintPage += new PrintPageEventHandler(this.printDocument1_PrintPage);
  31. try
  32. {
  33. document.Print();
  34. Thread.Sleep(1000);
  35. }
  36. catch (Exception)
  37. {
  38. document.PrintController.OnEndPrint(document, new PrintEventArgs());
  39. }
  40. }
  41. /// <summary>
  42. /// 打印事件
  43. /// </summary>
  44. /// <param name="sender"></param>
  45. /// <param name="e"></param>
  46. private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  47. {
  48. e.Graphics.DrawImage(img, 46, -40);
  49. e.Graphics.DrawString("请妥善保存取车码", new Font("宋体", 9), Brushes.Black, 95, 150);
  50. e.Graphics.DrawString(".", new Font("宋体", 9), Brushes.Black, 0, 190);
  51. }
  52. }
  53. }