C# 文件流以打开图像;系统.未经授权的访问例外情况“;对路径的访问被拒绝

C# 文件流以打开图像;系统.未经授权的访问例外情况“;对路径的访问被拒绝,c#,windows-phone-8,C#,Windows Phone 8,我正在写一个wp8应用程序。我有一个问题困扰了我几天。 我想上传一张照片到服务器。我从相册中选择了一张照片,并使用FileStream上传它,但我无法打开它。它说访问路径被拒绝 PhotoChooserTask photoChooserTask = new PhotoChooserTask(); photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed); void

我正在写一个wp8应用程序。我有一个问题困扰了我几天。 我想上传一张照片到服务器。我从相册中选择了一张照片,并使用FileStream上传它,但我无法打开它。它说访问路径被拒绝

PhotoChooserTask photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);

void photoChooserTask_Completed(object sender, PhotoResult e)
{
      if (e.TaskResult == TaskResult.OK)
      {
          // show the img
          BitmapImage bmp = new BitmapImage();
          bmp.SetSource(e.ChosenPhoto);
          ShowPhoto.Source = bmp;

          // get path of img
          string imagePath = e.OriginalFileName;
      }
}
在第行:FileStream fs=newfilestream(imagePath,FileMode.Open,FileAccess.Read); 我遇到了一个错误

System.UnauthorizedAccessException:对路径“C:\Data\Users\Public\Pictures\Camera Roll\WP\U 20140331\U 001.jpg”的访问被拒绝

PhotoChooserTask photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);

void photoChooserTask_Completed(object sender, PhotoResult e)
{
      if (e.TaskResult == TaskResult.OK)
      {
          // show the img
          BitmapImage bmp = new BitmapImage();
          bmp.SetSource(e.ChosenPhoto);
          ShowPhoto.Source = bmp;

          // get path of img
          string imagePath = e.OriginalFileName;
      }
}

我已经在WMAppMainfest.xml中选择了函数'D_CAP_MEDIALIB_PHOTO

我认为您无法像那样访问相机卷。您可能必须使用MediaLibrary类才能进行相同的操作。此外,您在PhotoChooserTask_Completed事件处理程序中有图像。你不必进入文件流。

我看到你自己解决了你的问题 这里仍然没有解决代码 我希望有人会发现它的用处:)


您还可以立即删除(新)图片!(如果您愿意)

我成功地解决了这个问题。我从e.ChosenPhoto获取流,并将其传输到byte[]进行上传。非常感谢。
  var imageAsByteArray = File.ReadAllBytes(imagePath);

  // I use as example a pictureBox:
  pictureBox1.Image = byteArrayToImage(imageAsByteArray);

  // Or safe/copy/replace it:
  File.WriteAllBytes(picture_Path, imageAsByteArray);