【问题标题】:UWP how to show image from a network directoryUWP如何显示网络目录中的图像
【发布时间】:2018-02-08 00:27:06
【问题描述】:

我已经尝试了很多方法来在我的 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. 错误

【问题讨论】:

  • 我曾经遇到过类似的问题。我的解决方案是,Visual Studio 以本地管理员身份运行。在我的情况下,本地管理员没有权限从网络文件夹中读取
  • @Noren 我已经为每个人设置了访问这些文件的权限
  • 我敢打赌,如果您编译它并使用您的帐户在资源管理器中打开它,它就会起作用。仅更改文件系统的权限是不够的。

标签: c# uwp uwp-xaml network-shares


【解决方案1】:

您不能使用任何路径。只有 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);
            }

【讨论】:

    【解决方案2】:

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

    通用命名约定 (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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-27
      • 2016-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      相关资源
      最近更新 更多