UWP中的最近邻居渲染

时间:2016-12-18 17:11:45

标签: c# graphics uwp

我正在尝试使用最近邻居缩放来缩放UWP中的图像。 在WPF中,我使用了RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.NearestNeighbor);。如何在UWP中获得相同的结果?

EXAMPLE

1 个答案:

答案 0 :(得分:1)

  

在WPF中,我使用了RenderOptions.SetBitmapScalingMode(image,BitmapScalingMode.NearestNeighbor);.如何在UWP中获得相同的结果?

在uwp中,BitmapTransform可用于缩放图像。要在WPF中使用BitmapScalingMode.NearestNeighbor获得相同的效果,您需要使用NearestNeighborprivate 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; } 的{​​{3}}。

您可以参考的示例代码如下:

var dateValue = moment().utc().format('YYYY-MM-DDTHH:mm:ss') + 'Z';