Summary

Team Fly 

Page 189

LISTING 7.7: THE PRINTBMP() SUBROUTINE

Private Sub PrintBMP()
    PD = New Printing.PrintDocument
    Dim PGSETUP As New PageSetupDialog
    PGSETUP.PageSettings = PD.DefaultPageSettings
    If PGSETUP.ShowDialog = DialogResult.OK Then
        Dim PP As New PrintPreviewDialog
        PP.Document = PD
        PP.ShowDialog()
    End If
End Sub

In the PrintPage event handler we print the bitmap by calling the DrawImage method of the Graphics object. The code calculates the coordinates of the bitmap's upper-left corner so that it will be centered on the page (regardless of the margins) and then prints the bitmap, with the statements of Listing 7.8.

LISTING 7.8: PRINTING A BITMAP CENTERED ON THE PAGE

Private Sub PD_PrintPage(ByVal sender As Object, _
            ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
            Handles PD.PrintPage
    Dim img As Image = bmp
    Dim X, Y As Integer
    X = Math.Max(0, (e.PageSettings.Bounds.Width - img.Width) / 2)
    Y = Math.Max(0, (e.PageSettings.Bounds.Height - img.Height) / 2)
    e.Graphics.DrawImage(img, X, Y)
End Sub

Notice that this time we retrieve the page's width and height from the PageSettings. Bounds property and we don't have to worry about the orientation of the page. In Listing 7.2 we used the DefaultPageSettings property of the PrintDocument object to extract the coordinates and dimensions of the printable area of the page, and we had to swap the width and height from within our code to account for landscape orientation.

Summary

In this chapter we thoroughly discussed the printing capabilities of .NET. We've demonstrated the printing process with practical examples, which you can use in your applications or extend by adding more features.

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net