【发布时间】:2011-04-06 11:20:22
【问题描述】:
我在将 Byte 数组转换为 Image 类型以便在 Windows Phone 7 上的应用程序中显示时遇到问题。
数据是从服务器检索的,当我上传和下载数据时它工作正常,但是在将其转换回图像格式时我很挣扎。
谁能帮我解释一下这个问题?
这是我把Byte数组变成BitmapImage的方法,
public BitmapImage decodeImage(byte[] array)
{
MemoryStream ms = new MemoryStream(array, 0, array.Length);
// Convert byte[] to Image
ms.Write(array, 0, array.Length);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(ms);
return bitmapImage;
}
然后这是我尝试将返回的 BitmapImage 设置为我在 XAML UI 中使用的图像框的源的代码。
BitmapImage usrIMG = new BitmapImage();
usrIMG = getJson.decodeImage(userProfile.Photos[0].Image);
profileImage.Source = usrIMG;
我知道代码看起来乱七八糟,我声明了一些我不需要的东西,我已经摆弄了很多年,我完全不知所措。
非常感谢
【问题讨论】:
-
您在字节数组中代表什么图像格式?你最初是如何保存它的?
-
我从 WP7 的 PhotoChooser 工具中获取它,然后将其直接放入流中,我不确定但我认为它的格式是 PhotoStream,因为这是我选择照片时返回的类型.
-
你不应该需要
ms.Write,因为你在构建MemoryStream时已经提供了数据。
标签: c# image xaml silverlight windows-phone-7