Image resizing

0

I'm using the following routine to resize images, it's working, however the resulting files are getting a very large size, for example when I resize an image from 1400x700 from 500kb to 1200x600 the new file is getting 1.6 Mb. can be wrong?

Main Routine:

uploadedFile.SaveAs(Server.MapPath(FilePath));
System.Drawing.Image img_original = 
System.Drawing.Image.FromFile(Server.MapPath(FilePath));
System.Drawing.Image img_resized = Funcoes.ResizeImage(img_original, new Size(1200, 600), true);

ImageResize function:

    public static System.Drawing.Image ResizeImage(System.Drawing.Image image, Size size,
    bool preserveAspectRatio = true)
    {
        int newWidth;
        int newHeight;
        if (preserveAspectRatio)
        {
            int originalWidth = image.Width;
            int originalHeight = image.Height;
            float percentWidth = (float)size.Width / (float)originalWidth;
            float percentHeight = (float)size.Height / (float)originalHeight;
            float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
            newWidth = (int)(originalWidth * percent);
            newHeight = (int)(originalHeight * percent);
        }
        else
        {
            newWidth = size.Width;
            newHeight = size.Height;
        }
        System.Drawing.Image newImage = new Bitmap(newWidth, newHeight);
        using (Graphics graphicsHandle = Graphics.FromImage(newImage))
        {
            graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
        }
        return newImage;
    }
    
asked by anonymous 29.04.2017 / 23:01

1 answer

0

What's missing from your question is how you're saving your image. It is possible that you are saving in .bmp? :)

Follow your code, slightly modified to test different formats:

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace RedimensionarImagens
{
    class Program
    {
        private static string FilePath = "stack.png";

        static void Main(string[] args)
        {
            Image img_original = Image.FromFile(FilePath);
            Image img_resized = ResizeImage(img_original, new Size(50, 50), true);
            img_resized.Save("minhaImagemMenor.bmp", ImageFormat.Bmp);
            img_resized.Save("minhaImagemMenor.png", ImageFormat.Png);
            img_resized.Save("minhaImagemMenor.jpg", ImageFormat.Jpeg);

        }

        public static Image ResizeImage(System.Drawing.Image image, Size size,
            bool preserveAspectRatio = true)
        {
            int newWidth;
            int newHeight;
            if (preserveAspectRatio)
            {
                int originalWidth = image.Width;
                int originalHeight = image.Height;
                float percentWidth = (float)size.Width / (float)originalWidth;
                float percentHeight = (float)size.Height / (float)originalHeight;
                float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
                newWidth = (int)(originalWidth * percent);
                newHeight = (int)(originalHeight * percent);
            }
            else
            {
                newWidth = size.Width;
                newHeight = size.Height;
            }
            System.Drawing.Image newImage = new Bitmap(newWidth, newHeight);
            using (Graphics graphicsHandle = Graphics.FromImage(newImage))
            {
                graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
            }
            return newImage;
        }
    }
}

Result:
  

The original image was 316x316 - see that in .bmp, at size 50x50, it almost reached the original size in .png!

    
30.04.2017 / 01:31