UWP: resize image, convert to jpeg, change jpeg quality

UWP: resize image, convert to jpeg, change jpeg quality

Hi everyone!
Working with images in UWP is very similar to what it was in WinRT API, however, there are some gotchas we would like to share.

Creating a thumbnail from StorageFile

First, here is our version of creating a thumbnail for image (if you want to pick an image and then show a smaller preview somewhere in the list or anywhere).
There is nothing complex here, pretty straightforward.
Note that in this case (probably because we’re creating encoder based on decoder) we don’t need to care about source image orientation. Great.
Code is available in the gist below.

Converting StorageFile to JPEG with specified quality

Now, common task if you’re doing a image upload is to convert image to JPEG of specific quality.
We found out that there is one thing you want to take care of.
Devices like Lumia 950 can take a picture and then specify image rotation (orientation) in metadata, BitmapDecoder class doesn’t actually respect this in a way that you’d expect.
So, when you get that oriented image size from decoder.PixelWidth and decoder.PixelHeight and then get the byte[] pixel data, the pixel data array is actually not using PixelWidth/PixelHeight as dimensions but rather uses decoder.OrientedPixelWidth and decoder.OrientedPixelHeight values.
When you pass pixel data to BitmapEncoder to encode in a different format you want to specify Oriented* values to get correct image.

This is how it will look like:

Correct and garbled images when you do the conversion of oriented image

Correct code that takes care of this for you is available below.

Code snippet – creating thumbnail from StorageFile UWP C# Xaml

Code snippet – Converting StorageFile to JPEG with specified quality

Happy Universal Windows Apps coding!