borderAndreaVB free resources for Visual Basic developersborder
AndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2017 Andrea Tincani
:: Print a Picture to Fit a Page!

Author  

Hans Kruse

Language  

VB5, VB6

Operating Systems  

Windows 95, 98 and NT
Module

Option Explicit

Public Sub PrintPictureToFitPage(Pic As Picture)
    Dim PicRatio As Double
    Dim printerWidth As Double
    Dim printerHeight As Double
    Dim printerRatio As Double
    Dim printerPicWidth As Double
    Dim printerPicHeight As Double

   
' Determine if picture should be printed in landscape or portrait
    ' and set the orientation.

    If Pic.Height >= Pic.Width Then
        Printer.Orientation = vbPRORPortrait ' Taller than wide.
    Else
        Printer.Orientation = vbPRORLandscape ' Wider than tall.
    End If
   
' Calculate device independent Width-to-Height ratio for picture.
    PicRatio = Pic.Width / Pic.Height
   
' Calculate the dimentions of the printable area in HiMetric.
    printerWidth = Printer.ScaleX(Printer.ScaleWidth, Printer.ScaleMode, vbHimetric)
    printerHeight = Printer.ScaleY(Printer.ScaleHeight, Printer.ScaleMode, vbHimetric)
   
' Calculate device independent Width to Height ratio for printer.
    printerRatio = printerWidth / printerHeight
   
' Scale the output to the printable area.
    If PicRatio >= printerRatio Then
       
' Scale picture to fit full width of printable area.
        printerPicWidth = Printer.ScaleX(printerWidth, vbHimetric, Printer.ScaleMode)
        printerPicHeight = Printer.ScaleY(printerWidth / PicRatio, vbHimetric, Printer.ScaleMode)
    Else
       
' Scale picture to fit full height of printable area.
        printerPicHeight = Printer.ScaleY(printerHeight, vbHimetric, Printer.ScaleMode)
        printerPicWidth = Printer.ScaleX(printerHeight * PicRatio, vbHimetric, Printer.ScaleMode)
    End If
   
' Print the picture using the PaintPicture method.
    Printer.PaintPicture Pic, 0, 0, printerPicWidth, printerPicHeight
End Sub

Usage

'********************************************
'I had a heck of a time trying to print graphics from my
'picturebox until I used the folowing code:
'Here is how you call the sub shown above
'********************************************

Private Sub Command1_Click()
   
'Turn on AutoRedraw
    AutoRedraw = -1
   
'Until I put this next line in nothing was working and I mean zip, blank sheets, nada
    Picture1.Picture = Picture1.Image
   
'Print the Image
    Call PrintPictureToFitPage(Form1.Picture1.Picture)
    Printer.EndDoc
End Sub

'Please watch the wordwrap!

'Hope someone can use this, regards Hans

:: Navigation

Home

Printing Using Visual Basic

Previous Tip

Next Tip

:: Search this site
Google
:: Sponsored Links



borderAndreaVB free resources for Visual Basic developersborder
borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2017 Andrea Tincaniborder