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
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)
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)
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.)