I am having trouble getting images to fit inside either an image box or a picture box. I want to scale the picture to fit in side the box, without distortion. I found a post here that suggested a subroutine, but I this appeared to do nothin when the picture was loaded. Can anyone help? having an imagebox inside a picturebox, I used this code:
Private Sub SetImageBoxSize (ImageBox As Image, _
Optional ImageReductionA mount As Long = 0)
Dim ParentRatio As Single
Dim PictureRatio As Single
Dim ContainerWidth As Single
Dim ContainerHeight As Single
Dim ContainerContro l As Control
With ImageBox
.Visible = False
PictureRatio = .Width / .Height
On Error Resume Next
ContainerWidth = .Container.Scal eWidth
If Err.Number Then
ContainerWidth = .Container.Widt h
ContainerHeight = .Container.Heig ht
Else
ContainerHeight = .Container.Scal eHeight
End If
ParentRatio = ContainerWidth / ContainerHeight
If ParentRatio < PictureRatio Then
.Width = ContainerWidth - 2 * ImageReductionA mount
.Height = .Width / PictureRatio
Else
.Height = ContainerHeight - 2 * ImageReductionA mount
.Width = .Height * PictureRatio
End If
.Move (ContainerWidth - .Width) \ 2, _
(ContainerHeigh t - .Height) \ 2
.Visible = True
End With
End Sub
It resizes the image box to a correct ratio, but the picture inside stays at it's full size so you only see part of it.
Private Sub SetImageBoxSize (ImageBox As Image, _
Optional ImageReductionA mount As Long = 0)
Dim ParentRatio As Single
Dim PictureRatio As Single
Dim ContainerWidth As Single
Dim ContainerHeight As Single
Dim ContainerContro l As Control
With ImageBox
.Visible = False
PictureRatio = .Width / .Height
On Error Resume Next
ContainerWidth = .Container.Scal eWidth
If Err.Number Then
ContainerWidth = .Container.Widt h
ContainerHeight = .Container.Heig ht
Else
ContainerHeight = .Container.Scal eHeight
End If
ParentRatio = ContainerWidth / ContainerHeight
If ParentRatio < PictureRatio Then
.Width = ContainerWidth - 2 * ImageReductionA mount
.Height = .Width / PictureRatio
Else
.Height = ContainerHeight - 2 * ImageReductionA mount
.Width = .Height * PictureRatio
End If
.Move (ContainerWidth - .Width) \ 2, _
(ContainerHeigh t - .Height) \ 2
.Visible = True
End With
End Sub
It resizes the image box to a correct ratio, but the picture inside stays at it's full size so you only see part of it.
Comment