首页 > 解决方案 > UWP - 调整大小后元数据中的图像大小未更改

问题描述

我设法在 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();
        }
}

标签: uwpimage-resizing

解决方案


推荐阅读