ImagePrintDocument.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Drawing.Printing;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.IO;
  9. namespace EmisTerminal
  10. {
  11. public class ImagePrintDocument : PrintDocument
  12. {
  13. private int m_currentPage;
  14. private List<Image> m_pages = new List<Image>();
  15. public ImagePrintDocument(IList<Image> imageList)
  16. {
  17. m_pages = imageList.ToList();
  18. }
  19. protected override void Dispose(bool disposing)
  20. {
  21. base.Dispose(disposing);
  22. if (disposing)
  23. {
  24. foreach (Image s in m_pages)
  25. {
  26. s.Dispose();
  27. }
  28. m_pages.Clear();
  29. }
  30. }
  31. protected override void OnBeginPrint(PrintEventArgs e)
  32. {
  33. base.OnBeginPrint(e);
  34. m_currentPage = 0;
  35. }
  36. protected override void OnPrintPage(PrintPageEventArgs e)
  37. {
  38. base.OnPrintPage(e);
  39. using (Image pageToPrint = m_pages[m_currentPage])
  40. {
  41. Rectangle adjustedRect = new Rectangle(
  42. e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
  43. e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
  44. e.PageBounds.Width,
  45. e.PageBounds.Height);
  46. // Draw a white background for the report
  47. e.Graphics.FillRectangle(Brushes.White, adjustedRect);
  48. // Draw the report content
  49. e.Graphics.DrawImage(pageToPrint, adjustedRect);
  50. // Prepare for next page. Make sure we haven't hit the end.
  51. m_currentPage++;
  52. e.HasMorePages = m_currentPage < m_pages.Count;
  53. }
  54. }
  55. }
  56. }