keeping aspect ratio when resizing an image

I currently have the following for resizing my image:
newgraphics.DrawImage(oldbmp, 0, 0, newsize.Width, newsize.Height)

I am setting the new width and height.

What I want to do is be able to set the new width of the image and let the height of the image adjust automatically to keep the aspect ratio.

Is this possible and if so can sum1 provide a change to my code example.

Thanks in advance
scm0smlAsked:
Who is Participating?
 
omegaomegaConnect With a Mentor DeveloperCommented:
Hello, scm0sml,

Since you know the width you want, you could just calculate the corresponding height, vis:

        Dim NewHeight As Integer = CInt(oldbmp.Height * (newsize.Width / oldbmp.Width))
        newgraphics.DrawImage(oldbmp, 0, 0, newsize.Width, NewHeight)

Cheers,
Randy

 
0
 
Juan_BarreraConnect With a Mentor Commented:
Hi, you can just define a fixed ratio and then calculate the new dimensions, like this:


0
 
scm0smlAuthor Commented:
I wanted to allow the width of the image to control the aspect ration so have gone with the 2nd answer.

cheers
0
All Courses

From novice to tech pro — start learning today.