tags:

views:

209

answers:

3

I was wondering what is the best way to go about taking a selected image, resizing it and saving it. From what i have heard WPF destroys image quality when it resizes images. Is this true, if so what method should i try to attempt this task?

Thanks, Kohan.

A: 

If you just want to resize and then save an image, I don't see what this has to do with WPF. You could just use image manipulation functions in .NET. See this link for an example.

RonaldV
I figured it was a WPF issue as this guy in a blog was blaming WPF specifically for ruining his images : http://thingsihateaboutmicrosoft.blogspot.com/2009/06/wpf-destroys-images-when-resizing-them.html
Kohan
I'll give TransformedBitmap a go and see if it works. thanks
Kohan
You're welcome. He's right when you want to do it live in WPF. I think this is fixed in WPF for .NET 4.0. Not sure though.
RonaldV
+1  A: 

WPF is designed to be able to resize images in real time for display (using graphics card acceleration) and so is biased towards speed over quality.

If you want decent quality you should use System.Drawing.Bitmap to do your resizing:

System.Drawing.Bitmap resizedImage;

using(System.Drawing.Image originalImage = System.Drawing.Image.FromFile(filePath))
    resizedImage = new System.Drawing.Bitmap(originalImage, newSize);

// Save resized picture
resizedImage.Save(resizedFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
resizedImage.Dispose();

The quality still won't be great - JPEG is a lossy image format and fidelity will be lost by loading and saving and also by the Bitmap classes crude scaling. If quality must be as high as possible then you should consider using an imaging library such as DevIL, which will do a better job of producing a smoothly resized image.

GraemeF
Thanks, i dont need the quality to be crystal perfect so your code was a perfect base for me. Up and running now. Many thanks.
Kohan
This code is a bit simplistic, and will likely render jpegs that are too big and of too poor quality. Have a look at http://stackoverflow.com/questions/1745446/image-resizing-performance-system-drawing-vs-system-windows-media for a better approach.
Oskar Austegard
+1  A: 

You don't have to revert back to GDI+ to scale bitmaps. WPF itself has all the APIs you need. The quality is the same: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/580fca03-d15b-48b2-9fb3-2e6fec8a2c68/

Image manipulation is very powerful in WPF plus it is faster than GDI+.

bitbonk