繁体   English   中英

使用 Bitmaptransform 缩放图像会在 win2d 中提供模糊图像

[英]Scaling image using Bitmaptransform gives blured images in win2d

这是我尝试过的:

var decoder = await BitmapDecoder.CreateAsync(fileStream);
BitmapTransform bitmapTransform = new BitmapTransform();
bitmapTransform.ScaledHeight = 300;
bitmapTransform.ScaledWidth = 800;

var pixelProvider = await decoder.GetPixelDataAsync(
                    BitmapPixelFormat.Bgra8,
                    BitmapAlphaMode.Ignore,
                    bitmapTransform,
                    ExifOrientationMode.IgnoreExifOrientation,
                    ColorManagementMode.DoNotColorManage);

byte[] by = pixelProvider.DetachPixelData();
CanvasBitmap cb = CanvasBitmap.CreateFromBytes(sender,by,800,300,Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8X8UIntNormalized);

我在 CanvasControl 的 Draw 事件中做了什么:

private  void Canvas_Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
args.DrawingSession.DrawImage(cb);
}

由于缩放,图像变得模糊。 我该如何解决这个问题? 我需要在运行时动态更改图像的宽度和高度,我该怎么做? 我的原图: n

绘制后(模糊图像): 在此处输入图像描述

出现这种情况,说明你的图片分辨率超过了你给定的控件尺寸。 而像素混叠是由插值算法引起的。

默认情况下,图像缩放时使用的插值算法是Linear ,如果要在较小的控件上渲染分辨率较大的图像,可以使用Fant算法。

bitmapTransform.ScaledHeight = 300;
bitmapTransform.ScaledWidth = 800;
bitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant;

如果打算动态改变图片的宽高,建议使用ViewBox作为CanvasControl容器。

<Viewbox Stretch="Uniform" StretchDirection="Both" x:Name="MyViewbox">
    <xaml:CanvasControl />
</Viewbox>

然后您可以在运行时更改 Viewbox 的大小。

MyViewbox.Width = 400;
MyViewbox.Height = 150;

谢谢。

您可以使用 Win2d 效果快速完成工作。

 var ScaledImage = new Microsoft.Graphics.Canvas.Effects.ScaleEffect(
Source = source, new Vector2(2f, 2f);)

上面的 function 产生一个ScaleEffect ,它实现了ICanvasBitmap所以你可以直接用DrawingSession.DrawImage渲染它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM