将字节[]转换为Windows Phone 7中C#格式的图像类型

将字节[]转换为Windows Phone 7中C#格式的图像类型,c#,image,xaml,silverlight,windows-phone-7,C#,Image,Xaml,Silverlight,Windows Phone 7,我在将字节数组转换为图像类型以在Windows Phone 7上的应用程序中显示时遇到问题 数据是从服务器检索的,当我上传和下载数据时,它工作得很好,但当我将其转换回图像格式时,我很困难 有人能为我解释一下这个问题吗 这是我将字节数组转换为位图图像的方法 public BitmapImage decodeImage(byte[] array) { MemoryStream ms = new MemoryStream(array, 0, array.Length); // Con

我在将字节数组转换为图像类型以在Windows Phone 7上的应用程序中显示时遇到问题

数据是从服务器检索的,当我上传和下载数据时,它工作得很好,但当我将其转换回图像格式时,我很困难

有人能为我解释一下这个问题吗

这是我将字节数组转换为位图图像的方法

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;
}    
然后,在这段代码中,我尝试将返回的位图图像设置为我在XAML UI中使用的图像框的源

BitmapImage usrIMG = new BitmapImage();
usrIMG = getJson.decodeImage(userProfile.Photos[0].Image);
profileImage.Source = usrIMG;
我知道代码看起来乱七八糟的,我正在声明我不需要的东西,我已经摆弄它好多年了,我完全不知所措


非常感谢

您需要一个
可写位图
,并且需要知道图像的高度和宽度才能做到这一点。
然后你可以这样做:

var result = new WriteableBitmap(width, height);
var ms = new MemoryStream();
ms.Write(myByteArray, myByteArray, myByteArray.Length);
result.SetSource(ms);

下面的代码在使用PhotoChooserTask并将所选图像存储在字节数组中的快速测试中非常适合我。您可能还需要检查代码,在那里存储和检索字节数组,以确保没有丢失任何内容

private byte[] imageBytes;
private void GetPhoto_Click(object sender, RoutedEventArgs e)
{
    PhotoChooserTask photoTask = new PhotoChooserTask();
    photoTask.Completed += new EventHandler<PhotoResult>(photoTask_Completed);
    photoTask.Show();
}

void photoTask_Completed(object sender, PhotoResult e)
{
    imageBytes = new byte[e.ChosenPhoto.Length];
    e.ChosenPhoto.Read(imageBytes, 0, imageBytes.Length);

    // save 'imageBytes' byte array to data base ...
}

private void ShowPhoto_Click(object sender, RoutedEventArgs e)
{
    // load 'imageBytes' byte array to data base ...
    BitmapImage bitmapImage = new BitmapImage();
    MemoryStream ms = new MemoryStream(imageBytes);
    bitmapImage.SetSource(ms);
    myImageElement.Source = bitmapImage;
}
private byte[]imageBytes;
私有void GetPhoto_单击(对象发送方,路由目标)
{
PhotoChooserTask photoTask=新的PhotoChooserTask();
photoTask.Completed+=新事件处理程序(photoTask_Completed);
Show();
}
void photoTask_已完成(对象发送方,PhotoResult e)
{
imageBytes=新字节[e.ChosenPhoto.Length];
e、 读取(imageBytes,0,imageBytes.Length);
//将“imageBytes”字节数组保存到数据库。。。
}
私有void ShowPhoto_单击(对象发送者,路由目标)
{
//将“imageBytes”字节数组加载到数据库。。。
BitmapImage BitmapImage=新的BitmapImage();
MemoryStream ms=新的MemoryStream(imageBytes);
bitmapImage.SetSource(毫秒);
myImageElement.Source=bitmapImage;
}

var bitmapImage=new bitmapImage()

bitmapImage.SetSource(新的MemoryStream(…二进制数组数据…)

img1.Source=位图图像

public BitmapImage ByteArraytoBitmap(Byte[] byteArray)
        {
            MemoryStream stream = new MemoryStream(byteArray);
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.SetSource(stream);
            return bitmapImage;
        }

我以前使用过这个代码,它的工作100%成功

您在字节数组中表示什么图像格式?你最初是如何保存它的?我从WP7中的PhotoChooser工具中获取它,然后将它直接放入流中,我不确定,但我认为它的格式是PhotoStream,因为这是我选择照片时返回的类型。你不需要
ms.Write
,因为你在构建
MemoryStream
时已经提供了数据。我尝试过这个方法,但仍然不起作用,我认为问题在于将接收的文件设置为显示的图像