问题描述
有没有办法指定如何在 Image
元素中放大图像,其中 LayoutTransform
设置为具有整数值的 ScaleTransform
ScaleX
和 ScaleY
?
Is there a way to specify how an image is scaled up in an Image
element with LayoutTransform
set to a ScaleTransform
with integer values for ScaleX
and ScaleY
?
我想清晰地显示缩放后的图像(即使用最近邻"缩放),没有模糊.(想象一下您希望位图编辑程序在放大时的行为方式).
I want to display the scaled image crisply (ie using 'nearest neighbour' scaling), with no blurring. (Imagine how you would want a bitmap editing program to behave when zooming in).
我注意到 Image
上的受保护属性 VisualBitmapScalingMode
,因此创建了一个 Image
的子类,将该属性设置为 BitmapScalingMode.NearestNeighbor代码>.然而,这没有效果.
I noticed the protected property VisualBitmapScalingMode
on Image
, so created a subclass of Image
that sets this property to BitmapScalingMode.NearestNeighbor
. However, this had no effect.
推荐答案
我通过在我的 Image 子类中覆盖 OnRender 并在绘制图像之前设置 VisualBitmapScalingMode 解决了这个问题:
I fixed this by overriding OnRender in my Image subclass, and setting the VisualBitmapScalingMode before drawing the image:
class MyImage : System.Windows.Controls.Image
{
protected override void OnRender(DrawingContext dc)
{
this.VisualBitmapScalingMode = System.Windows.Media.BitmapScalingMode.NearestNeighbor;
base.OnRender(dc);
}
}
这篇关于如何指定 WPF Image 使用的图像缩放算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!