views:

198

answers:

1

Im trying to create a PNG sequencer class that will allow me to change an ImageBrush's ImageSource property via an animation.

The issue is that I have around 150 PNG files to load, and it really really affects performance when I have a few animations on the screen.

I have read a little about RenderTargetBitmap and also WriteableBitmap but Im not sure how to get a big performance boost, because I really do need it.

Im getting down to 6fps in some cases, which is obviously not acceptable.

In my Sequencer class, I just update a CurrentFrame DP that changes the ImageSource property of the ImageBrush.

Any ideas on how to increase the performance here?

+1  A: 

Step 1 is loading all your images in ahead of time (preferably on a background thread). You should have your BitmapImage objects initialized with the CacheOption = BitmapCacheOption.OnLoad. You may already be doing this or it might not be the problem (the images cache by default).

However the rendering thread also needs to do some work when you change the image source. If you're not displaying at the source image size, that might be a problem, as by default the Image control uses the high quality Fant scaling algorithm. In that case you could get a performance increase by calling RenderOptions.SetBitmapScalingMode(uiImage, BitmapScalingMode.LowQuality); on your Image. Low quality scaling is orders of magnitude faster. However even after that there's still a bit of work involved. If you want to get the fastest animation possible, you can create an Image control for every frame, then overlap them all on the same place and change which one appears on top. You'll still take the hit on the render thread loading all the images in, but the actual animation should be quite snappy.

RandomEngy
Wow, at this point in time, it looks like the Scaling Mode option has worked nicely! Thanks a lot. One more thing, when I run the WPF performance profiler, I can see my video card memory jumping by a few hundred MB when I first load up my images, is that normal? Also, it doesnt ever drop back down, should it?
Mark
I would expect that behavior if you're loading each frame up in a separate Image and changing the z-order, since all the images have been rendered to the video card ahead of time (that's why it's so fast).If you're still just changing out the BitmapSource, I don't know what it should be doing in that situation.Is the memory jump correlated with the total number of frames in all of your animations?
RandomEngy
Its hard to tell, but its definitely related, as nothing happens with the video memory until I run my animation. I should also mention that I am using my animations as a Material on the 3D geometry, so perhaps its loads them into memory because its all hardware rendered?
Mark
Yeah, the images are certainly getting loaded into video memory in that case. It may seem higher than it should because it's all being stored uncompressed. If the pictures are a decently high resolution and all the frames are being loaded in, it could add up.
RandomEngy
I suppose thats true, do you have any tips to reduce/clear the video memory every now and then?
Mark
I don't know that much about how video memory works; I think WPF handles it all under the covers. It's hard for me to say without specifics or the ability to investigate. I would just tinker around with image dimensions, number of frames loaded and number of animations and see how it all adds up.
RandomEngy