Skip Navigation LinksHome > View Post

Improving Performance of images used with 3D

I've been working on some 3D using WPF and was making heavy use of some largish (around 1 megapixel) images with ImageBrushes applied to DiffuseMaterials. Naturally, being WPF there was some animation thrown in for good measure to make sure I'm working that processor like a bad boy.

All was going well, except for one annoying niggle that I couldn't fix. Whenever a new image was displayed on screen there was a notable stutter in the animation whilst *something* happened. For starters, I assumed this was the I/O time to read the image from disk but I tried pre-loading the images in all kinds of ways without any succcess.

Failing to get a result, I turned to some graphics heroes in the WPF team who immediately suspected it was the Fant scaler resizing the image on first load (I too thought he'd misspelt Font for a while). The quick fix, it transpires is to swap your ImageBrush for a VisualBrush.

Whoa! But VisualBrushes are slower than ImageBrushes. I know, but it works. The key to the trick is removing the Fant scaler and going for the faster, Linear scaler. So we went from:

<DiffuseMaterial.Brush>
    <ImageBrush ImageSource="{Binding Source}" />
</DiffuseMaterial.Brush>

... to ...

<DiffuseMaterial.Brush>
    <VisualBrush RenderOptions.CachingHint="Cache">
        <VisualBrush.Visual>
            <Image Source="{Binding Source}" RenderOptions.BitmapScalingMode="Linear" />
        </VisualBrush.Visual>
    </VisualBrush>
</DiffuseMaterial.Brush>

And now it's lightning fast and silky smooth. There's a little loss in quality of the scaled bitmap but you'd have to be looking closely to spot it. And you'll be too busy admiring the smooth animation to do so.

Tags: 3D WPF

 
Josh Post By Josh Twist
12:56 AM
21 Apr 2009

» Next Post: Dogfooding Ukadc.Diagnostics
« Previous Post: WPF Quick Tip: Converters as MarkupExtensions

Comments are closed for this post.

Posted by Matt @ 22 Apr 2009 5:39 AM
Nice one cheers. Like you say, theres no chance of writing WPF without any animation, so these things are nice to know. One of the places I'm using animation and an imagebrush is making the images have rounded corners by making the image the background of a border. So this might speed up the raising of the image on hover.

Posted by K.T @ 20 Apr 2010 2:09 AM
Great!

© 2005 - 2014 Josh Twist - All Rights Reserved.