1

I'm trying to resize an image and save it with the following snippet code. It works fine but some images lose quality after the resize. When I checked, the original images looked fine and only the ones which were resized had a low quality. I don't know how I can improve the image quality while resizing it.

System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, MaxHeight, null, IntPtr.Zero);
// Clear handle to original file so that we can overwrite it if necessary
FullsizeImage.Dispose();
// Save resized picture
//NewImage.Save(NewFile);

if (fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".jpeg")
{
      NewImage.Save(NewFile, System.Drawing.Imaging.ImageFormat.Jpeg);
}

Please, help me. Thanks.

2 Answers 2

2

You can use this class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;

/// <summary>
/// Summary description for ResizeImage
/// </summary>
public class ResizeImage
{
    public static Image Resize(Image imgToResize, int h, int w)
    {
        Size size = new Size(w, h);

        int sourceWidth = imgToResize.Width;
        int sourceHeight = imgToResize.Height;

        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;

        nPercentW = ((float)size.Width / (float)sourceWidth);
        nPercentH = ((float)size.Height / (float)sourceHeight);

        if (nPercentH < nPercentW)
            nPercent = nPercentH;
        else
            nPercent = nPercentW;

        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);

        Bitmap b = new Bitmap(destWidth, destHeight);
        Graphics g = Graphics.FromImage((Image)b);

        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
        g.Dispose();

        return (Image)b;
    }
}

Also, You can use this code to option image quality:

graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality; 
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;

Like here.

Sign up to request clarification or add additional context in comments.

2 Comments

You should Dispose the Bitmap too. Rather than explicitly calling Dispose, a using statement is the preferred way of disposing because it guarantees clean-up in the case of an Exception occurring.
Ah. Perhaps disposing the Bitmap isn't such a good idea, seeing as that is what is returned. There should be a caveat to the user of this method that the returned Image should be disposed when finished.
0

With image resizing, you should also keep in mind few more things (rules of thumb, not a gospel as it depends on what you're doing etc.)...

  • keep stored in Db (or wherever) the largest image you can get your hands on (and providing your Db/storage can allow for that). i.e. you can make thumbnails on the fly or cache them or something, but the largest image is the 'raw model' sort of,
  • down-scale - if possible - don't scale up, as that's never going to be as good,
  • keep the 'proportion' of images,
  • be careful of the image manipulation, it has to be done right to avoid adding noise etc.
  • the image format you're using (for saving or temp format etc.) is also very important, that can also ruin your image, as different formats have different algorithms and make / sacrifice different parameters of your image (whether that is the colors or the details etc.),
  • use as little 'conversions' as possible - so keep the original, do simple scaling on it - and keep in memory while you can, e.g. don't save/load etc.

hope this helps,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.