user3865905
user3865905

Reputation: 15

decreasing dimensions images without increases size .net

I'm trying to decrease images dimensions, example 100px X 100px to 50px X 50px. I try some code but it increases the size of image, example 270kb to 700kb. Any idea?

I try two methods and have the same result:

 public void ResizeImage()
    {
        Image img = Image.FromFile("~/Images/image.jpg");

        double imgHeight = img.Size.Height;
        double imgWidth = img.Size.Width;

        double x = 0.5;
        //New sizes
        int newWidth = Convert.ToInt32(imgWidth * x);
        int newHeight = Convert.ToInt32(imgHeight * x);

        Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
        Image myThumbnail = img.GetThumbnailImage(newWidth, newHeight, myCallback, IntPtr.Zero);

        //Save
        myThumbnail.Save("~/Images/image.jpg");

    }

    public bool ThumbnailCallback()
    {
        return false;
    }


    public void ResizeImage2()
    {
        Image img = Image.FromFile("~/Images/image.jpg");

        double imgHeight = img.Size.Height;
        double imgWidth = img.Size.Width;

        double x = 0.5;
        //Seteo nuevos tamaños
        int newWidth = Convert.ToInt32(imgWidth * x);
        int newHeight = Convert.ToInt32(imgHeight * x);


        Bitmap b = new Bitmap(newWidth, newHeight);
        Graphics g = Graphics.FromImage((Image)b);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;

        g.DrawImage(img, 0, 0, newWidth, newHeight);
        g.Dispose();

        b.Save("~/Images/image.jpg");


    }

Thanks

Upvotes: 0

Views: 98

Answers (3)

Artiom
Artiom

Reputation: 7847

you are saving it as bmp. Set compression. http://msdn.microsoft.com/ru-ru/library/9t4syfhh(v=vs.110).aspx Also you can read this post about compression quality High quality JPEG compression with c#

For all overloads look here http://msdn.microsoft.com/ru-ru/library/System.Drawing.Image.Save(v=vs.110).aspx

Just compression

bmp.Save("c:\\button.gif", System.Drawing.Imaging.ImageFormat.Jpeg);

saving with quality settings

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); 
ImageCodecInfo ici = null; 

foreach (ImageCodecInfo codec in codecs)
{ 
    if (codec.MimeType == "image/jpeg") 
    ici = codec; 
} 

EncoderParameters ep = new EncoderParameters(); 
ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100); 
bm.Save("C:\\quality" + x.ToString() + ".jpg", ici, ep);

Upvotes: 2

Pablo Romeo
Pablo Romeo

Reputation: 11406

You are saving the image without specifying the format, try using the other overload of Bitmap.Save(): http://msdn.microsoft.com/en-us/library/ms142147(v=vs.110).aspx

such as:

b.Save("~/Images/image.jpg", ImageFormat.Jpeg);

For image resizing I usually use the following helper method which supports keeping aspect ratio and padding. Original source: https://stackoverflow.com/a/9301367/1373170

I changed it to use streams, for my own convenience:

    public static void ResizeImage(Image image, Stream outputStream, int maximumWidth, int maximumHeight, bool enforceRatio, bool addPadding)
    {
        var canvasWidth = maximumWidth;
        var canvasHeight = maximumHeight;
        var newImageWidth = maximumWidth;
        var newImageHeight = maximumHeight;
        var xPosition = 0;
        var yPosition = 0;

        if (enforceRatio)
        {
            var ratioX = maximumWidth / (double)image.Width;
            var ratioY = maximumHeight / (double)image.Height;
            var ratio = ratioX < ratioY ? ratioX : ratioY;
            newImageHeight = (int)(image.Height * ratio);
            newImageWidth = (int)(image.Width * ratio);

            if (addPadding)
            {
                xPosition = (int)((maximumWidth - (image.Width * ratio)) / 2);
                yPosition = (int)((maximumHeight - (image.Height * ratio)) / 2);
            }
            else
            {
                canvasWidth = newImageWidth;
                canvasHeight = newImageHeight;
            }
        }

        var thumbnail = new Bitmap(canvasWidth, canvasHeight);
        var graphic = Graphics.FromImage(thumbnail);

        if (enforceRatio && addPadding)
        {
            graphic.Clear(Color.White);
        }

        graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphic.SmoothingMode = SmoothingMode.HighQuality;
        graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
        graphic.CompositingQuality = CompositingQuality.HighQuality;
        graphic.DrawImage(image, xPosition, yPosition, newImageWidth, newImageHeight);

        thumbnail.Save(outputStream, ImageFormat.Png);
    }

Note: This code uses PNG encoding, but that can easily be changed to be a parameter to make it configurable.

Upvotes: 0

HypeZ
HypeZ

Reputation: 4127

You just need to set save format to jpeg and choose a different quality level. In a few tries you can find your right quality!

Code example (taken from http://msdn.microsoft.com/it-it/library/ytz20d80(v=vs.110).aspx )

// Save the bitmap as a JPEG file with quality level 25.
myEncoderParameter = new EncoderParameter(myEncoder, 25L);
myEncoderParameters.Param[0] = myEncoderParameter;
myBitmap.Save("Shapes025.jpg", myImageCodecInfo, myEncoderParameters);

Upvotes: 0

Related Questions