How to specify the image scaling algorithm used by a WPF Image?

雨燕双飞 提交于 2019-12-10 15:26:49

问题


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).

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.


回答1:


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);
    }
  }



回答2:


You can set the RenderOptions.BitmapScalingMode property in the XAML for the Image control. There's no need to inherit the Image class.



来源:https://stackoverflow.com/questions/2913060/how-to-specify-the-image-scaling-algorithm-used-by-a-wpf-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!