我设法在UWP应用中调整图片的大小,当我打开图像时,它具有正确的大小,但是问题是图像元数据仍然显示原始图像大小值。
是否需要在某些时候更改图像元数据?
非常感谢您的帮助!
这是我正在使用的代码:
private async Task ResizeFile(StorageFile resizedFile, StorageFile sourceFile, int width, int height)
{
var imageStream = await sourceFile.OpenReadAsync();
var decoder = await BitmapDecoder.CreateAsync(imageStream);
// get original picture size
int originalWidth = (int)decoder.PixelWidth;
int originalHeight = (int)decoder.PixelHeight;
// calculate aspect ratio
var ratioX = (float)width / (float)originalWidth;
var ratioY = (float)height / (float)originalHeight;
int newWidth = width;
int newHeight = height;
var ratio = Math.Min(ratioX, ratioY);
if (ratio != 0)
{
newHeight = (int)(originalHeight * ratio);
newWidth = (int)(originalWidth * ratio);
}
using (var resizedStream = await resizedFile.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateForTranscodingAsync(resizedStream, decoder);
encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Linear;
encoder.BitmapTransform.ScaledHeight = (uint)newHeight;
encoder.BitmapTransform.ScaledWidth = (uint)newWidth;
await encoder.FlushAsync();
}
}