Image resizing

Asked

Viewed 45 times

0

I am using the following routine below to resize images, it is 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. What could 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;
    }

1 answer

0

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

Follow your code, slightly altered 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;
        }
    }
}


Upshot: lista das imagens redimensionadas e seus tamanhos finais

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

  • I also imagined that it could be the format, I added the parameter with the format Imageformat.Jpeg but at first I had no result, after doing several tests, I realized that other image files were getting correct, Then I did some more tests using the same image, saving with a different name was getting correct, I believe there was some kind of cache when saved with the same name. To conclude, I think the problem was the lack of the parameter with the same format.

  • @Lucianobeguelini If you think the answer really solves your problem, please mark it as accepted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.