【问题标题】:“Unspecified error” when converting a byte array into a BitmapImage将字节数组转换为 BitmapImage 时出现“未指定错误”
【发布时间】:2012-02-01 06:04:01
【问题描述】:

我的目标是使用网络服务上传和下载图像。我知道为了做到这一点,需要将图像转换为字节数组。但是,将字节数组转换为 BitmapImage 时出现“未指定错误”。

我创建了一个测试装置,可以将图像(来自 PhotoChooserTask)转换为字节数组并再次返回,从而重现我的问题。下面列出了进行转换的代码,其中突出显示了问题行。

任何帮助将不胜感激!

private void PhotoChooserTaskCompleted(object sender, PhotoResult e)
{

    if (e.TaskResult == TaskResult.OK)
    {
        //Display the photo
        BitmapImage PhotoBitmap = new BitmapImage();
        PhotoBitmap.SetSource(e.ChosenPhoto);
        Photo.Source = PhotoBitmap;

        //Convert the photo to bytes
        Byte[] PhotoBytes = new byte[e.ChosenPhoto.Length];
        e.ChosenPhoto.Read(PhotoBytes, 0, PhotoBytes.Length);

        //Convert the bytes back to a bitmap
        BitmapImage RestoredBitmap = new BitmapImage();
        MemoryStream stream = new MemoryStream(PhotoBytes);
        BitmapImage image = new BitmapImage();
        RestoredBitmap.SetSource(stream);    //<------ I get "Unspecified error" on this line

        //Display the restored photo
        RestoredPhoto.Source = RestoredBitmap;
    }
}

【问题讨论】:

  • 你能检查e.ChosenPhoto.Read(PhotoBytes, 0, PhotoBytes.Length);的结果吗?它应该返回读取的字节数。
  • 我检查了 e.ChosenPhoto.Read() 的结果,即使 e.ChosenPhoto.Length 为 119264,它也返回 0 - 创建字节数组时是否遗漏了什么?

标签: c# .net wcf silverlight windows-phone-7


【解决方案1】:

第一次使用e.ChosePhoto 作为源时,将读取流并将Position 属性推进到最后。您可以在调试器中检查PhotoBytes 数组,以查看在您的读取操作之后它实际上没有任何内容(或检查Read 方法的返回值以确认读取了零字节)。

您需要做的是在再次读取之前将 Position 重置为零:

//Convert the photo to bytes
Byte[] PhotoBytes = new byte[e.ChosenPhoto.Length];

// rewind first
e.ChosenPhoto.Position = 0;

// now succeeds
e.ChosenPhoto.Read(PhotoBytes, 0, PhotoBytes.Length);

【讨论】:

  • 正确,我支持 Goodcat 先生,重置位置使其可以正常获取流数据。 :)
【解决方案2】:

我敢打赌这就是正在发生的事情(cmets inline):

//Display the photo
BitmapImage PhotoBitmap = new BitmapImage();
PhotoBitmap.SetSource(e.ChosenPhoto); // This is reading from the stream
Photo.Source = PhotoBitmap;

//Convert the photo to bytes
Byte[] PhotoBytes = new byte[e.ChosenPhoto.Length];
e.ChosenPhoto.Read(PhotoBytes, 0, PhotoBytes.Length); // Fails to read the full stream
                                                      // because you already read from it

//Convert the bytes back to a bitmap
BitmapImage RestoredBitmap = new BitmapImage();
MemoryStream stream = new MemoryStream(PhotoBytes); // You're creating a stream that
                                                    // doesn't contain the image
BitmapImage image = new BitmapImage();
RestoredBitmap.SetSource(stream); // Fails because your stream is incomplete

Seek 在尝试从流中读取之前将其设置为 0。并检查来自Read 调用的返回值,以确保它与PhotoBytes.Length 匹配。

【讨论】:

    【解决方案3】:

    这个:

    //Display the photo
    BitmapImage PhotoBitmap = new BitmapImage();
    PhotoBitmap.SetSource(e.ChosenPhoto);
    Photo.Source = PhotoBitmap;
    

    使用e.ChosenPhoto 的流并且可能不会倒回流的位置。

    所以当你这样做时:

    Byte[] PhotoBytes = new byte[e.ChosenPhoto.Length];
    e.ChosenPhoto.Read(PhotoBytes, 0, PhotoBytes.Length);
    

    你从流的末尾开始,什么也没读。

    使用 Seek 重置流的位置。

    【讨论】:

      【解决方案4】:

      您是否查看了我已经做过的其他帖子? 我从它那里得到了很好的评价。

      BitmapImage to byte[] and byte[] to BitmapImage

      【讨论】:

        猜你喜欢
        • 2016-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-14
        相关资源
        最近更新 更多