Link to home
Start Free TrialLog in
Avatar of rito1
rito1

asked on

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

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,

Rit
/// <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;
                break;
            case 2:
                shtImageWidth = shtWidth;
                shtImageHeight = Convert.ToInt16(objImage.Height / ((double)objImage.Width / shtWidth));
                break;
            default:
                shtImageWidth = Convert.ToInt16(objImage.Width / ((double)objImage.Height / shtHeight));
                shtImageHeight = shtHeight;
                break;
        }

        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);

        objImage.Dispose();
        CropedImage.Dispose();
    }

Open in new window

Avatar of sudheeshthegreat
sudheeshthegreat

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?
Avatar of rito1

ASKER

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,

Rit
ASKER CERTIFIED SOLUTION
Avatar of sudheeshthegreat
sudheeshthegreat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rito1

ASKER

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

Much appreciated,

Rit
Avatar of rito1

ASKER

Your theory was perfect, thank you.

Rit