in my computer don't have vb so i could check this code but u can check this.
but if u understand the concept u can easly correct it.
image box name is "img"
picture box name is "pic"
commond button name is "cmd"
place the image control into picturebox and change the image control streach property to true.
Private Sub cmd_Click()
img.width = img.picture.width
img.height = img.picture.height
if pic.width < img.width then
img.width = pic.width
img.height = img.height/(img.picture.width/img.width)
end if
if pic.height < img.height then
img.height = pic.height
img.width = img.width/(img.picture.height/img.height)
end if
img.left = 0
img.top = 0
End Sub
I have tried the following code and it works well...
Public Sub ScaleIMG(PicFle)
' This Sub-routine is designed as a physical add-in module
' {i.e. one which is copied into the parent application}
' PURPOSE
' The purpose of this subroutine is to proportionally scale any pictures
' down to the maximum size of a PictureBox Control inserted on a form,
' whilst maintaining the aspect ratio of the original picture! Pictures
' which are smaller than the PictureBox control are shown at their
' natural size.
' METHODOLOGY
' The Picture filename "PicFle" is derived within the main application
' and passed to this sub-routine as a parameter.
' The main application MUST HAVE:-
' =========
' 1. A Picture Box named <PicScale> on the default form whose
' width & height define the maximum dimensions of any image
' to be displayed.
' 2. An Image Box named <ImgScale> must be drawn WITHIN <PicScale>.
' This Image Box can be of any size and can lie anywhere within
' the Picture Box <PicScale>
' Pictures which are too large for the Picture Box will be scaled down to
' the dimensions of the Picture Box, whilst maintaining the aspect ratio
' of the image. Landscape oriented pictures will be constrained by the
' PicScale Width set at design time and portrait oriented pictures will
' be constrained to the height of PicScale. PicScale's dimensions are
' then changed to match the image being shown.
' Pictures which are smaller than PicScale will be shown at their natural
' size.
' ################################################## ########################
Static Init, DefH, DefW 'Static variables defined for this sub only
If Init = 0 Then
' ___This is the first time this session that this subroutine has been called
Init = 1 '<< Prevents this "If Then..." statement being called again this session
DefH = PicScale.Height '__ Save Original Picture Box Height at Design time value
DefW = PicScale.Width '__ Save Original Picture Box Width at Design time value
End If
'__ Restore Picture Box's Default Height & Width
PicScale.Height = DefH
PicScale.Width = DefW
'__Load Picture into ImageBox and stretch to it's natural size
ImgScale.Stretch = False
ImgScale.Picture = LoadPicture(PicFle)
ImgScale.Stretch = True
Iw = ImgScale.Width ' Width of picture loaded into Img
Ih = ImgScale.Height ' Height of Picture loaded into Img
Pw = PicScale.Width ' Width of Picture Box
Ph = PicScale.Height ' Height of Picture Box
If Iw > Ih Then
'____ Format = Landscape
Fcr = Pw / Iw ' Scale on width
Else
'____ Format = Square or Portait
Fcr = Ph / Ih ' Scale on height
End If
If Fcr < 1 Then
'__ i.e. The Image is larger than the Picture Box and we must
' shrink it, otherwise we leave it the same size!
ImgScale.Width = ImgScale.Width * Fcr
ImgScale.Height = ImgScale.Height * Fcr
End If
'__Re-assign PictureBox dimensions to fit the image
PicScale.Width = ImgScale.Width
PicScale.Height = ImgScale.Height
'__Position the Top LH corner of the ImageBox
' into the top LH corner of the PictureBox
ImgScale.Left = 0
ImgScale.Top = 0
ImgScale.Visible = True
End Sub