Question

I am using WPF for an image resizing pipeline which has been working beautifully under .NET v3.5. I just upgraded the project to target v4.0 and now all of my resized images are heavily aliased. None of the image pipeline code has changed.

  1. Has a default WPF setting changed between v3.5 and v4.0?

  2. How do I control the dithering of my resized bitmap images in WPF?

I'm using BitmapImage, DrawingVisual, DrawingContext, RenderTargetBitmap, BitmapEncoder, and BitmapFrame but I'm not seeing any properties related to dithering. GDI+ had a bunch of settings, so I'm guessing that I'm missing something.

Update: it appears that all of the solutions I've seen assume a Window object or XAML environment. This runs inside a Windows Service which has no UI. I need a way to programmatically affect this setting.

I specifically switched from GDI+ to WPF because GDI+ has memory leaks in long running processes like services & web apps.

Was it helpful?

Solution

The only way I've been able to affect the setting of BitmapScalingMode is to inherit from the DrawingVisual class and set it via its protected accessor:

// exposes BitmapScalingMode (also works for other protected properties)
public class MyDrawingVisual : DrawingVisual
{
    public BitmapScalingMode BitmapScalingMode
    {
        get { return this.VisualBitmapScalingMode; }
        set { this.VisualBitmapScalingMode = value; }
    }
}

If anyone else knows of a better way to set this, I would be excited to hear about it.

It seems that this would work:

RenderOptions.SetBitmapScalingMode(myDrawingVisual, BitmapScalingMode.HighQuality);

...but it does not. Apparently being outside of the XAML windowing runtime must mean that it cannot set the appropriate values.

OTHER TIPS

The default BitmapScalingMode was Fant in 3.0 but in 4.0 it is now BiLinear. You can change the default a few different ways. A couple described here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top