我的目标是使用Web服务上传和下载图像。我明白,为了做到这一点,图像需要转换为字节数组。但是,将字节数组转换为BitmapImage时出现“未指定错误”。将字节数组转换为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)的结果;'?它应该返回读取的字节数。 – 2012-02-01 06:13:29
我检查了e.ChosenPhoto.Read()的结果,并且它返回0,即使e.ChosenPhoto.Length是119264 - 我在创建字节数组时丢失了什么吗? – 2012-02-01 06:41:21