How to proportional resize image of any type in .NET?

23,630

Solution 1

First and foremost, you're not grabbing the CURRENT height and width of the image. In order to resize proportionately you'll need to grab the current height/width of the image and resize based on that.

From there, find the greatest attribute and resize proportionately based on that.

For instance, let's say the current image is 800 x 600 and you wanna resize proportionately within a 400 x 400 space. Grab the greatest proportion (800) and find it's ratio to the new size. 800 -> 400 = .5 Now take that ratio and multiply by the second dimension (600 * .5 = 300).

Your new size is 400 x 300. Here's a PHP example (sorry....you'll get it though)

$thumb_width = 400;
$thumb_height = 400;

$orig_w=imagesx($src_img); 
$orig_h=imagesy($src_img);      

if ($orig_w>$orig_h){//find the greater proportion
    $ratio=$thumb_width/$orig_w; 
    $thumb_height=$orig_h*$ratio;
}else{
    $ratio=$thumb_height/$orig_h; 
    $thumb_width=$orig_w*$ratio;
}

Solution 2

I think your code is fine, but taking in the width and the height as parameters is where you're going wrong in my opinion. Why should the caller of this method have to decide how big they want the width and the height? I would suggest changing it to a percentage:

public bool ResizeImage(string fileName, string imgFileName,
    ImageFormat format, int percent)
{
    try
    {
        using (Image img = Image.FromFile(fileName))
        {
            int width = img.Width * (percent * .01);
            int height = img.Height * (percent * .01);
            Image thumbNail = new Bitmap(width, height, img.PixelFormat);
            Graphics g = Graphics.FromImage(thumbNail);
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            Rectangle rect = new Rectangle(0, 0, width, height);
            g.DrawImage(img, rect);
            thumbNail.Save(imgFileName, format);
        }
        return true;
    }
    catch (Exception)
    {
        return false;
    }
}
Share:
23,630
eKek0
Author by

eKek0

I'm a software developer living in Santiago del Estero, one of the smallest towns in Argentina. I'm also a system analyst, and program in Delphi and ASP.NET with C#. Currently, I'm working in a bank developing in-house software.

Updated on April 01, 2020

Comments

  • eKek0
    eKek0 about 4 years

    Is possible to resize image proportionally in a way independent of the image type (bmp, jpg, png, etc)?

    I have this code and know that something is missing (but don't know what):

    public bool ResizeImage(string fileName, string imgFileName,
        ImageFormat format, int width, int height)
    {
        try
        {
            using (Image img = Image.FromFile(fileName))
            {
                Image thumbNail = new Bitmap(width, height, img.PixelFormat);
                Graphics g = Graphics.FromImage(thumbNail);
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                Rectangle rect = new Rectangle(0, 0, width, height);
                g.DrawImage(img, rect);
                thumbNail.Save(imgFileName, format);
            }
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
    

    If not possible, how can I resize proportional a jpeg image?

    I know that using this method, but don't know where to put this (!).

  • ctorx
    ctorx almost 12 years
    It might be very relevant for the caller to know the exact dimension they want the image to be...for example, if I have an image that is 1024X768 and I want it scaled down to 50X50 for a thumbnail, I shouldn't have to calculate the percentage. Personally, I would standardize on width/height and provide a percentage overload that ultimately calls out to the width/height version. Best of both and not a lot of extra code.
  • David McLean
    David McLean over 11 years
    I know this is an old comment but.. I would personally provide width or height as the constraint and maintain the aspect ration so the images doesn't get all distorted. You may even just want it to resize the largest edge to 50 no matter if that's the height or width.