[英]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);
}
出现这种情况,说明你的图片分辨率超过了你给定的控件尺寸。 而像素混叠是由插值算法引起的。
默认情况下,图像缩放时使用的插值算法是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.