Link to home
Start Free TrialLog in
Avatar of groundwar
groundwar

asked on

VBA Picture Sizing

Perhaps I'm on glue or have been looking at this too long, but how do you resize a picture that you've loaded onto a form? Basically what I'm trying to do is have a client select a logo file from their computer and load it onto the form. Then I want the form to resize to a "BEST FIT" Ratio. So if their Logo is 800X200 and the Standard Logo insert size is 200X50, it will resize the image by a ratio of 25%. I know how to deduce the resize ratio, but I can't resize it. Here's my code:

--

'Org/Logo Options - Load Logos onto Form
Private Sub loadLogoButton_Click()
 
 'Scaler Preportion
 scaler = 26.46
 
 'Check to see if logo selected
 If logo <> "" Then
 
  'Check if valid File Type
  FileType = Right(logo, 3)
 
  If FileType = "jpg" Or FileType = "jpeg" Or FileType = "gif" Then
   
   'Load picture onto form
   LargeLogoInsert.Picture = LoadPicture(logo)
   SmallLogoInsert.Picture = LoadPicture(logo)

     
   'Set best Fit Picture Size
   logo_ratio = LargeLogoInsert.Picture.Width / LargeLogoInsert.Picture.Height
   large_bestfit_ratio = 200 / LargeLogoInsert.Picture.Width
   large_bestfit_size = large_bestfit_ratio * LargeLogoInsert.Picture.Width
   
     
   'Change Picture Size to Match Best Fit
   ???? = large_bestfit_size

end sub

--

Thanks in advanced for your help.
Avatar of twalgrave
twalgrave

LargeLogoInsert.Width
and
LargeLogoInsert.Height
Avatar of groundwar

ASKER

Close, but the picture control is the only thing that changes size. The picture remains the same size. I thought that using the autosize would help the situation but it doesn't so what I end up with is a picture that is cropped cause the Control is smaller than the picture loaded in it. Do I have to load the picture into the control?

--

My glue habit continues.
ASKER CERTIFIED SOLUTION
Avatar of bhagyesht
bhagyesht
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here's some code you will want to look at:
http://www.vb-helper.com/howto_fit_picture_to_box.html
Adding -> LargeLogoInsert.PictureSizeMode = fmPictureSizeModeStretch made the world of difference. bhagyesht, you're right. Thanks for the help. In case anybody is looking for how it all looked in the end I've attached it below.

--

'Org/Logo Options - Load Logos onto Form
Private Sub loadLogoButton_Click()
 
 'Check to see if logo selected
 If logo <> "" Then
 
  'Check if valid File Type
  FileType = Right(logo, 3)
 
  If FileType = "jpg" Or FileType = "jpeg" Or FileType = "gif" Then
   
   'Load picture onto form
   LargeLogoInsert.PictureSizeMode = fmPictureSizeModeStretch
   SmallLogoInsert.PictureSizeMode = fmPictureSizeModeStretch
   LargeLogoInsert.Picture = LoadPicture(logo)
   SmallLogoInsert.Picture = LoadPicture(logo)

     
   'Set best Fit Picture Size
   aspect_ratio = LargeLogoInsert.Picture.Width / LargeLogoInsert.Picture.Height
   
   If LargeLogoInsert.Picture.Width > LargeLogoInsert.Picture.Height Then
    Large_bestfit_ratio = 200 / LargeLogoInsert.Picture.Width
    Small_bestfit_ratio = 70 / SmallLogoInsert.Picture.Width
   Else
    Large_bestfit_ratio = 95 / LargeLogoInsert.Picture.Height
    Small_bestfit_ratio = 44 / SmallLogoInsert.Picture.Height
   End If
   
   'Resert Size
    LargeLogoInsert.Width = LargeLogoInsert.Picture.Width * Large_bestfit_ratio
    LargeLogoInsert.Height = LargeLogoInsert.Picture.Height * Large_bestfit_ratio
    SmallLogoInsert.Width = SmallLogoInsert.Picture.Width * Small_bestfit_ratio
    SmallLogoInsert.Height = SmallLogoInsert.Picture.Height * Small_bestfit_ratio
     
   'Enter the Pictures Width/Height for Alteration
    LargeLogoWidth = FormatNumber(LargeLogoInsert.Picture.Width * Large_bestfit_ratio, 0)
    LargeLogoHeight = FormatNumber(LargeLogoInsert.Picture.Height * Large_bestfit_ratio, 0)
    SmallLogoWidth = FormatNumber(SmallLogoInsert.Picture.Width * Small_bestfit_ratio, 0)
    SmallLogoHeight = FormatNumber(SmallLogoInsert.Picture.Height * Small_bestfit_ratio, 0)
         
   'Set Spinner buttons
   With LargeLogoWidthSpinButton
    .Max = LargeLogoWidth
    .Min = 0
    .Value = LargeLogoWidth
   End With
   
   With LargeLogoHeightSpinButton
    .Max = LargeLogoHeight
    .Min = 0
    .Value = LargeLogoHeight
   End With
   
   With SmallLogoWidthSpinButton
    .Max = SmallLogoWidth
    .Min = 0
    .Value = SmallLogoWidth
   End With
   
   With SmallLogoHeightSpinButton
    .Max = SmallLogoHeight
    .Min = 0
    .Value = SmallLogoHeight
   End With

 
  Else
   MsgBox ("Please Select a Valid Image File (*.JPG, *.JPEG, *.GIF)")
   logo.Value = ""
  End If
 
 Else
     
   MsgBox ("Please Enter a Valid Image File (*.JPG, *.JPEG, *.GIF)")
   logo.Value = ""
     
 End If

--

Thanks for your help all.

Jesse
groundwar:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.