Click here to Skip to main content
Licence CPOL
First Posted 23 Feb 2006
Views 48,818
Downloads 201
Bookmarked 26 times

Image Resizing Maintaining Height/Width Ratio

By | 23 Feb 2006 | Article
This article explains how to dynamically resize an image in your web page to a given size, maintaining the original height/width ratio.

Introduction

This article explains how to dynamically resize an image in your web page to a given size, maintaining the original height/width ratio. If you have a fixed area where to put an image of an unknown size, you can resize the image to respect the desired width and height limits without stretching it.

Source code

You can download the source code, which contains a simple ASPX web page with three controls: two textboxes, where you can set the maximum width and height of the image, and a button that starts image loading. When you click the button, the page loads the given image (the one included is 450x45 pixels, you can change it programmatically) and resizes it to respect the given limits without stretching it.

The main function of the page is the following:

string COMMONFUNCTIONS_IMAGES_RESIZE_TO_TAG(System.Drawing.Image img, 
                       int MaxWidth, int MaxHeight)
{
    if (img.Width > MaxWidth || img.Height > MaxHeight)
    {
        double widthRatio = (double) img.Width / (double) MaxWidth;
        double heightRatio = (double) img.Height / (double) MaxHeight;
        double ratio = Math.Max(widthRatio, heightRatio);
        int newWidth = (int) (img.Width / ratio);
        int newHeight = (int) (img.Height / ratio);
        return " width=\"" + newWidth.ToString() + "\"" + 
               " height=\"" + newHeight.ToString() + "\" ";
    }
    else
    {
        return "";
    }
}

This function has the following parameters:

  • img: the System.Drawing.Image to resize.
  • MaxWidth: the maximum width (taken from the textbox).
  • MaxHeight: the maximum height (taken from the textbox).

It returns a string in the following format: " width="100" height="50" ".

You can put this piece of code where useful, for example, in an img tag.

How to run the app

Unzip and run the .aspx file using IIS, or another web server like Cassini or WebMatrix.

Feel free to use this code in your web pages.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Emanuele Briano

Web Developer

Italy Italy

Member

My name is Emanuele Briano, I am 23 years old. I'm an Industrial and Management Engineer from Italy.
I have expertize in: optimization of complex systems, production flow optimization, linear programming, statistical methods, simulation, design of experiments, business management.
I also operate in web consulting and web design. I realise professional web pages, static or dynamic (usually in ASP.NET), from a design based on usability to realisation and online marketing.
My Web Site (english) is http://www.emanuelebriano.it/e_default.aspx
My Web Site (Italian) is http://www.emanuelebriano.it

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNeed code to resize Image PinmembervalapdasiSandhya19:29 28 Oct '09  
AnswerUse this server-side resizing module instead. This code just changes the image attributes. [modified] PinmemberNathanael Jones10:30 6 Aug '08  
Generalthanx PinmemberZubair Alam20:34 23 Aug '07  
GeneralDoesn't resize the image - just changes attributes PinmemberKreyten15:41 14 Jun '07  
Generalexcelent article PinmemberautumnEND14:49 1 Feb '07  
GeneralNot working Pinmemberfhirzall3:41 22 Sep '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 24 Feb 2006
Article Copyright 2006 by Emanuele Briano
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid