Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 977
  • Last Modified:

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.
0
groundwar
Asked:
groundwar
1 Solution
 
twalgraveCommented:
LargeLogoInsert.Width
and
LargeLogoInsert.Height
0
 
groundwarAuthor Commented:
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.
0
 
bhagyeshtCommented:
Simple way out would be to have an image control with the stretch property set to true and the following code

Image1.Picture = Picture1.Picture
picture1.visible=false

then set the height and width of image to suit your requirements.


---
I believe in KISS (keep it short and simple)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
twalgraveCommented:
Here's some code you will want to look at:
http://www.vb-helper.com/howto_fit_picture_to_box.html
0
 
groundwarAuthor Commented:
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
0
 
CleanupPingCommented:
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now