Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@alexsorokoletov
Created May 23, 2016 01:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexsorokoletov/56a120c5562344d60e1a6b3fa75bda2c to your computer and use it in GitHub Desktop.
Save alexsorokoletov/56a120c5562344d60e1a6b3fa75bda2c to your computer and use it in GitHub Desktop.
How to resize image in UWP C# XAML
/// <summary>
/// Resizes and crops source file image so that resized image width/height are not larger than <param name="requestedMinSide"></param>
/// </summary>
/// <param name="sourceFile">Source StorageFile</param>
/// <param name="requestedMinSide">Width/Height of the output image</param>
/// <param name="resizedImageFile">Target StorageFile</param>
/// <returns></returns>
private async Task<IStorageFile> CreateThumbnaiImage(StorageFile sourceFile, int requestedMinSide, StorageFile resizedImageFile)
{
var imageStream = await sourceFile.OpenReadAsync();
var decoder = await BitmapDecoder.CreateAsync(imageStream);
var originalPixelWidth = decoder.PixelWidth;
var originalPixelHeight = decoder.PixelHeight;
using (imageStream)
{
//do resize only if needed
if (originalPixelHeight > requestedMinSide && originalPixelWidth > requestedMinSide)
{
using (var resizedStream = await resizedImageFile.OpenAsync(FileAccessMode.ReadWrite))
{
//create encoder based on decoder of the source file
var encoder = await BitmapEncoder.CreateForTranscodingAsync(resizedStream, decoder);
double widthRatio = (double)requestedMinSide / originalPixelWidth;
double heightRatio = (double)requestedMinSide / originalPixelHeight;
uint aspectHeight = (uint)requestedMinSide;
uint aspectWidth = (uint)requestedMinSide;
uint cropX = 0, cropY = 0;
var scaledSize = (uint)requestedMinSide;
if (originalPixelWidth > originalPixelHeight)
{
aspectWidth = (uint)(heightRatio * originalPixelWidth);
cropX = (aspectWidth - aspectHeight) / 2;
}
else
{
aspectHeight = (uint)(widthRatio * originalPixelHeight);
cropY = (aspectHeight - aspectWidth) / 2;
}
//you can adjust interpolation and other options here, so far linear is fine for thumbnails
encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Linear;
encoder.BitmapTransform.ScaledHeight = aspectHeight;
encoder.BitmapTransform.ScaledWidth = aspectWidth;
encoder.BitmapTransform.Bounds = new BitmapBounds()
{
Width = scaledSize,
Height = scaledSize,
X = cropX,
Y = cropY,
};
await encoder.FlushAsync();
}
}
else
{
//otherwise just use source file as thumbnail
await sourceFile.CopyAndReplaceAsync(resizedImageFile);
}
}
return resizedImageFile;
}
@alexandresanlim
Copy link

as used in this case? I need to resize before going to the blob

var file = await ImageChooser.GetSelectedImageAsStorageFile();

                var sasUri = await Vm.GetBlobSasUri(file.DisplayName);

                var blob = await file.UploadToBlob(sasUri);

                await Vm.ImageSelected(blob.Uri);

@alexsorokoletov
Copy link
Author

@alehlima018, is you question still unanswered?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment