Private Sub PrintPictureToFitPage(Prn As Printer, Pic As Picture)
Dim PicRatio As Double
Dim PrnWidth As Double
Dim PrnHeight As Double
Dim PrnRatio As Double
Dim PrnPicWidth As Double
Dim PrnPicHeight As Double
Const vbHiMetric As Integer = 8 '
' Determine if picture should be printed in landscape
' or portrait and set the orientation.'
If Pic.Height >= Pic.Width Then
Prn.Orientation = vbPRORPortrait 'Taller than wide
Else
Prn.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.'
With Prn
PrnWidth = .ScaleX(.ScaleWidth, .ScaleMode, vbHiMetric)
PrnHeight = .ScaleY(.ScaleHeight, .ScaleMode, vbHiMetric)
End With '
' Calculate device independent Width to Height ratio for printer.'
PrnRatio = PrnWidth / PrnHeight '
' Scale the output to the printable area.'
If PicRatio >= PrnRatio Then '
' Scale picture to fit full width of printable area. '
PrnPicWidth = Prn.ScaleX(PrnWidth, vbHiMetric, Prn.ScaleMode)
PrnPicHeight = Prn.ScaleY(PrnWidth / PicRatio, vbHiMetric, Prn.ScaleMode)
Else
' Scale picture to fit full height of printable area. '
PrnPicHeight = Prn.ScaleY(PrnHeight, vbHiMetric, Prn.ScaleMode)
PrnPicWidth = Prn.ScaleX(PrnHeight * PicRatio, vbHiMetric, Prn.ScaleMode)
End If '
' Print the picture using the PaintPicture method.'
Call Prn.PaintPicture(Pic, 0, 0, PrnPicWidth, PrnPicHeight)
End Sub
Private Sub cmdPrintPicture_Click()
Call PrintPictureToFitPage(Printer, Picture1.Picture)
Printer.EndDoc
End Sub