Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image Resize Aliasing in WPF v4 but not under v3.5

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.

like image 418
mckamey Avatar asked Oct 15 '22 05:10

mckamey


1 Answers

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.

like image 134
mckamey Avatar answered Oct 18 '22 05:10

mckamey