Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Print multiple pages in a PrintDocument
 

Print multiple pages in a PrintDocument

Hello...

I want to print a number of images into a PrintDocument, one image on one page.

Here is the sample code:

Code Snippet

Bitmap[] bitmaps = new Bitmap[4];

private void button1_Click(object sender, EventArgs e)

{

bitmaps[0] = new Bitmap(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Blue hills.jpg");

bitmaps[1] = new Bitmap(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg");

bitmaps[2] = new Bitmap(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg");

bitmaps[3] = new Bitmap(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Blue hills.jpg");

this.printDocument1.PrintPage+=new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);

this.printDocument1.DefaultPageSettings.Landscape = true;

this.printDocument1.Print();

}

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)

{

Graphics g = e.Graphics;

e.HasMorePages = true;

for (int i = 0; i < this.bitmaps.Length; i++)

{

Bitmap bitmap = this.bitmaps[i];

g.DrawImage(bitmap, new Rectangle(0, e.PageSettings.PaperSize.Height * i, bitmap.Width, bitmap.Height));

}

e.HasMorePages = false;

g.Dispose();

}

The problem is that this code is printing only the first image on page 1.

What can I do to print all 4 images on 4 pages?

Thanks.

Eusebiu  Tuesday, August 05, 2008 6:42 PM

I should have read a little more carefull about the PrintPage event(Occurs when the output to print for the current page is needed).

The solution is easy...

Code Snippet

int _page;

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)

{

Graphics g = e.Graphics;

Bitmap bitmap = this.bitmaps[_page];

g.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));

e.HasMorePages = ++_page < this.bitmaps.Length;

g.Dispose();

}

Hope this helps someone!

Eusebiu  Wednesday, August 06, 2008 4:02 PM

I should have read a little more carefull about the PrintPage event(Occurs when the output to print for the current page is needed).

The solution is easy...

Code Snippet

int _page;

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)

{

Graphics g = e.Graphics;

Bitmap bitmap = this.bitmaps[_page];

g.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));

e.HasMorePages = ++_page < this.bitmaps.Length;

g.Dispose();

}

Hope this helps someone!

Eusebiu  Wednesday, August 06, 2008 4:02 PM

You should alway wrap temporary IDisposables with a using statement:

using (Bitmap bitmap = this.bitmaps[_page])
{
     e.Graphics.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width,
          bitmap.Height));
}
e.HasMorePages = ++_page < this.bitmaps.Length;
This ensures that cleanup is done in the case of exceptions; It also simply does more cleanup than Dispose();
Kevin Phelps  Monday, August 24, 2009 1:58 AM

You can use google to search for other answers

Custom Search

More Threads

• ArguementOutOfRange error when raising PropertyChanged event
• Can datagridview contain treeview like items? (image included)
• Detect if Ctrl is pressed when a ColumnHeader is clicked
• DataGridView validation
• Setting a DataGridView in another form to show a table's contents
• ListControl.FormatString, .NET 2.0
• Question about the DataConnector (BindingSource) component
• beginner problem with databinding
• Help in datagridview Navigation
• Why doesn't BindingSource.Sort get updated when DataGridView sort occurs?