Created
May 23, 2016 01:49
-
-
Save alexsorokoletov/56a120c5562344d60e1a6b3fa75bda2c to your computer and use it in GitHub Desktop.
How to resize image in UWP C# XAML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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; | |
} |
@alehlima018, is you question still unanswered?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
as used in this case? I need to resize before going to the blob