keeping aspect ratio when resizing an image

Posted on 2008-11-16
Last Modified: 2012-05-05
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
Question by:scm0sml
    LVL 20

    Assisted Solution

    Hi, you can just define a fixed ratio and then calculate the new dimensions, like this:

    LVL 12

    Accepted Solution

    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)



    Author Closing Comment

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


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now