【发布时间】: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