将流转换为 byte[] 数组总是 returns 0 长度在 windows phone 8 C#
Converting Stream to byte[] array always returns 0 length in windows phone 8 C#
我从 PhotoResult
的 photoChooserTask_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;
}
我不知道 BinaryReader 有什么问题,它 returns imageData
只有 0 长度。尝试将类型转换为 br.ReadBytes((int)mystream.Length)
但仍然无效。
也尝试了 Creating a byte array from a stream 中的所有答案,但仍然无效。也许我的 e.ChosenPhoto
不能用作普通的 Stream。
谢谢。
根据docs,您可能必须在读取流之前将流的位置设置为 0:
using (BinaryReader br = new BinaryReader(mystream))
{
mystream.Position = 0;
imageData = br.ReadBytes(Convert.ToInt32(mystream.Length));
}
我从 PhotoResult
的 photoChooserTask_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;
}
我不知道 BinaryReader 有什么问题,它 returns imageData
只有 0 长度。尝试将类型转换为 br.ReadBytes((int)mystream.Length)
但仍然无效。
也尝试了 Creating a byte array from a stream 中的所有答案,但仍然无效。也许我的 e.ChosenPhoto
不能用作普通的 Stream。
谢谢。
根据docs,您可能必须在读取流之前将流的位置设置为 0:
using (BinaryReader br = new BinaryReader(mystream))
{
mystream.Position = 0;
imageData = br.ReadBytes(Convert.ToInt32(mystream.Length));
}