Image Manipulation using WPF Imaging Classes

The following question recently appeared in the WPF forum:


Is there posibility of resizing BitmapImage using WPF?

I do not want to resize an Image control. I want to resize image itself so when i convert it to bytes and stores it in database it has desired size.


In my response to that question, I included some sample code demonstrating how you might resize an image by leveraging the BitmapImage class to decode the image to the desired pixel width and height and then save the converted image back to a disk file or store it in a database.

I also mentioned that there are some additional imaging classes that can be leveraged for other common image manipulation tasks.  After receiving a couple of inquiries about these other classes, I decided to expand my sample a little. 

Here is a sample application that demonstrates how to use the CroppedImage and TransformedImage classes.  It also includes the resize code from the earlier sample.

Note that I’ve written this sample as a simple .NET console application.  (This should reinforce the idea that WPF is more than just a “presentation” framework.)  You can pass the app commands like rotate left, rotate right, flip horizontally, flip vertically, and crop square. 

Hope others find this useful.

Cheers,
Dr. WPF

4 Responses to “Image Manipulation using WPF Imaging Classes”

  1. Angus says:

    Won’t work in WPF/Web, at least if you modify the DecodePixelH/W because of partial trust not liking bitmap effects. Any idea?

  2. Dr. WPF says:

    Hi Angus, This sample performs various actions that are not allowed in partial trust (although bitmap effects do not come into play here). The file access, alone, is enough to preclude it from running as an xbap. I’m afraid its just not designed for that scenario.

  3. Nathan Phillips says:

    Is this sample still available anywhere?

  4. Dr. WPF says:

    Sorry about that Nathan! The link was broken due to my blog migration a couple of months ago. I’ve fixed it now, so you should be able to download the sample.

    Cheers,
    -dw