internal static async Task ResizeAndSaveAs(IStorageFile source, IStorageFile destination,
int targetDimension)
{
// open the file...
using(var sourceStream = await source.OpenReadAsync())
{
// step one, get a decoder...
var decoder = await BitmapDecoder.CreateAsync(sourceStream);
// step two, create somewhere to put it...
using(var destinationStream = await destination.OpenAsync(FileAccessMode.ReadWrite))
{
// step three, create an encoder...
var encoder = await BitmapEncoder.CreateForTranscodingAsync(destinationStream, decoder);
// how big is it?
uint width = decoder.PixelWidth;
uint height = decoder.PixelHeight;
decimal ratio = (decimal)width / (decimal)height;
// orientation?
bool portrait = width < height;
// step four, configure it...
if (portrait)
{
encoder.BitmapTransform.ScaledHeight = (uint)targetDimension;
encoder.BitmapTransform.ScaledWidth = (uint)((decimal)targetDimension * ratio);
}
else
{
encoder.BitmapTransform.ScaledWidth = (uint)targetDimension;
encoder.BitmapTransform.ScaledHeight = (uint)((decimal)targetDimension / ratio);
}
// step five, write it...
await encoder.FlushAsync();
}
}
}
}