Share via


Chart Printing (Chart Controls)

In the Chart Control for Windows Forms, you can print the chart picture. To do this, use the Chart control's Printing property. This object prints all the Chart control's elements at the current data view, except for scroll bars.

You can invoke the Print dialog box or print in the background. In the Printing object, the PrintDocument property allows you to set printing properties, such as page margins.

Custom Printing

To print the chart picture in a document that contains other document elements, invoke the PrintPaint method inside a PrintPageEventHandler. You must pass to the PrintPaint method the Graphics property from the PrintPageEventArgs object, along with a Rectangle object that defines the position of the chart picture in the document.

The following code demonstrates printing a document with a line of text, then the chart picture, then another line of text.

' Create new PrintDocument 
Dim pd As New System.Drawing.Printing.PrintDocument() 
' Add the event handler, and then print 
AddHandler pd.PrintPage, AddressOf pd_PrintPage 
' Print the document 
pd.Print() 
... 
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs) 
   ' Create and initialize print font 
   Dim printFont As New System.Drawing.Font("Arial", 10) 
   ' Create Rectangle structure, used to set the position of the chart 
   Dim myRec As New System.Drawing.Rectangle(10, 30, 150, 150) 
   ' Draw a line of text, followed by the chart, and then another line of text 
   ev.Graphics.DrawString("Line before chart", printFont, Brushes.Black, 10, 10) 
   chart1.Printing.PrintPaint (ev.Graphics, myRec) 
   ev.Graphics.DrawString("Line after chart", printFont, Brushes.Black, 10, 200) 
End Sub
// Create new PrintDocument 
System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument(); 
// Add a PrintPageEventHandler for the document 
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage); 
// Print the document 
pd.Print(); 
...
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
   // Create and initialize print font 
   System.Drawing.Font printFont = new System.Drawing.Font("Arial", 10); 
   // Create Rectangle structure, used to set the position of the chart Rectangle 
   myRec = new System.Drawing.Rectangle(10, 30, 150, 150); 
   // Draw a line of text, followed by the chart, and then another line of text 
   ev.Graphics.DrawString("Line before chart", printFont, Brushes.Black, 10, 10); 
   chart1.Printing.PrintPaint (ev.Graphics, myRec); 
   ev.Graphics.DrawString("Line after chart", printFont, Brushes.Black, 10, 200); 
}

See Also

Other Resources

Using Chart Controls