4

I'am using this code to resize image. But result is not fine, I want to best quality. I know its low quality because I also resize same image with photoshop and result is different so better. How do I fix it?

private static Image resizeImage(Image imgToResize, Size size)
    {
        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;
    }
beratuslu
  • 1,101
  • 3
  • 14
  • 27
  • I know its just resizing but its worth it to say you are comparing a javascript function to a 1000$ professional graphics software. So its possible you are getting different results. – Iznogood Aug 27 '10 at 13:02
  • its not javascript function its c# and I know that there is a way to do it with c# – beratuslu Aug 27 '10 at 13:25
  • The [ImageResizing.Net](http://imageresizing.net) library will give you the best quality, and handles [the 29+ image resizing pitfalls](http://nathanaeljones.com/163/20-image-resizing-pitfalls/) properly. Take a look at the latter link for tips on improving your quality - look at the former if you want a free, open-source solution that handles all the edge cases for you. – Lilith River Aug 02 '11 at 23:15

1 Answers1

3

This is the routine I use. Perhaps you'll find it useful. It is an extension method, to boot. The only difference is that I omit the code to preserve the aspect ratio, which you could just as easily plug in.

public static Image GetImageHiQualityResized(this Image image, int width, int height)
{
    var thumb = new Bitmap(width, height);
    using (var g = Graphics.FromImage(thumb))
    {
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.InterpolationMode = InterpolationMode.High;
        g.DrawImage(image, new Rectangle(0, 0, thumb.Width, thumb.Height));
        return thumb;
    }
}

Example usage of this extension method could include:

// Load the original image
using(var original = Image.FromFile(@"C:\myimage.jpg"))
using(var thumb = image.GetImageHiQualityResized(120, 80))
{
    thumb.Save(@"C:\mythumb.png", ImageFormat.Png);
}

Notes

The difference between the default JPG encoding and the default PNG encoding is, indeed, very different. Below are two thumbs using your example, one saved with ImageFormat.Png and one with ImageFormat.Jpeg.

PNG Image

PNG Image

JPEG Image

JPEG Image

You may find the work done by the original poster in this question to be helpful if you determine that you absolutely must use JPEG. It involves configuring the image codec and encoding parameters to high-quality settings. .NET Saving jpeg with the same quality as it was loaded

Were it me, I'd just as soon use the PNG format, as it is lossless.

Community
  • 1
  • 1
kbrimington
  • 25,142
  • 5
  • 62
  • 74
  • @berotomanya: Interesting. I've always been happy with the quality of this routine. It would be helpful if you could post an example of an image that is not resizing to your satisfaction, compared with the PhotoShop results. Also, it would be interesting to know what format you save your images in. Should you be using the lossy .jpg format, that could be the source of the quality woes. – kbrimington Aug 27 '10 at 13:43
  • 1. Original Image: http://img841.imageshack.us/img841/147/pron1.jpg 2. Photoshop resize: http://img835.imageshack.us/img835/9956/photoshopc.jpg 3. Programmatic resize: http://img198.imageshack.us/img198/5216/270as.jpg As you can see the size of image that resized by Photoshop is 17kb, on the other hand programmatic one is 4kb. and sure quality obviously poor(especially when your customer is a bridal house). – beratuslu Aug 27 '10 at 14:08
  • @berotomanya: Imageshack is blocked at my work. I'll take a look when I get home. In the mean time, change out your save code so that it uses the `ImageFormat.Png` instead. That will prevent you from loss due to the .JPG image format. – kbrimington Aug 27 '10 at 17:37
  • how do I change my code to uses ImageFormat.Png? And Isn't there any way to solve this problem with existing format? – beratuslu Aug 27 '10 at 19:22
  • @berotomanya: I've added more to the answer after inspecting your example pictures. I think you'll find the answer complete. – kbrimington Aug 28 '10 at 06:05
  • Thank you so so much man!. But I think that I need to look some more for this topic. – beratuslu Aug 28 '10 at 12:33