簡體   English   中英

FileStream打開圖像“ System.UnauthorizedAccessException”,拒絕訪問該路徑

[英]FileStream to open an image “System.UnauthorizedAccessException” Access to the path is denied

我正在寫一個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); 我遇到一個錯誤。

System.UnauthorizedAccessException:拒絕訪問路徑“ C:\\ Data \\ Users \\ Public \\ Pictures \\ Camera Roll \\ WP_20140331_001.jpg”。

我在WMAppMainfest.xml中選擇了功能`D_CAP_MEDIALIB_PHOTO

我認為您無法像那樣訪問相機膠卷。 您可能必須使用MediaLibrary類。 另外,您還可以在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);

您也可以立即刪除(新)圖片! (如果你想)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM