Problems to retrieve Image from SQL Server in Silverlight

90 views Asked by At

I'm kind of new to Silverlight. I've managed to solve this problem for WPF, unfortunately not for Silverlight.

I open an image with an OpenFileDialog. I make a stream out of it and write it in a byte array. This byte array is then put into the database. This is the code I use to open the image and convert it into a byte array:

OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = @"C:\";
dlg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif";
dlg.FilterIndex = 1;

if(dlg.ShowDialog() == true)
{
            pImage.Naam = dlg.File.Name;
            BitmapImage bitImage = new BitmapImage();

            Stream s = (Stream)dlg.File.OpenRead();
            imgItem.Source = bitImage;

            pImage.Data = new byte[s.Length];
            s.Read(pImage.Data, 0, (int)s.Length);
            s.Close();
}

I manage to save the byte array as a varbinary without any problem to my database.

When I retrieve it, I have no difficulties converting the varbinary back to the byte array (I'm pretty sure the two byte arrays are the same as when debugging I get exactly the same size).

But then I get stuck! Whenever I want to convert the byte array back to an image using the following code, my stream (and thus my image) is empty:

var newImage = new BitmapImage();
var myStream = new MemoryStream(pHuidigeImage.Data);
newImage.SetSource(myStream);
imgItem.Source = newImage;

So, what's wrong here?

Thanks in advance!

0

There are 0 answers