2017-08-30 92 views
0

我已經嘗試了很多方法將圖像源設置爲我的UWP應用程序中的網絡目錄。例如,我已經試過了這個例子:UWP如何從網絡目錄中顯示圖像

BitmapImage bitmap = new BitmapImage(new Uri(@"\\venera\Mariner\747\03_WEDNESDAY\003\00 Flat B -\APR3783216-MED-BLK TAPE-GB-Apron.jpg")); 
UserImage.Source = bitmap; 

bitmap.UriSource = new Uri(@"\\venera\Mariner\747\03_WEDNESDAY\003\00 Flat B -\APR3783216-MED-BLK TAPE-GB-Apron.jpg", UriKind.Absolute); 
UserImage.Source = bitmap; 

但他們沒有工作。我結束了以下錯誤E_NETWORK_ERROR我已經閱讀了很多來自stackoverflow和其他資源的鏈接,但沒有任何東西適用於我。

我已經爲它設置了所需的功能和聲明。

我已經用Windows.Storage.Pickers.FolderPicker試過了,但是我找不到任何可以設置文件夾位置讀取文件的地方。 我不想打開文件夾選取器,我只想直接從指定的網絡位置獲取圖像。

可能會有人試圖將它與此票相關How to display an image from a network folder or local drive in a Windows Universal App 但這並不能幫助我完成我的任務。

我也嘗試了這些例子對我來說,但仍無法達到目標: https://docs.microsoft.com/en-us/windows/uwp/files/quickstart-managing-folders-in-the-music-pictures-and-videos-libraries

https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.image

我得到System.UnauthorizedAccessException: 'Access is denied.錯誤

+0

我曾經有類似的問題。我的解決方案是,Visual Studio以本地管理員身份運行。在我的情況下,本地管理員沒有權限從網絡文件夾中讀取 – Noren

+0

@Noren我已經爲每個人設置了訪問這些文件的權限 –

+0

我敢打賭,如果您使用您的帳戶編譯並在資源管理器中打開它,它就會起作用。僅改變文件系統的權限是不夠的。 – Noren

回答

3

和網絡上的共享文件夾中設置一個.jpg文件,我們可以得到StorageFile第一,然後使用SetSource方法來設置源到BitmapImage。要訪問共享文件夾中的文件,我們需要在應用清單中聲明一些功能。

enter image description here

通用命名約定(UNC)是Microsoft Windows通常用於訪問共享網絡文件夾的命名系統。詳情請參閱。

這是我Package.appxmanifest:

<Applications> 
    <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="UWP_how_to_show_image_from_a_network.App"> 
    <uap:VisualElements DisplayName="UWP how to show image from a network" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="UWP how to show image from a network" BackgroundColor="transparent"> 
     <uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png"> 
     </uap:DefaultTile> 
     <uap:SplashScreen Image="Assets\SplashScreen.png" /> 
     </uap:VisualElements> 
    <Extensions> 
     <uap:Extension Category="windows.fileTypeAssociation"> 
     <uap:FileTypeAssociation Name="mypictest"> 
      <uap:DisplayName>MyPicTest</uap:DisplayName> 
      <uap:SupportedFileTypes> 
      <uap:FileType>.jpg</uap:FileType> 
      </uap:SupportedFileTypes> 
     </uap:FileTypeAssociation> 
     </uap:Extension> 
    </Extensions> 
    </Application> 
</Applications> 
<Capabilities> 
    <Capability Name="internetClient" /> 
    <Capability Name="privateNetworkClientServer" /> 
    <Capability Name="internetClientServer" /> 
    <uap:Capability Name="enterpriseAuthentication" /> 
</Capabilities> 

設置的BitmapImage的代碼:

StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(@"\\venera\Mariner\747\03_WEDNESDAY\003\00 Flat B -"); 
    StorageFile file = await folder.GetFileAsync("APR3783216-MED-BLK TAPE-GB-Apron.jpg"); 
    using (var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read)){ 
    BitmapImage bitmap = new BitmapImage(); 
    bitmap.SetSource(stream); 
    UserImage.Source = bitmap; 
} 
2

不能使用任何路徑。只有KnownFolders和本地應用程序路徑。但是,你可能會得到的字節數組從任何地方:

  var file = File.ReadAllBytes(_path); 

      var bitmap = new BitmapImage(); 

      using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream()) 
      { 
       await stream.WriteAsync(file.AsBuffer()); 
       stream.Seek(0); 
       await bitmap.SetSourceAsync(stream); 
      }