BaconographyW8.PlatformServices.ImagesService.GenerateResizedImageAsync C# (CSharp) Method

GenerateResizedImageAsync() private static method

private static GenerateResizedImageAsync ( StorageFile inputFile, uint width, uint height, uint edgePadding = 5, uint bottomPadding = 20, NameCollisionOption collisionOption = NameCollisionOption.ReplaceExisting ) : Task
inputFile Windows.Storage.StorageFile
width uint
height uint
edgePadding uint
bottomPadding uint
collisionOption NameCollisionOption
return Task
        private static async Task<StorageFile> GenerateResizedImageAsync(StorageFile inputFile, uint width, uint height, uint edgePadding = 5, uint bottomPadding = 20, NameCollisionOption collisionOption = NameCollisionOption.ReplaceExisting)
        {
            try
            {
                string fileName = inputFile.DisplayName + width + "x" + height;
                string extension = inputFile.Name.Substring(inputFile.Name.LastIndexOf('.'));

                string folder = inputFile.Path.Substring(0, inputFile.Path.LastIndexOf('\\'));
                var outputFolder = await StorageFolder.GetFolderFromPathAsync(folder);
                var newFile = await outputFolder.CreateFileAsync(fileName + extension, CreationCollisionOption.ReplaceExisting);

                var inputStream = await inputFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
                var outputStream = await newFile.OpenTransactedWriteAsync();

                var inMemStream = new InMemoryRandomAccessStream();
                var decoder = await BitmapDecoder.CreateAsync(inputStream);
                var encoder = await BitmapEncoder.CreateForTranscodingAsync(inMemStream, decoder);

                // Find aspect ratio for resize
                float nPercentW = (((float)width - (edgePadding * 2)) / (float)decoder.PixelWidth);
                float nPercentH = (((float)height - (edgePadding * 2)) / (float)decoder.PixelHeight);
                float nPercent = nPercentH < nPercentW ? nPercentH : nPercentW;

                // Scale height and width
                if (nPercent < 1)
                {
                    encoder.BitmapTransform.ScaledHeight = (uint)(decoder.PixelHeight * nPercent);
                    encoder.BitmapTransform.ScaledWidth = (uint)(decoder.PixelWidth * nPercent);
                }

                // Image may still exceed intended bounds, resize as appropriate
                if (encoder.BitmapTransform.ScaledWidth > width || encoder.BitmapTransform.ScaledHeight > height)
                {
                    BitmapBounds bounds = new BitmapBounds();
                    if (encoder.BitmapTransform.ScaledWidth > width)
                    {
                        bounds.Width = width;
                        bounds.X = (encoder.BitmapTransform.ScaledWidth - width) / 2;
                    }
                    else
                        bounds.Width = encoder.BitmapTransform.ScaledWidth;
                    if (encoder.BitmapTransform.ScaledHeight > height)
                    {
                        bounds.Height = height;
                        bounds.Y = (encoder.BitmapTransform.ScaledHeight - height) / 2;
                    }
                    else
                        bounds.Height = encoder.BitmapTransform.ScaledHeight;
                    encoder.BitmapTransform.Bounds = bounds;
                }
                await encoder.FlushAsync();

                var outDecoder = await BitmapDecoder.CreateAsync(inMemStream);
                var outEncoder = await BitmapEncoder.CreateForTranscodingAsync(outputStream.Stream, outDecoder);

                var transparentBytes = GenerateTransparentBitmap(width, height);

                PixelDataProvider data = await outDecoder.GetPixelDataAsync();
                uint heightOffset = (height - outDecoder.PixelHeight) / 2 - bottomPadding;
                uint widthOffset = (width - outDecoder.PixelWidth) / 2;
                byte[] bytes = MergePixelArrays(transparentBytes, width, height, data.DetachPixelData(), outDecoder.PixelWidth, outDecoder.PixelHeight, widthOffset, heightOffset);
                outEncoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, width, height, 72.0, 72.0, bytes);

                await outEncoder.FlushAsync();
                return newFile;
            }
            catch (Exception)
            {

            }

            return null;
        }