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.
You can set the RenderOptions.BitmapScalingMode property in the XAML for the Image control. There's no need to inherit the Image class.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With