title: How to print bitmap image vertically to multiple pages in c#
body: <p>Bitmap image height may be greater than page height. In this case it should printed into multiple pages.</p>
  
  <p>I tried code below for small single page image but got OutOfMemoryException at line</p>
  
  <pre><code>    e.Graphics.DrawImage(bm.Clone(new Rectangle(0, 
         pageHeight * (pageno - 1), bm.Width, 
         pageHeight),
          System.Drawing.Imaging.PixelFormat.DontCare),
          0, 0);
  </code></pre>
  
  <p>Printing is done in ASP.NET MVC4 controller to server printer using</p>
  
  <pre><code>using System.Drawing.Printing;
  using System.Drawing;
  using System.IO;
  
  
          byte[] bmp =   bmp image created by wkhtmltoimage
          var doc = new PrintDocument();
          doc.PrinterSettings.PrinterName = "myprinter";
          doc.PrintPage += new PrintPageEventHandler(ProvideContent);
          pageHeight = doc.DefaultPageSettings.PaperSize.Height;
          using (bm = new Bitmap(new MemoryStream(bmp)))
          {
              var lehti = (int)Math.Ceiling((double)(bm.Height / pageHeight));
              doc.PrinterSettings.FromPage = 1;
              doc.PrinterSettings.ToPage = lehti;
              doc.PrinterSettings.MaximumPage = 1;
              doc.PrinterSettings.MinimumPage = lehti;
              pageno = 1;
              doc.Print();
          }
      }
  
      int pageno;
      int pageHeight;
      Bitmap bm;
  
      void ProvideContent(object sender, PrintPageEventArgs e)
      {
          e.Graphics.DrawImage(bm.Clone(new Rectangle(0, pageHeight * (pageno - 1), bm.Width, pageHeight), System.Drawing.Imaging.PixelFormat.DontCare),
              0, 0);
      }
  </code></pre>
  
  <p>How to print image to multiple pages ?
  <code>e.Graphics.DrawImage(bm,0, 0);</code> works but only top part of image is printed.</p>