Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  GDI+ Chart Success Part 7: Printing Charts  »  Bar Chart - Printing
 »  Home  »  Windows Development  »  Graphics  »  GDI+ Chart Success Part 7: Printing Charts  »  Bar Chart - Printing
GDI+ Chart Success Part 7: Printing Charts
by Ged Mead | Published  10/16/2006 | .NET Newbie Graphics | Rating:
Bar Chart - Printing

The Easy Way and the Hard Way

   If I'd created the code for Part 4 knowing that I would eventually want to use it in more than one way I certainly would have set about it differently.   Certainly, for a start I would have avoided hard coding in the name of the Picturebox as I did in some of the procedures.  It wouldn't take a huge amount of effort to comb through the original code and replace hard coded names with parameters.  A couple more tweaks here and there and it would then be possible to use the PrintPage event of a PrintDocument control to draw the chart straight on to the printed page, much as we did with the pie chart.

   That however is still a harder hill than we need to climb because there is a much easier way we can go.   Because we have drawn the chart onto a PictureBox control we automatically have access to the PictureBox's Image property.   And of course that image will be the exact chart that we want to print out!  The GDI+ methods include one that draws an image (DrawImage - what else?) and we can draw an image to a printer just as easily as we can draw one to a screen - as we shall now see.

   First, drag a PrintDocument control from the Toolbox on to the form you are using to create the Bar Chart, then read on:


  Essentially, all we have to do is grab the image from the PictureBox, read the size of the PictureBox and pass this information to the DrawImage method.   By placing the DrawImage method in the PrintPage event of the PrintDocument control, we force the chart (i.e. the image) to be drawn to the printer.

   Here is the code for the PrintPage event:

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
 ' Grab the image
 Dim ImageToPrint As Image = Me.PBBarChart.Image
 ' Set the X and Y position values that you want as the start point
 ' for printing on the page, together with the width and height of the image.

 Dim R As New Rectangle(20, 20, ImageToPrint.Width, ImageToPrint.Height)
 ' Draw the image (placing it within the rectangle defined above)
 e.Graphics.DrawImage(ImageToPrint, R)
End Sub

   I don't think there is anything I need to add to the commenting for you to see how it works.

    And as we did with the Pie Chart, you can add a second button to the form, name it BtnPrint and use it to call the PrintDocument's Print method:

Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrint.Click
End Sub

  The form will look something like this and you will be able to print out the displayed bar chart by clicking on the Print Graph button.


Sponsored Links