I'm trying to upload a photo from a Silverlight client to a server, using a WCF service.
The method called by the client is void UpdatePicture(Stream image); This method on the client side, appears as UpdatePicture(byte[] array), so I created a converter (the input stream is a FileStream from OpenFileDialog.File.OpenRead())
private byte[] StreamToByteArray(Stream stream)
{
byte[] array = new byte[stream.Length];
stream.Read(array, 0, array.Length);
return array;
}
The converter seems to work well.
On the WCF side, I have to save the stream to a file. I use this :
public void UpdatePicture(Stream image)
{
if (SelectedUser == null)
return;
if (File.Exists(image_path + SelectedUser.sAMAccountName + ".jpg"))
{
File.Delete(image_path + SelectedUser.sAMAccountName + ".jpg");
}
using (FileStream file = File.Create(image_path + SelectedUser.sAMAccountName + ".jpg"))
{
DataManagement.CopyStream(image, file);
}
}
to copy the Stream to the FileStream, I use this :
public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, len);
}
}
The file is created as expected, the size is ok, but the image is not displayed by the PhotoViewer ou any other program.
Does somebody knows why ? Any help would be very appreciated :)
EDIT :
Something really strange :
I created a WCF method GetWCFBytes(byte[] array) which returns the parameter without doing anything. If use StreamToByteArray to pass a stream to this method as a byte array, and set the result to a Image via a BitmapImage with a MemoryStream, it displays a blank image.
If I take the OpenFileDialog's stream, convert it to a byte array, create a new MemoryStream from this array, and set my BitmapImage with it : the image is ok.
Does WCF use some magic against streams and byte arrays ?