Search code examples
vb.netmathpictureboxaspect-ratio

vb.net multiple pictureBoxes, same width, maintain aspect ratio


I have a global variable called fWidth that is currently 300.

I'm setting pictureBox.width = fWidth, but then I want to do something like pictureBox.height = calcImgHeight(originalHeight,originalWidth)

This is where the problem comes in, because I'm terrible at math :D

Private Function calcImgHeight(oHeight, oWidth)
    Dim nHeight, ratio As Integer
    ratio = oWidth / fWidth
    nHeight = oHeight / ratio

    Return nHeight
End Function

What should the correct code be?


Solution

  • I bet I'm worse at maths than you, but fortunately I'm good at Googling.

    http://andrew.hedges.name/experiments/aspect_ratio/

    original height / original width x new width = new height