【问题标题】:Converting Byte[] to Image type in C# for Windows Phone 7在 Windows Phone 7 的 C# 中将 Byte[] 转换为图像类型
【发布时间】: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


【解决方案1】:

在快速测试您使用 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;
}

【讨论】:

    【解决方案2】:

    您需要WritableBitmap 并知道图片的高度和宽度才能执行此操作。
    然后你可以这样做:

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

    【讨论】:

    • 我试过了,但还是不行,我认为问题在于将接收到的文件设置为显示的图像
    【解决方案3】:

    var bitmapImage = new BitmapImage();

    bitmapImage.SetSource(new MemoryStream(..Binary array Data..));

    img1.Source = bitmapImage;

    【讨论】:

      【解决方案4】:
      public BitmapImage ByteArraytoBitmap(Byte[] byteArray)
              {
                  MemoryStream stream = new MemoryStream(byteArray);
                  BitmapImage bitmapImage = new BitmapImage();
                  bitmapImage.SetSource(stream);
                  return bitmapImage;
              }
      

      我之前使用过此代码,它的工作 100% 成功。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-02
        • 2023-04-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多