简体   繁体   English

从Silverlight到WCF的图像文件

[英]Image file from Silverlight to WCF

I'm trying to upload a photo from a Silverlight client to a server, using a WCF service. 我正在尝试使用WCF服务将照片从Silverlight客户端上传到服务器。

The method called by the client is void UpdatePicture(Stream image); 客户端调用的方法是无效的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()) 在客户端,此方法显示为UpdatePicture(byte [] array),因此我创建了一个转换器(输入流是来自OpenFileDialog.File.OpenRead()的FileStream)

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. 在WCF端,我必须将流保存到文件中。 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 : 要将Stream复制到FileStream,我使用以下命令:

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. 文件已按预期创建,大小确定,但是PhotoViewer或任何其他程序均不显示图像。

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. 我创建了WCF方法GetWCFBytes(byte [] array),该方法不执行任何操作即可返回参数。 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. 如果使用StreamToByteArray将流作为字节数组传递给此方法,然后通过带有MemoryStream的BitmapImage将结果设置为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. 如果我使用OpenFileDialog的流,则将其转换为字节数组,从该数组创建一个新的MemoryStream,并为其设置BitmapImage:图像正常。

Does WCF use some magic against streams and byte arrays ? WCF是否对流和字节数组使用了一些魔术?

Your CopyStream method makes sure to keep reading from the input stream until it doesn't get anything more. 您的CopyStream方法可确保继续从输入流中读取内容,直到没有任何其他信息为止。 Your StreamToByteArray method doesn't. 您的StreamToByteArray方法没有。 Are you sure you're converting the entire stream on the client, rather than just the first x bytes followed by zeroes? 您确定要在客户端上转换整个流,而不是仅转换前x个字节后跟零吗?

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 ! 我找到了答案,并且与WCF无关!

The problem is that I convert my OpenFileDialog result on confirm button on in my ViewModel. 问题是我在ViewModel中的确认按钮上转换了OpenFileDialog结果。 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. 我不知道为什么,但是如果我使用称为openfiledialog的方法进行转换,则字节数组不会损坏,并且一切正常。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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