问题
I have tried many ways to set image source to a network directory in my UWP application. For example I have tried this example:
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;
and
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;
But none of them works. I ended up with the following error E_NETWORK_ERROR
I have read many links from stackoverflow and from other resource but there is not any thing which works for me.
I have set required capabilities and declarations for it.
I have tried it with Windows.Storage.Pickers.FolderPicker
but I couldn't find any thing where I can set the folder location where to read files.
I don want to open folder picker, I just want to get images directly from a specified location of network.
May be some you try to relate it with this ticket How to display an image from a network folder or local drive in a Windows Universal App but this is not helping me in my task.
I also have tried these examples for me but still couldn't achieve the target: 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
I am getting System.UnauthorizedAccessException: 'Access is denied.
Error
回答1:
To set a .jpg file in shared folder on network, we can get the StorageFile
first and then use the SetSource
method to set the source to the BitmapImage
. To access files in the shared folder, we need declare some capabilities in the app manifest.
Universal Naming Convention (UNC) is the naming system commonly used in Microsoft Windows for accessing shared network folders. For more info, please refer File access permissions.
This is my 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>
The code of setting 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:
You can't use any path. Only KnownFolders and local app path. But you may get array of bytes from anywhere:
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);
}
来源:https://stackoverflow.com/questions/45957886/uwp-how-to-show-image-from-a-network-directory