图表打印
在 Windows 窗体的图表控件中,可以打印图表图片。 若要执行此操作,请使用图表控件的 Printing 属性。 该对象用于打印当前数据视图上所有图表控件的元素(滚动条除外)。
可以调用“打印”对话框,也可以在后台打印。 在 Printing 对象中,使用 PrintDocument 属性可以设置打印属性(如页边距)。
自定义打印
如果要打印的图表图片所在的文档还包含其他文档元素,请在 PrintPageEventHandler 内调用 PrintPaint 方法。 必须将 PrintPageEventArgs 对象的 Graphics 属性以及用于定义图表图片在文档中的位置的 Rectangle 对象,一起传递给 PrintPaint 方法。
下面的代码演示打印文档中的一行文本,然后打印图表图片,最后打印另一行文本。
' 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);
}