首页 文章

UWP如何从网络目录中显示图像

提问于
浏览
0

我已经尝试了很多方法将图像源设置为我的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. 错误

2 回答

  • 4

    要在网络上的共享文件夹中设置.jpg文件,我们可以先获取 StorageFile ,然后使用 SetSource 方法将源设置为 BitmapImage . 要访问共享文件夹中的文件,我们需要在应用清单中声明一些功能 .

    enter image description here

    通用命名约定(UNC)是Microsoft Windows中常用于访问共享网络文件夹的命名系统 . 有关详细信息,请参阅File access permissions .

    这是我的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);
                }
    

相关问题