The ImageDecodeSizeBehavior

These are very useful properties that forces the memory surface to store the image to fit a certain size, and avoid the waste of memory induced by large downscaled images. This is a very common performance issue for applications that display variable sized images, where the memory can grow very quickly.

So in C#, the following attached property can be written pretty easily :

The idea here is to use the current image control size as the target surface size, do be gentle with the memory. The downside is that if images need to be zoomed in, then they're going to be pixelated, unless the image is re-created.

In this sample, the hook on SizeChanged is more of a speed test, because one would not want to re-create the image every time the size changes, particularly if it changes too much (though the Rx Throttle operator may help).

Except that it’s a lot more verbose. Particularly the lambda syntax that uses all the available braces on the keyboard.

Performance

How do the two compare, in terms of performance ? Testing this in a real control is a bit complex, because of the performance issue I outlined in this post, but also because the measured time varies a lot, of about +/- 2%. This margin of error is not small enough to show the actual improvements.

Which is a bit of a change from a simple delegate registration. I'm guessing that there is a lot of work related to reference tracking and marshalling, under the hood, which may account for the loss of performance.

It is worth the investment ?

We’re talking about quite small amounts of time, yet we’ve got 16.6ms to perform operations on the UI thread, and keep the 60fps rate to preserve smooth animations. So, 0.7ms may count in the balance.

C++/CX requires a bit more maintenance than its C# counterpart, which may add to the development cost. It also forces the creation of packages for all the available platforms, as it requires the creation of a native WinMD component.

I, for one, tend to favor maintainability over performance, unless it is in a very critical path.

Cloud

About me

My name is Jerome Laban, I am a Software Architect, C# MVP and .NET enthustiast from Montréal, QC. You will find my blog on this site, where I'm adding my thoughts on current events, or the things I'm working on, such as the Remote Control for Windows Phone.