Dynamic Custom BitmapSource

Jan 12, 2011 at 10:57 PM

Hi Dwayne

After Reading your very informative blog on "Implementing A Custom BitmapSource" I was wondering if there is a way to create a custom BitmapSource that contains bitmap data that can be dymanically modified?

More specifically, after setting a custom BitmapSource as the Source property on an Image control, the CopyPixels method is only called once when the BitmapSource is first attached. Even if I call Image.InvalidateVisual() or detach and re-attach the bitmap to the Image.Source property, CopyPixels is not called to refresh the image data, so I assume the pixel data is being cached somewhere within the Image control.

With the WriteableBitmap and InteropBitmap classes, the Image control is refreshed after the bitmap data is modified (on the call to WriteableBitmap.Unlock or InteropBitmap.Invalidate). Is there any way to replicate this behaviour in a custom BitmapSource derived class, to prompt the Image control to re-read the data from the bitmap?

Thanks, Andy

Coordinator
Jan 13, 2011 at 5:07 AM

I don’t know how to accomplish this, or even if it is possible. It is clear from the code that this is not intended to be supported.

Jan 13, 2011 at 9:52 AM

Thanks Dwayne

Do you know if there's any information on the mechanism(s) that WriteableBitmap and InteropBitmap use to achieve dynamic updating available anywhere? I'm guessing they make use of some feature(s) in the underlying WIC infrastructure but my research on the web has been fruitless so far.

Thanks again,
Andy

Coordinator
Jan 18, 2011 at 11:15 PM

We use something similar to, but different, than WIC for our internal bitmap handling. We also plumb commands to our rendering engine to propagate dirty rects, which is the thing WIC doesn’t really expose. WPF has to manage video textures and copy the pixels from the bitmap down, and obviously update the video texture as the bitmap changes. We expose this via a couple of classes, such as WriteableBitmap, InteropBitmap, and D3DImage.

From: andybird [email removed]
Sent: Thursday, January 13, 2011 2:53 AM
To: Dwayne Need
Subject: Re: Dynamic Custom BitmapSource [MicrosoftDwayneNeed:241525]

From: andybird

Thanks Dwayne

Do you know if there's any information on the mechanism(s) that WriteableBitmap and InteropBitmap use to achieve dynamic updating available anywhere? I'm guessing they make use of some feature(s) in the underlying WIC infrastructure but my research on the web has been fruitless so far.

Thanks again,
Andy

Sep 27, 2012 at 2:26 PM

Any ideas on this yet? I'm in need of something similar. Think of your ColorKeyBitmap, where i want to dynamically change which color should be used.

Kind regards,

Stijn

Coordinator
Oct 1, 2012 at 5:05 PM

The last time I looked into this deeply, the bitmap source pixels would be cached. Unfortunately, the mechanisms for updating the cache were not exposed.

Your best bet is probably to look at shader effects, which will be more performant as well.

http://www.codeproject.com/Articles/71617/Getting-Started-with-Shader-Effects-in-WPF

Dwayne Need | System Center

From: MythJuha [email removed]
Sent: Thursday, September 27, 2012 7:26 AM
To: Dwayne Need
Subject: Re: Dynamic Custom BitmapSource [MicrosoftDwayneNeed:241525]

From: MythJuha

Any ideas on this yet? I'm in need of something similar. Think of your ColorKeyBitmap, where i want to dynamically change which color should be used.

Kind regards,

Stijn

Apr 12, 2015 at 8:45 PM
I also have not found a way to make CustomBitmap notify about changes.
However, there is the possibility to create a new copy of your Bitmap (reusing your cache, if you have any).
It is then redrawn in the next rendering cycle.