tags:

views:

153

answers:

1

I want to take a snapshot once an image is loaded, and here is a code snippet:

imageBrush.ImageOpened += (sender, e) => { doSomething(); };

One problem with this code snippet is that if the image brush is already loaded, the event will not be fired, so I need a way to verify that the image is already loaded but seems like there is no "IsLoaded" property in silverlight. Could you suggest an easy workaround?

A: 

You can check that an Image control's Width property is non-zero to test its loaded.

It may be therefore that you can also use the BitmapSource PixelWidth property to perform the same test:-

 bool isLoaded = ((BitmapSource)ImageBrush.ImageSource).PixelWidth == 0;

I've not tested this myself but it seems logical.

AnthonyWJones
Thanks for your reply, but I've got another issue and I'm wondering if you can help me with this one as well.A canvas is received coming from the server, and an image brush is extracted from the canvas via searching for a path.In summary, Canvas -> Path -> Path.Fill -> ImageBrush.Now, I want to take a snapshot of the ImageBrush using WriteableBitmap when it is loaded and here is what I did
umlgorithm
var imageBrushes = VisualTreeUtility.FindVisualChildren<Path>(canvas).Where(i => i.Fill.GetType() == typeof(ImageBrush)).Select(p => p.Fill);foreach(var image in imageBrushes){ image.ImageOpened += delegate { //never gets here!! }; if (image.isLoaded()) { // I made an extension based on your code snippet // It gets here when it has non-zero width }}
umlgorithm
@umlgorithm: It would be much much better for you to post another question than to try to squeeze and additional question into comments on answer, as you can see code doesn't format very well in comments. Comments are really for discussion of a specific answer or question or for minor requests for clarification of an answer.
AnthonyWJones