I use the following routine to resize the picture box to display the image
centered within the original size of the picture box while maximizing the
image size as much as possible. Note that MxSz is the original size of the
picture box and the function returns the new size and location to which the
picture box can be set.
Private Function get_NewImgSize(ByVal OldImage As Image, ByVal MxSz As
Size) As Size
Dim w As Integer = OldImage.Width
Dim h As Integer = OldImage.Height
Dim nw, nh As Integer
Dim wtoh As Double = w / h
If (w >= MxSz.Width AndAlso h <= MxSz.Height) Then
nw = MxSz.Width
nh = CInt(nw / wtoh)
ElseIf h >= MxSz.Height AndAlso w <= MxSz.Width Then
nh = MxSz.Height
nw = CInt(nh * wtoh)
ElseIf w >= MxSz.Width AndAlso h >= MxSz.Height Then
If (w - MxSz.Width) >= (h - MxSz.Height) Then
nw = MxSz.Width
nh = CInt(nw / wtoh)
Else
nh = MxSz.Height
nw = CInt(nh * wtoh)
End If
Else
'Both are smaller
If (MxSz.Width - w) >= (MxSz.Height - h) Then
nw = MxSz.Width
nh = CInt(nw / wtoh)
Else
nh = MxSz.Height
nw = CInt(nh * wtoh)
End If
End If
Return New Size(nw, nh)
End Function
--
Dennis in Houston
"Aussie Rules" wrote:
Hi,
I have a picture box control on my winform, and it is of course fixed to a
certain height and width.
Users can load and view images of many sizes, and the only way I can deal
with this is to set the picture box sizemode to be centerimage. This does
however stretch the image out to the height and width of the control. which
usually screws up the image
is there a way to have the image display in the center where it is resizedd
keeping its perspective but showing the image in total?
Thanks