Stretch property of the Picturebox doesn't strech very well.

yousuf42

Well-known member
Joined
Feb 12, 2006
Messages
101
Programming Experience
1-3
Dear All,

I have a picturebox control on a form. But the Stretch property of the picturebox control doesn't strech very well - i.e. it distorts the Aspect Ratio to fit the control size. So some pictures look ugly on the form. Can you help with sample code please.

Thanks in advance
 
.Net 1.1:
VB.NET:
Sub setimage()
  [COLOR=green]'set these two properties in Designer instead[/COLOR]
  PictureBox1.BorderStyle = BorderStyle.FixedSingle
  PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
  [COLOR=green]'get an image[/COLOR]
  Dim img As Image = Image.FromFile(Application.StartupPath & "\Some.jpg")
  [COLOR=green]'display the scaled image[/COLOR]
  PictureBox1.Image = autofitImage(img, PictureBox1.Size)
End Sub
 
Function autofitImage(ByVal img As Image, ByVal limits As Size) As Image
  Dim sz As New Size(img.Width, img.Height)
  [COLOR=green]'scale image if unscaled doesn't fit[/COLOR] 
  If sz.Height > limits.Height Or sz.Width > limits.Width Then
    Dim scale As Double = Math.Max(sz.Width / limits.Width, sz.Height / limits.Height)
    sz.Width /= scale
    sz.Height /= scale
  End If
  [COLOR=green]'return scaled image[/COLOR]
  Return img.GetThumbnailImage(sz.Width, sz.Height, Nothing, Nothing)
End Function
.Net 2.0 has got a Zoom SizeMode that work the same for aspect ratio.

yousuf42, please update Primary Platform in your user profile or state the Framework version when you ask questions.
 
Option strict on disallows implicit conversions from Double to Integer

Thanks a lot for giving your valuable time. But following error message appears on the code.


sz.Width /= scale '
sz.Height /= scale

Option strict on disallows implicit conversions from Double to Integer.

Thanks

 
Strict is boring. Is this better in your strict:
sz.Width \= scale
sz.Height \= scale
 
Now it is 'Option strict on disallows implicit conversion from 'Double to Long'

Now it is 'Option strict on disallows implicit conversions from 'Double to Long'

sz.Width \= scale
sz.Height \= scale
 
So what is the solution, yousuf42? Perhaps you should turn Option Strict Off if you can't handle it? Or perhaps you should try to declare Scale As Integer since you don't need it precise?
 
Convert into Integer results in loss of picture quality

Thanks a lot.
Finally, I tried with CInt(Scale)as below and it works. But the picture quality is not well with conversion into integer.

sz.Width \=
CInt(scale)
sz.Height \=
CInt(scale)

also we can't declate scale as integer.
 
Picture stretch quality has nothing to do with whether size is integer or double precision. Blame GetThumbnailImage, but I can't say I have noticed it before..

Why can't you declare scale as integer with strict? You can do the same \ integer division there so Math.Max(int,int) overload will return integer.
 
Resolved

Thanks Mr. John.

Finally it is working with this code. Getthumbnailimage gives poor quality images.

Const PICTUREBOX_WIDTH As Int32 = 352
Const PICTUREBOX_HEIGHT As Int32 = 304

Private
Sub Fit()

With pic
.Width = PICTUREBOX_WIDTH
.Height = PICTUREBOX_HEIGHT
.SizeMode = PictureBoxSizeMode.StretchImage
End With
' If Fit was called by the Zoom In button, then center the image. This is
' only needed when working with images that are smaller than the PictureBox.
' Feel free to uncomment the line that sets the SizeMode and then see how
' it causes Zoom In for small images to show unexpected behavior.
If pic.Image.Width < pic.Width And _
pic.Image.Height < pic.Height
Then
'If Not IsFitForZoomIn Then
' pic.SizeMode = PictureBoxSizeMode.CenterImage
'End If
End If
CalculateAspectRatioAndSetDimensions()
End Sub

Private Function CalculateAspectRatioAndSetDimensions() As Double
' Calculate the proper aspect ratio and set the image's dimensions.
Dim ratio As Double
If pic.Image.Width > pic.Image.Height Then
ratio = pic.Image.Width / _
pic.Image.Height
pic.Height =
CInt(CDbl(pic.Width) / ratio)
Else
ratio = pic.Image.Height / _
pic.Image.Width
pic.Width =
CInt(CDbl(pic.Height) / ratio)
End If
Return ratio
End Function
 
Back
Top