将Stream转换为byte []数组始终在Windows Phone 8 C#中返回0长度

Tra*_*Nam 4 c# stream windows-phone-8

我从接到流PhotoResultphotoChooserTask_Completed(object sender, PhotoResult e)事件处理程序.

e.ChosenPhoto本身就是一个Stream,所以我将它分配给它Stream stream.我使用以下方法将其转换为byte []数组:

    public static byte[] ReadImageFile2(Stream mystream)
    {
        // The mystream.length is still full here.
        byte[] imageData = null;
        using (BinaryReader br = new BinaryReader(mystream))
        {
            imageData = br.ReadBytes(Convert.ToInt32(mystream.Length));
        }
        // But imageData.length is 0
        return imageData;
    }
Run Code Online (Sandbox Code Playgroud)

我不知道BinaryReader有什么问题,只返回imageData0长度.试图将类型转换为br.ReadBytes((int)mystream.Length)但仍然无效.

还尝试了从流创建字节数组但仍然无法正常工作的所有答案.也许我e.ChosenPhoto不能用作普通的Stream.

谢谢.

Gra*_*ICA 9

根据文档,您可能必须在读取之前将流的位置设置为0:

using (BinaryReader br = new BinaryReader(mystream))
{
    mystream.Position = 0;
    imageData = br.ReadBytes(Convert.ToInt32(mystream.Length));
}
Run Code Online (Sandbox Code Playgroud)