Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

Resize an image, keeping aspect ratio


'this expands the size of an image control to the maximum
'dimension available, while keeping aspect ratio

'iWidth=width of picture and iHeight=height of picture in pixels

Dim wMax As Long , hMax As Long
'wMax is maximum physical width of control
'hMax is maximum physical height of control
'iWidth is width of image to be resized
'iHeight is height of image to be resized

'adjust size of control
Aspect = iWidth / iHeight
If iWidth > iHeight Then
    'width is bigger, aspect>1
   imgDisplay.Width = wMax
   imgDisplay.Height = imgDisplay.Width / Aspect
Else
    'height is bigger, aspect<1
   imgDisplay.Height = hMax
   imgDisplay.Width = imgDisplay.Height * Aspect
End If