【发布时间】:2016-12-18 17:11:45
【问题描述】:
我正在尝试通过使用最近邻缩放来缩放 UWP 中的图像。
在 WPF 中,我使用了RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.NearestNeighbor);。我怎样才能在 UWP 中获得相同的结果?
【问题讨论】:
我正在尝试通过使用最近邻缩放来缩放 UWP 中的图像。
在 WPF 中,我使用了RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.NearestNeighbor);。我怎样才能在 UWP 中获得相同的结果?
【问题讨论】:
在 WPF 中,我使用了 RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.NearestNeighbor);。我怎样才能在 UWP 中获得相同的结果?
在 uwp 中,BitmapTransform 可用于缩放图像。要获得与在 WPF 中使用 BitmapScalingMode.NearestNeighbor 相同的效果,您需要使用具有 NearestNeighbor 值的 BitmapInterpolationMode。
您可以参考的示例代码如下:
private async Task<IStorageFile> CreateNewImage(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)
{
using (var resizedStream = await resizedImageFile.OpenAsync(FileAccessMode.ReadWrite))
{
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;
aspectHeight = (uint)(widthRatio * originalPixelHeight);
cropY = (aspectHeight - aspectWidth) / 2;
encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.NearestNeighbor;
encoder.BitmapTransform.ScaledHeight = aspectHeight;
encoder.BitmapTransform.ScaledWidth = aspectWidth;
encoder.BitmapTransform.Bounds = new BitmapBounds()
{
Width = scaledSize,
Height = scaledSize,
X = cropX,
Y = cropY,
};
await encoder.FlushAsync();
}
}
return resizedImageFile;
}
【讨论】: