Article Options
Premium Sponsor
Premium Sponsor

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

Ged Mead (XTab) is a Microsoft Visual Basic MVP who has been working on computer software and design for more than 25 years. His journey has taken him through many different facets of IT. These include training as a Systems Analyst, working in a mainframe software development environment, creating financial management systems and a short time spent on military laptop systems in the days when it took two strong men to carry a 'mobile' system.

Based in an idyllic lochside location in the West of Scotland, he is currently involved in an ever-widening range of VB.NET, WPF and Silverlight development projects. Now working in a consultancy environment, his passion however still remains helping students and professional developers to take advantage of the ever increasing range of sophisticated tools available to them.

Ged is a regular contributor to forums on vbCity and authors articles for DevCity. He is a moderator on VBCity and the MSDN Tech Forums and spends a lot of time answering technical questions there and in several other VB forum sites. Senior Editor for DevCity.NET, vbCity Developer Community Leader and Admin, and DevCity.NET Newsletter Editor. He has written and continues to tutor a number of free online courses for VB.NET developers.


View all articles by Ged Mead...
Some Printing Options

Preview Before Printing

   When we printed the chart images for the Bar Chart and the Line Chart, we manually set the X and Y locations of the Rectangle inside which we drew/printed that image to paper.   I used 20, 20 just to make sure that there was a reasonable gap from the left and the  top of the paper.   In both those cases this caused the chart to be printed  in the top left hand corner of the page, leaving quite a lot of white space at the right hand side.   It may be that sometimes you want to see what the finished printout will look like  - and also you may want to change the location of the chart on the printed page.  

   The most straightforward way of doing this is to drag a PrintPreviewDialog control on to the form.(Not to be confused with the very similarly named PrintPreviewControl control  - no wonder people find this stuff hard to master at first!).    You can leave the control with its default name of PrintPreviewDialog1.

   Add a second button to the form and call this one BtnPreview.   The following code in the Button's click event will display the chart in a preview pane and give the user the opportunity of confirming the print or cancelling it.  

Private Sub BtnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPreview.Click
   PrintPreviewDialog1.Document = PrintImageDocument
End Sub

 The PrintPreviewDialog also allows the user to zoom in on the page and this is something you may need to do to check that all is well; the default display is sometimes a rather scrappy thumbnail version that gives the impression that parts of the chart won't print.  

 Zooming in will reassure you that all's well. 


Centre the Printout

    Of course it's not much help to you to see in the Preview Pane that the chart will be printed over to the left and you'd like it to be centred.   There isn't a facility in the PrintPreviewDialog to make those kind of changes.  

   However, you can use relatively simple code to calculate where the X and Y points of the rectangle should be placed so that the chart is centred.

   Although we could tweak the PrintDocument control we first used (PrintImageDocument) I think it will be better to use a separate one so that you can cherry pick if you simply want to copy and paste from either variation.   So, drag a second PrintDocument on to the form and name it PrintCentred.

   In the PrintPage event of this control, we first create the merged image as before:

Private Sub PrintCentred_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintCentred.PrintPage
   ' First create the image using the function prepared above:
  Dim ImageToPrint As Image = MergeImages(picValues.Image, picGraph.Image)

   Then set default values for the X and Y points of the rectangle inside which we draw the chart on the paper.

Dim X As Integer = 20
Dim Y As Integer = 20

  Test if the image to be printed is no wider than the width of the paper it's to be printed on.   If the image is smaller then calculate where it should be placed on the printed page to centre it horizontally. 

With PrintCentred.DefaultPageSettings.PaperSize
   ' Only do this if image is smaller than the paper width
    If ImageToPrint.Width < .Width Then
      X = CInt((.Width - ImageToPrint.Width) / 2)
    End If
End With

   Draw the image on to the paper using either the default X, Y values or the new value of X if this has been changed above.

Dim R As New Rectangle(X, Y, ImageToPrint.Width, ImageToPrint.Height)

e.Graphics.DrawImage(ImageToPrint, R)

   I probably should mention that it would take only a couple of additional lines of code to centre the chart vertically also.   Simply test the Height of the Image vs. the height of the paper and if the image is smaller than the paper then use the same logic as above to set the value of Y correctly.

   In order to print the page, drag a third button on to the form, name it BtnCentrePrint.  This code in its click event will call the PrintPreviewDialog (we can use the same one).  As you will see when you view the preview, the chart is now centred on the page.


    There are many more tweaks you can make to the printout and I hope to cover those in a future article.  For now though you have the ability to print hard copies of any of the three kinds of charts (plus other variations you create yourself ) with very little extra effort.

   I have created a VB.NET 2003* version of the code for this article and you can download and try out the demo solution for yourself.

  (* As the original articles were written at the time when VB.NET 2003 was the current version.   The same approach will work just as well in VB 2005.)

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.38834951456312 out of 5
 103 people have rated this page
Article Score55695
Sponsored Links