How to Resizing an Image to a Set Maximum Width and Set Maximum Height

rito1 used Ask the Experts™
Hi All,

I have the attached function which at the moment enables a user to resize an image using either:

1. Set dimensions
2. Set width and proportionate height
3. Set height and proportionate width

It works great for the above ratios but can anyone help me enhance the function to include an option to set the dimensions to a max height and max width rather than either dimensions needing to be of a set size?

Many thanks,

/// <summary>
    /// Create a resized version of image supplied
    /// </summary>
    /// <param name="strImagePath">Full path of image</param>
    /// <param name="strFileName">Filename of image</param>
    /// <param name="strExtension">Extension of file being manipulated</param>
    /// <param name="strImageType">a prefix used to create instance of file e.g. thumb_, medium_ or large_</param>
    /// <param name="shtWidth">Width</param>
    /// <param name="shtHeight">Height</param>
    /// <param name="intSizeRatio">1 = fixed size, 2 = proportion max width, 3 = proportion max height</param>
    public static void ImageSizing(string strImagePath, string strFileName, string strExtension, string strImageType, short shtWidth, short shtHeight, int intSizeRatio)
        short shtImageWidth;
        short shtImageHeight;

        System.Drawing.Image objImage = System.Drawing.Image.FromFile(strImagePath + "source_" + strFileName + strExtension);

        switch (intSizeRatio)
            case 1:
                shtImageWidth = shtWidth;
                shtImageHeight = shtHeight;
            case 2:
                shtImageWidth = shtWidth;
                shtImageHeight = Convert.ToInt16(objImage.Height / ((double)objImage.Width / shtWidth));
                shtImageWidth = Convert.ToInt16(objImage.Width / ((double)objImage.Height / shtHeight));
                shtImageHeight = shtHeight;

        System.Drawing.Image CropedImage = new Bitmap(shtImageWidth, shtImageHeight);
        using (Graphics G = Graphics.FromImage(CropedImage))
            G.SmoothingMode = SmoothingMode.Default;
            G.InterpolationMode = InterpolationMode.HighQualityBicubic;
            G.PixelOffsetMode = PixelOffsetMode.HighQuality;
            G.DrawImage(objImage, 0, 0, shtImageWidth, shtImageHeight);

        CropedImage.Save(strImagePath + strImageType + "_" + strFileName + strExtension);


Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
For an image file, there is no maximum as such defined for the height or width. When you mean maximum, is it based on the user's dekstop/browser resolution, or is the max of either the height or width?

Let me put down the 3 functionalities you currently have.
1. For an existing image file of height h and width w, change the width to x and height to y.
2. For an existing image file of height h and width w, change the width to x and the height proportionately.
3. For an existing image file of height h and width w, change the height to y and the width proportionately.
I see that for options 2 and 3, the height to width ratios remain the same after change.

To help me explain what option you are trying to add here, can you please provide a similar sentence (as above) for the functionality you are trying to add?


Hi, Thanks for your response.

What I am trying to achieve is:

For an existing image file of height h and width w, if bigger than specified x and y change the width to w and height to h to fit x and y but maintain aspect ratio.

Here is another way of describing it too:

Haven't you always wanted to have an image where you could specify the maximum width and height, such that the image would draw it's correct size if it were smaller than those limits? But if it were larger in either dimension (or both), it would be scaled down to fit your specified limits, while maintaining proper aspect ratio?

I think Adobe fireworks refers to it as - Scale to fit area

I hope this helps.

many thanks,

Ok, I think I understand what you need now.
Consider the current image height and width to be h and w. Now, your option 4 to maximize this image to height x and width y:
1. Calculate ratios of x/w and y/h.
2. Take the lesser of the 2 ratios. Apply this on both h and w.
For example, take an image of h=20 and x=20
With x=80 and y=40, ratios are
x/w=4 and y/h=2. So, smaller ratio is 2. Apply this on both h and w.
New height = 20*2 = 40 and new width = 20*2 = 40.
This is the max the image could scale up to within x and y.


Thanks sudheeshthegreat, I am just trying this out now. I will come back to you with the result.

Much appreciated,



Your theory was perfect, thank you.


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial