VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Image Resize / Aspect Ratio

by Zecho (1 Submission)
Category: Graphics
Compatability: Visual Basic 5.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (6 Votes)

If you want to load an image and resize it maintaining the original aspect ratio, this is simple, short and works. Coded in VB6 but is most likely compatible with previous versions.
Start with an Image control INSIDE a PictureBox with the default names (Image1, Picture1).
There are two versions of the sub one with and one without comments.
Call this sub when you load a picture and from the Form_Resize() if your Picture1 resizes with the form.

Rate Image Resize / Aspect Ratio


'~ Commented version -- Uncommented version is below
'~ Simply to show how short and easy this really is
Private Sub Reset_Image()
Image1.Visible = False
If Image1.Picture Then
'~ this is used in case the image changes
'~ if it's not used, the image control is
'~ still the same size as the previous pic
  Image1.Height = Image1.Picture.Height
  Image1.Width = Image1.Picture.Width
  If Image1.Picture.Height > Image1.Picture.Width Then
'~ the Pic is taller than wide
    Image1.Height = Picture1.Height
    Image1.Width = Image1.Width / (Image1.Picture.Height / Image1.Height)
    
    If Image1.Width > Picture1.Width Then
'~ If the PictureBox isn't square, the pic still may be larger than it
      Image1.Width = Picture1.Width
      Image1.Height = Image1.Picture.Height / (Image1.Picture.Width / Image1.Width)
    End If
  End If
  If Image1.Picture.Width > Image1.Picture.Height Then
'~ Image is wider than tall
    Image1.Width = Picture1.Width
    Image1.Height = Image1.Height / (Image1.Picture.Width / Image1.Width)
    If Image1.Height > Picture1.Height Then
      Image1.Height = Picture1.Height
      Image1.Width = Image1.Picture.Width / (Image1.Picture.Height / Image1.Height)
    End If
  End If
  
'~ Center Image1 within Picture1
  Image1.Left = (Picture1.Width / 2) - (Image1.Width / 2)
  Image1.Top = (Picture1.Height / 2) - (Image1.Height / 2)
  Image1.Visible = True
End If
End Sub



'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
'~ UN-Commented version of the same sub
'~ Don't use both at the same time
Private Sub Reset_Image()
Image1.Visible = False
If Image1.Picture Then
  Image1.Height = Image1.Picture.Height
  Image1.Width = Image1.Picture.Width
  If Image1.Picture.Height > Image1.Picture.Width Then
    Image1.Height = Picture1.Height
    Image1.Width = Image1.Width / (Image1.Picture.Height / Image1.Height)
    If Image1.Width > Picture1.Width Then
      Image1.Width = Picture1.Width
      Image1.Height = Image1.Picture.Height / (Image1.Picture.Width / Image1.Width)
    End If
  End If
  If Image1.Picture.Width > Image1.Picture.Height Then
    Image1.Width = Picture1.Width
    Image1.Height = Image1.Height / (Image1.Picture.Width / Image1.Width)
    If Image1.Height > Picture1.Height Then
      Image1.Height = Picture1.Height
      Image1.Width = Image1.Picture.Width / (Image1.Picture.Height / Image1.Height)
    End If
  End If
  Image1.Left = (Picture1.Width / 2) - (Image1.Width / 2)
  Image1.Top = (Picture1.Height / 2) - (Image1.Height / 2)
  Image1.Visible = True
End If
End Sub

Download this snippet    Add to My Saved Code

Image Resize / Aspect Ratio Comments

No comments have been posted about Image Resize / Aspect Ratio. Why not be the first to post a comment about Image Resize / Aspect Ratio.

Post your comment

Subject:
Message:
0/1000 characters