tags:

views:

40

answers:

1

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;
    }
+2  A: 

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. http://stackoverflow.com/questions/1072897/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.

kbrimington
same problem continue
berotomanya
@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
1. Original Image: http://img841.imageshack.us/img841/147/pron1.jpg2. Photoshop resize: http://img835.imageshack.us/img835/9956/photoshopc.jpg3. Programmatic resize: http://img198.imageshack.us/img198/5216/270as.jpgAs 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).
berotomanya
What you think?
berotomanya
@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
how do I change my code to uses ImageFormat.Png? And Isn't there any way to solve this problem with existing format?
berotomanya
@berotomanya: I've added more to the answer after inspecting your example pictures. I think you'll find the answer complete.
kbrimington
Thank you so so much man!. But I think that I need to look some more for this topic.
berotomanya