Good Day All

I have a Generic function that in my Static Class. i have this Class in Silverlight

public static BitmapSource LoadImage(Byte[] imageBytes)
            BitmapImage bmpImage = new BitmapImage();
            MemoryStream mystream = new MemoryStream(imageBytes);
            return bmpImage;

Now this looked fine for me until i had to bind an Image in Silverlight like this

PhotoHolder.Source =GenericMethods.LoadImage(model.imbPhoto);

The Images i have in the Database are Supported by silverlight they are jpg type.

and the Elelment that i am binding to look like this in Silverlight

<Image x:Name="PhotoHolder" Height="98" Width="102" OpacityMask="Black"  Canvas.Left="141" Canvas.Top="30">

so if i bind i get this Error

Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))


I had a similar problems when using Image.FromStream with a MemoryStream object.
The local MemoryStream object goes out of scope when you exit the function.
As the image does not have a strong reference to the stream it is subject to Garbage Collection.
This means the MemoryStream is closed and disposed of resulting in an error when you reference the image.
I solved this by keeping a seperate reference to the MemoryStream.
Not sure if there is another solution.

i found the reason to this. I had a stored procedure that was retrieving it from the DB and casting it to a varbinary and i realized that if you cast an image to another type it will never work together as an image again. after i removed that , my function that convert the byte[] to Imagesource started to work as it should.