using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Drawing.Printing; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace EmisTerminal { public class ImagePrintDocument : PrintDocument { private int m_currentPage; private List m_pages = new List(); public ImagePrintDocument(IList imageList) { m_pages = imageList.ToList(); } protected override void Dispose(bool disposing) { base.Dispose(disposing); if (disposing) { foreach (Image s in m_pages) { s.Dispose(); } m_pages.Clear(); } } protected override void OnBeginPrint(PrintEventArgs e) { base.OnBeginPrint(e); m_currentPage = 0; } protected override void OnPrintPage(PrintPageEventArgs e) { base.OnPrintPage(e); using (Image pageToPrint = m_pages[m_currentPage]) { Rectangle adjustedRect = new Rectangle( e.PageBounds.Left - (int)e.PageSettings.HardMarginX, e.PageBounds.Top - (int)e.PageSettings.HardMarginY, e.PageBounds.Width, e.PageBounds.Height); // Draw a white background for the report e.Graphics.FillRectangle(Brushes.White, adjustedRect); // Draw the report content e.Graphics.DrawImage(pageToPrint, adjustedRect); // Prepare for next page. Make sure we haven't hit the end. m_currentPage++; e.HasMorePages = m_currentPage < m_pages.Count; } } } }