How to Convert Byte Array to BitmapSource in C# for Silverlight [Resolved]

Posted by Vuyiswamb under Silverlight on 5/26/2011 | Points: 10 | Views : 6591 | Status : [Member] [MVP] [Administrator] | Replies : 2
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))


Thank you for posting at Dotnetfunda


Posted by: Vuyiswamb on: 5/30/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25


The Problem here is that i had a Image field from the Database and i casted that image to a varbinary and that image was not usable anymore in Silverlight.

Thank you for posting at Dotnetfunda

Vuyiswamb, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thackeray on: 2/27/2014 [Member] Starter | Points: 25

I find some free sample code on a third party on how to convert image to byte array:

Private Sub button1_Click(sender As Object, e As EventArgs)
Dim fileName As String = "c:/Sample.png"

Dim reImage As REImage = REFile.OpenImageFile(fileName)
Dim bytes As Byte() = REFile.GetByteArrayFromImage(reImage, New PNGEncoder())

Return bytes

Vuyiswamb, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response