hey
i ve an imagebox height=5820 width=6400 which dynamically loads any image. now the problem is when i set its stretch property true it displays the pic but it appear strectched and distorted.
if i use the following code it displays the pic in its original size....bt the dimensions of my imagebox change.

Dim Left As Long, Top As Long, Width As Long, Height As Long
Dim AspectRate As Double
imgPhoto.Visible = False
imghoto.Stretch = False

AspectRate = imghoto.Width / imgPhoto.Height
If AspectRate > imgPhoto.Container.ScaleWidth / imgPhoto.Container.ScaleHeight Then

'Picture is Wide
Width = imgPhoto.Container.ScaleWidth
Height = Width / AspectRate
Left = (imgCandidatePhoto.Width / 2 - imgPhoto.Container.ScaleWidth / 2) / 2

Else
'Picture is High
Height = img Dim Left As Long, Top As Long, Width As Long, Height As Long
Dim AspectRate As Double
imgCandidatePhoto.Visible = False
imgCandidatePhoto.Stretch = False

AspectRate = imgCandidatePhoto.Width / imgCandidatePhoto.Height
If AspectRate > imgCandidatePhoto.Container.ScaleWidth / imgCandidatePhoto.Container.ScaleHeight Then
'Picture is Wide
Width = imgCandidatePhoto.Container.ScaleWidth
Height = Width / AspectRate
Left = (imgCandidatePhoto.Width / 2 - imgCandidatePhoto.Container.ScaleWidth / 2) / 2

Else
'Picture is High
Height = imgCandidatePhoto.Container.ScaleHeight
Width = Height * AspectRate
Left = (imgCandidatePhoto.Container.ScaleWidth / 2 - imgCandidatePhoto.Width / 2) / 2

End If

imgPhoto.Move Left, Top, Width, Height
imgPhoto.Visible = True


Top=60

imgCandidatePhoto.Move Left, Top, Width, Height
imgCandidatePhoto.Visible = True

How can i display my images in the fixed imagebox dimensions without distorting the image?

Recommended Answers

All 3 Replies

Unfortunately this is the crux of an image control. a Picturebox tends to keep its aspect ratios much better, so try a picturebox and see if it helps. Below is some code to fit a picture to a picturebox, which gives it the same property as the stretch in an image control -

Public Function FitPictureToBox(picLarge As PictureBox)

picLarge.ScaleMode = 3
picLarge.AutoRedraw = True

picLarge.PaintPicture picLarge.Picture, _
    0, 0, picLarge.ScaleWidth, picLarge.ScaleHeight, _
    0, 0, _
    picLarge.Picture.Width / 26.46, _
    picLarge.Picture.Height / 26.46
End Function

Now call it from a form -

Call FitPictureToBox Picture1

bt picture paint doesnt work with inagebox...n i have to use image box only

You need to calculate its aspect ratio...

Option Explicit

Private Sub Form_Load()
Dim P As StdPicture, AspectRatio As Double
Set P = LoadPicture("your image path file name here")
If P.Width > P.Height Then
  AspectRatio = P.Height / P.Width
  IMG.Width = Me.ScaleWidth - 60
  IMG.Move 30, 30, Me.ScaleWidth - 60, (IMG.Width * AspectRatio)
ElseIf P.Width < P.Height Then
  AspectRatio = P.Width / P.Height
  IMG.Height = Me.ScaleHeight - 60
  IMG.Move 30, 30, (IMG.Height * AspectRatio), Me.ScaleHeight - 60
Else
  AspectRatio = 1
  IMG.Height = Me.ScaleHeight - 60
  IMG.Move 30, 30, (IMG.Height * AspectRatio), Me.ScaleHeight - 60
End If
IMG.Picture = P
End Sub

Good Luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.