views:

101

answers:

3

Hi, I have resize images exceeding a max size. Methods I tried so far are not good enough :-(

  1. System.Drawing.Image.GetThumbnailImage generates very poor quality images in general.
  2. Playing with options like this one I can generate better images in quality but heavier than the original one.

Probably the second option (or something similar) is the best option and I would need to resize using the proper options.

Any advice?

EDIT
My option 2 was generating heavier images for some specific pictures. In general is working as expected so I'd say this is solved.

+2  A: 

Create a new Bitmap object, then using the Graphics object, re-draw the old image into the new image's buffer at the increased/decreased size based on the resize engine you want.

// inImage is your original
Bitmap outImage = new Bitmap(newWid, newHei);

Graphics gfx = Graphics.FromImage(outImage);
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;

gfx.DrawImage(inImage,
   new Rectangle(0, 0, newWid, new Hei),
   new Rectangle(0, 0, inImage.Width, inImage.Height),
   GraphicsUnit.Pixel);
Aren
Thanks Aren. This is very similar to the code posted by OrbMan and my issue with this is that generates heavier images than the original
Claudio Redi
That's because the default format is probably BMP format. You can "save" it to a memory stream with in ImageFormatter and use png/jpg to compress the image data.
Aren
+3  A: 

Try something like this:

public Bitmap Resize(Bitmap originalImage, int newWidth)
{
    int newHeight = (int)Math.Round(originalImage.Height * (decimal)newWidth / originalImage.Width, 0);
    var destination = new Bitmap(newWidth, newHeight);
    using (Graphics g = Graphics.FromImage(destination))
    {
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        g.DrawImage(originalImage, 0, 0, newWidth, newHeight);
    }
    return destination;
}
RedFilter
Thanks OrbMan. Just tested it. Unfortunately I'm having the same issue as my option 2. Reduced images are heavier than original:-(
Claudio Redi
What format are they being retrieved as/saved in? If you're saving the thumbnail as a bitmap I wouldn't be surprised if they were large. Make sure you're using jpeg or png or some other compressed format.
Sean Edwards
@Sean: Same format I retrieve same format I save. I'm processing image of any kind.
Claudio Redi
@OrbMan: It was generating heavier images for some specific pictures. In general is working as expected.
Claudio Redi
@Claudio, I would suggest using the WPF libs, as mentioned in my answer. It's the future :)
Mikael Svenson
@Claudio: show me your code where you are saving the image
RedFilter
@OrbMan: forget about it :-), as I said, I'm having issues for some very specific images. Works perfect for the most of them. @Mikael: I will take into account your advice too. Thanks man!
Claudio Redi
+3  A: 

I would go with using the WPF libs instead of the GDI+ libs. The WPF libs perform faster and I think they yield better results compared to the GDI+ libs.

Check out these excellent posts from Bertrand Le Roy.

Mikael Svenson