RenderOptions.SetBitmapScalingMode
I’ll be speaking at MIX UK in a few weeks (awesome!) and so I’m spending a little time this reworking the ComicViewer app that we showed at MIX in Vegas. In doing so I reencountered RenderOptions.SetBitmapScalingMode. I’ve been meaning to blog about this guy since the first time I worked on the ComicViewer. It’s super handy thing to know about when dealing with scaled images.
When WPF scales an image, it does so with a pretty high quality algorithm. This, of course, is what you want in 97% of the scenarios where you’re scaling an image because the result looks great. There are times, though (the other 3%) when you don’t want to pay for the high quality scaling. This is particularly true when you’re animating the image in some way. In that case, the image may be changing so rapidly that the high quality scaling doesn’t pay off. More importantly, the cost of the algorithm might actually make the animation appear jerky or sluggish.
Fortunately, WPF let’s you opt into a faster (albeit worse looking) algorithm for just those occasions. To control the quality of the scaling, you can call the method RenderOptions.SetBitmapScalingMode and pass in the element that will be animated and a quality setting. Your options for controlling the quality are HighQuality and LowQuality. The LowQuality option is considerably faster than the HighQuality and looks much worse–perfect for an animation.
This is exactly what I do when I scale or move pages (which are really just big images) in the ComicViewer. Immediately before animating I set BitmapScalingMode to LowQuality and immediately after animating, I set it back to HighQuality. I believe that changing this value invalidates render so you get an immediate update when you switch back to HighQuality. The result is much better perf without degrading quality in any noticeable way.
October 9th, 2007 at 6:03 am
It’s a shame there’s no RenderOptions.SetVideoScalingMode() that we could use for the MediaElement/Avalon EVR! Stretching video taxes the CPU pretty badly.