Click here to Skip to main content
16,017,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys

Hope some one will be able to help me. I have a class that needs to print a panel on more than one page. My printpreview shows the pages perfect but when i press print it throws me a blank page out of the printer?

Can any one please help.

public class PrintClass
    {
        readonly PrintDocument printdoc1 = new PrintDocument();
        readonly PrintPreviewDialog previewdlg = new PrintPreviewDialog();
        Bitmap MemoryImage;
 
        private readonly Panel panel_;
       
        public PrintClass(Panel pnl)
        {
            panel_ = pnl;
 
            printdoc1.PrintPage += (printdoc1_PrintPage);
            MemoryImage = new Bitmap(pnl.Width, pnl.Height);
        }
       
                private int page = 0;
                private void GetPrintArea(Control pnl)
                {
                        page = 0;
                        MemoryImage = new Bitmap(pnl.Width, pnl.Height);
                        pnl.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pnl.Width, pnl.Height));
                }
 
                private void printdoc1_PrintPage(object sender, PrintPageEventArgs e)
                {
                        var pagearea = e.PageBounds;
                        e.Graphics.DrawImage(MemoryImage, new Rectangle(e.PageBounds.Location, e.PageBounds.Size), new Rectangle(e.PageBounds.Location.X, e.PageBounds.Height * page, e.PageBounds.Width, e.PageBounds.Height), GraphicsUnit.Pixel);
                        e.HasMorePages = e.PageBounds.Height * (page+1) < panel_.Size.Height;
                        page++;
                }
 
        public void Print()
        {
            GetPrintArea(panel_);
            previewdlg.Document = printdoc1;
            previewdlg.ShowDialog();
        }
    }


[Edited]Code is wrapped in "pre" tag[/Edited]
Posted
Updated 18-Jul-11 3:42am
v2
Comments
johannesnestler 18-Jul-11 11:12am    
I thought about your problem and gave it a quick test. I created a scrollable panel (AutoScroll). When I called DrawToBitmap it only drew the visible part of the panel. I realized i had to change from panel.Width/-Height to panel.DisplayRectangle.Width/-Height but nothing changed. So for me it seems that DrawToBitmap doesn't work as expected. But in my case it showed the second page empty on print preview. How did you create your panel?
Gigantour 19-Jul-11 2:45am    
Panel is created dynamically at runtime.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900