How to specify image scaling algorithm used by WPF image?

Is there a way to specify how the image is scaled in the Image element with a LayoutTransform set to ScaleTransform with integer values ​​for ScaleX and ScaleY ?

I want the image of the scaled image to be clear (that is, by scaling the “nearest neighbor”), without blurring. (Imagine how you would like a bitmap editing program to behave when zoomed).

I noticed the protected VisualBitmapScalingMode property on Image , so I created a subclass of Image that sets this property as BitmapScalingMode.NearestNeighbor . However, this had no effect.

+4
source share
2 answers

I fixed this by overriding OnRender in my Image subclass and setting 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); } } 
+8
source

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

+7
source

Source: https://habr.com/ru/post/1310965/


All Articles