4imble
4imble

Reputation: 14426

WPF: Selecting a jpg, resizing it and then saving it? Best option?

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.

Upvotes: 1

Views: 6006

Answers (3)

GraemeF
GraemeF

Reputation: 11457

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.

Upvotes: 3

bitbonk
bitbonk

Reputation: 49649

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+.

Upvotes: 2

RonaldV
RonaldV

Reputation: 633

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.

Upvotes: 0

Related Questions