Exemplo n.º 1
0
        public static async Task ResizeImageUniformAsync(StorageFile sourceFile, StorageFile targetFile, int maxWidth = Int32.MaxValue, int maxHeight = Int32.MaxValue)
        {
            using (var stream = await sourceFile.OpenReadAsync())
            {
                var decoder = await BitmapDecoder.CreateAsync(stream);

                if (IsGifImage(decoder))
                {
                    await sourceFile.CopyAndReplaceAsync(targetFile);
                    return;
                }

                maxWidth = Math.Min(maxWidth, (int)decoder.OrientedPixelWidth);
                maxHeight = Math.Min(maxHeight, (int)decoder.OrientedPixelHeight);
                var imageSize = new Size(decoder.OrientedPixelWidth, decoder.OrientedPixelHeight);
                var finalSize = imageSize.ToUniform(new Size(maxWidth, maxHeight));

                if (finalSize.Width == decoder.OrientedPixelWidth && finalSize.Height == decoder.OrientedPixelHeight)
                {
                    await sourceFile.CopyAndReplaceAsync(targetFile);
                    return;
                }

                await ResizeImageAsync(decoder, targetFile, finalSize);
            }
        }
Exemplo n.º 2
0
 private Size GetStretchSize(Size containerSize, Size virtualSize)
 {
     switch (this.Stretch)
     {
         case Stretch.Fill:
             return new Size(this.ActualWidth, this.ActualHeight);
         case Stretch.Uniform:
             return virtualSize.ToUniform(containerSize);
         case Stretch.UniformToFill:
             return virtualSize.ToUniformToFill(containerSize);
         default:
             return virtualSize;
     }
 }