简体   繁体   中英

Image file from Silverlight to WCF

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 ?

Your CopyStream method makes sure to keep reading from the input stream until it doesn't get anything more. Your StreamToByteArray method doesn't. Are you sure you're converting the entire stream on the client, rather than just the first x bytes followed by zeroes?

private byte[] StreamToByteArray(Stream stream)
{
    byte[] array = new byte[stream.Length];
    int index = 0, length = 0;
    while ((length = stream.Read(array, index, array.Length - index)) > 0)
    {
        index += length;
    }

    return array;
}

I find the answer, and it did have nothing to do with WCF !

The problem is that I convert my OpenFileDialog result on confirm button on in my ViewModel. I don't know why but if I do the conversion in the method which called the openfiledialog, the byte array isn't corrupted and all work fine.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM