Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Everyone,

I want to add a print function to print everything( few graphs) that is displayed on tabpage.


I found this code, that supposed to convert the panel to bitmap and then print it.

however it just prints a blank sheet.

What is wrong with what im doing?

C#
Bitmap MyChartPanel = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(MyChartPanel, new Rectangle(0, 0, panel1.Width, panel1.Height));
PrintDialog MyPrintDialog = new PrintDialog();
if (MyPrintDialog.ShowDialog() == DialogResult.OK)
    {
         System.Drawing.Printing.PrinterSettings values;
         values = MyPrintDialog.PrinterSettings;
         MyPrintDocument.PrintController = new System.Drawing.Printing.StandardPrintController();
         MyPrintDocument.Print();
    }
 MyPrintDocument.Dispose();
Posted
Updated 4-Mar-12 22:39pm
v3

All you are doing is calling the Print event of the PrintDocument, without defining what method should handle the Print event. You should add something like this to your code in the if block:-
C#
MyPrintDocument.PrintPage += new PrintPageEventHandler(PrintPage);

then you define a method PrintPage which actually does the printing
C#
void PrintPage(object sender, PrintPageEventArgs e)
{
     Bitmap MyChartPanel = new Bitmap(panel1.Width, panel1.Height);  
     panel1.DrawToBitmap(MyChartPanel, new Rectangle(0, 0, panel1.Width, panel1.Height));  
     e.Graphics.DrawImage(MyChartPanel, newPoint(0,0));
}


Hope this helps
 
Share this answer
 
Comments
nikki88 5-Mar-12 7:23am    
That you Wayne, it works :)
Wayne Gaylard 5-Mar-12 7:36am    
Glad to help!

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