UWP如何显示网络目录中的图像 [英] UWP how to show image from a network directory
问题描述
我尝试了多种方法将图像源设置为UWP应用程序中的网络目录.例如,我尝试过以下示例:
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;
和
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和其他资源中读取了很多链接,但是没有任何适合我的东西.
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.
我已经尝试过使用Windows.Storage.Pickers.FolderPicker
进行尝试,但是找不到任何可以设置读取文件位置的文件夹.
我不想打开文件夹选择器,我只想直接从网络的指定位置获取图像.
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
我遇到System.UnauthorizedAccessException: 'Access is denied.
错误
推荐答案
要在网络上的共享文件夹中设置.jpg文件,我们可以先获取StorageFile
,然后使用SetSource
方法将源设置为BitmapImage
.要访问共享文件夹中的文件,我们需要在应用清单中声明一些功能.
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.
通用命名约定(UNC)是Microsoft Windows中常用于访问共享网络文件夹的命名系统.有关更多信息,请参考文件访问权限.
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.
这是我的Package.appxmanifest:
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>
设置BitmapImage的代码:
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;
}
这篇关于UWP如何显示网络目录中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!