iscode
asked on
Keep aspect ratio VB6
I am trying to make a Picturebox to keep the aspect ratio on a Form when Form resize.
I am not loading Image on the PictureBox. The Picturebox has Width = 360 and Height = 180,
when I make the Form wider or taller the Picturebox should size with the Form and always have same aspect ratio between Width and Height.
Code in Visual Basic 6
I am not loading Image on the PictureBox. The Picturebox has Width = 360 and Height = 180,
when I make the Form wider or taller the Picturebox should size with the Form and always have same aspect ratio between Width and Height.
Code in Visual Basic 6
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi IsCode,
I suspect a Picturebox can't "stretch" (resize) its image..
You might need to use an Imagebox instead (it can)
Set the Imagebox "Stretch" option = True
You might also need to set the forms :AutoRedraw option to True..
I suspect a Picturebox can't "stretch" (resize) its image..
You might need to use an Imagebox instead (it can)
Set the Imagebox "Stretch" option = True
You might also need to set the forms :AutoRedraw option to True..
Some how, determine what is the maximum width of the form. Then in the form resize event, don't allow the form to get larger than the calculated maximum width.
Private Sub Form_Resize()
If Me.Width > m_CalculatedMaximum Then
Me.Width = m_CalculatedMaximum
End If
End Sub
ASKER
Idle mind solution is in right path.
BrianVSoft:
Imagebox is not an option.
HooKooDooKu:
I dont want to control the Form size,
I want to restrict the Picturebox to keep aspect ratio but be all visible and resize on Form resize.
BrianVSoft:
Imagebox is not an option.
HooKooDooKu:
I dont want to control the Form size,
I want to restrict the Picturebox to keep aspect ratio but be all visible and resize on Form resize.
It should always be visible width-wise...are you saying you want it to become shorter and adjust the width instead if necessary?
ASKER
Yes it (the picturebox) should always be visible width-wise and height-wise
ASKER
if I shrink or pull the form width-wise or height-wise the picturebox should be visible but keep aspect ratio
Then what you really need to do is when the form resizes, determine the maximum height of the picturebox that will still fit in the form, and the maximum width of the picturebox that will still fit in the form. Then determine the ratio of the maximums. If the ratio is greater than the desired ratio, use the maximum heigth of the picturebox, and adjust the width to maintain the desired ratio, but if the ratio is less than the desired ratio, use the maximum width of the picturebox, and adjust the height to maintain the desired ratio.
protected m_Ratio as Double = 1.5 'Width is 1.5 times the height
Private Sub Form_Resize()
Dim MaxWidth as Integer
Dim MaxHeigth as Integer
Dim Ratio as Double
MaxWidth = Form.Width - m_FormWidthNotDevotedToPictureBox
MaxHeight = Form.Height - m_FormHeightNotDevotedToPictureBox
Ratio = CDbl( MaxWidth ) / CDbl( MaxHeight )
if Ratio < m_Ratio then
PictureBox.Width = MaxWidth
PictureBox.Height = CInt( CDbl( MaxWidth ) / m_Ratio )
else
PictureBox.Height = MaxHeight
PictureBox.Width = CInt( CDbl( MaxHeight ) * m_Ratio )
endif
end sub
I like HooKooDooKu's approach.
Here is another:
(untested as I don't have VB6 on this machine)
Here is another:
(untested as I don't have VB6 on this machine)
Option Explicit
Private pbWidthPercentage As Double
Private pbHeightPercentage As Double
Private pbAspectRatio As Double
Private Sub Form_Load()
pbWidthPercentage = Me.Picture1.Width / Me.Width
pbHeightPercentage = Me.Picture1.Height / Me.Height
pbAspectRatio = Me.Picture1.Height / Me.Picture1.Width
End Sub
Private Sub Form_Resize()
Me.Picture1.Width = Me.Width * pbWidthPercentage
Me.Picture1.Height = Me.Picture1.Width * pbAspectRatio
If Me.Picture1.Height > Me.Height Then
Me.Picture1.Height = Me.Height * pbHeightPercentage
Me.Picture1.Width = Me.Picture1.Height / pbAspectRatio
End If
End Sub
ASKER
HooKooDooKu:
I get error on first line :
protected m_Ratio as Double = 1.5 'Width is 1.5 times the height
I get error on first line :
protected m_Ratio as Double = 1.5 'Width is 1.5 times the height
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
On a side note...this type of thing is SUPER easy in .Net. Have you considered moving up to VB.Net? There is the free Express version...
ASKER
HooKooDooKu:
Would you put m_Ratio=1.5 in Form_Load ?
Idle_Mind:
Yes I will need to move on to VB.Net soon :-)
Would you put m_Ratio=1.5 in Form_Load ?
Idle_Mind:
Yes I will need to move on to VB.Net soon :-)
Yes, I left setting the ratio out of Form1_Load(). Ment to include the following:
m_Ratio = CDbl( PictureBox1.Width ) / CDbl( PictureBox1.Height )
That way, the ratio and position as setup in the IDE is maintained rather than hardcoding a ratio.
m_Ratio = CDbl( PictureBox1.Width ) / CDbl( PictureBox1.Height )
That way, the ratio and position as setup in the IDE is maintained rather than hardcoding a ratio.
ASKER
I think this is absolute solid solution HooKooDooKu, great programming!
I have to award Idle_Mind for his work too because I forgot to mention in topic that the solution had to
have picturebox visible at resize.
Thank you guys
I have to award Idle_Mind for his work too because I forgot to mention in topic that the solution had to
have picturebox visible at resize.
Thank you guys
ASKER
how can I prevent this?