Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Print a Picture to Fit a Page!

Total Hit ( 1658)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Module

Click here to copy the following block
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

Click here to copy the following block
'********************************************
'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


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.