StreetFoo.Client.ImageHelper.ResizeAndSaveAs C# (CSharp) Method

ResizeAndSaveAs() static private method

static private ResizeAndSaveAs ( IStorageFile source, IStorageFile destination, int targetDimension ) : System.Threading.Tasks.Task
source IStorageFile
destination IStorageFile
targetDimension int
return System.Threading.Tasks.Task
        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();
                }
            }
        }
    }
ImageHelper