文件流打开图像“系统.未经授权访问异常” 对路径的访问被拒绝

本文关键字:访问 异常 路径 拒绝 授权 图像 系统 文件 | 更新日期: 2023-09-27 17:55:43

我正在编写一个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;
      }
}

上传

if (imagePath != null)
{
     FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
     StreamContent imageContent = new StreamContent(fs);
}

在行: FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read);我遇到了一个错误。

系统。未经授权访问异常:拒绝访问路径"C:''数据''用户''公共''图片''相机胶卷''WP_20140331_001.jpg"。

我在WMAppMainfest中选择了'D_CAP_MEDIALIB_PHOTO功能.xml

文件流打开图像“系统.未经授权访问异常” 对路径的访问被拒绝

我认为你不能像那样访问相机胶卷。您可能必须使用相同的媒体库类。此外,您在 PhotoChooserTask_Completed 事件处理程序中具有图像。您不必进入文件流。

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

  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);

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