R – Silverlight – Load Image with FileStream

silverlight

I have a Silverlight application that I want to show pictures in. The pictures are loaded from a database into FileStream objects. I need to load these FileStream objects into a System.Windows.Image element so they are shown in my Silverlight application. The problem is, I cannot figure out how to set the Source of an Image to a FileStream. Does anyone know how to do this?

Thank you!

Best Answer

This should work. I actually put something like this in an attached property so I can decorate images in xaml with our image identifier. The attached property also does some caching of images in the local application store.

var image = d as Image;
if(image != null)
{
    var bitMap = new BitmapImage();
    byte[] buffer = new byte[e.Result.Length];
    e.Result.Read(buffer, 0, (int) e.Result.Length);
    var stream = new MemoryStream(buffer);
    bitMap.SetSource(stream);
    image.Source = bitMap;
}

Cheers!

Related Topic