如何在 Windows 通用应用程序中显示来自网络文件夹或本地驱动器的图像

How to display an image from a network folder or local drive in a Windows Universal App

我无法在 Windows 通用应用程序的图像控件中显示 jpg。 (我在尝试创建 Windows 8 Store 应用程序时也遇到了同样的问题)

我有一个带有图像控件的简单窗体。我想要做的就是能够打开本地驱动器上的文件夹或本地网络上的网络驱动器中的图像并显示它们。但我没有任何运气。我得到的唯一信息是 E_NETWORK_ERROR,没有其他信息。

我想这可能与安全有关,但肯定有设置或许可允许我这样做。我尝试在清单的功能选项卡中启用专用网络,但这没有帮助。我在声明中没有看到任何听起来像我需要的东西。

我知道 UWP 应用有些沙盒化,但如果您甚至无法访问本地文件,它们有什么用?

这是我试过的代码示例。我也做了其他迭代,但它们都有相同的最终结果。

Xaml:

<Image Name="Image1"/>

后面的代码:

    public LoadImage()
    {
        var bitmap = new BitmapImage();
        bitmap.ImageFailed += Bitmap_ImageFailed;
        bitmap.UriSource = new Uri(@"D:\Users\Steve\Documents\SomeImage.JPG", UriKind.Absolute);
        Image1.Source = bitmap;
    }

    private void Bitmap_ImageFailed(object sender, ExceptionRoutedEventArgs e)
    {
        Debug.Write(e.ErrorMessage);
    }

当我 运行 它时,我最终进入 Bitmap_ImageFailed 事件并且错误消息 属性 只是 "E_NETWORK_ERROR",图像中没有显示任何内容。帮助不大。

我错过了什么?它必须是我忽略的简单明了的东西。

更新:

多亏了这里的所有建议,我才得以顺利进行。我无法理解的部分是你不能只给它一个文件夹并期望它读取一个文件,即使是 "quick & dirty test"。您必须经过 "proper channels" 才能到达那里。我将它们拼凑在一起并想出了这个显示所选文件夹中的第一张图像的示例:

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        FolderPicker folderPicker = new FolderPicker();
        folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
        folderPicker.FileTypeFilter.Add(".jpg");
        folderPicker.FileTypeFilter.Add(".tif");
        folderPicker.FileTypeFilter.Add(".png");

        StorageFolder folder = await folderPicker.PickSingleFolderAsync();
        if (folder != null)
        {
            StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
            var files = await folder.GetFilesAsync();

            var bitmap = new BitmapImage();
            bitmap.ImageFailed += Bitmap_ImageFailed;
            var stream = await files.First().OpenReadAsync();
            await bitmap.SetSourceAsync(stream);
            Image1.Source = bitmap;
        }
    }

此外,我必须将 .jpg、.tif 和 .png 的文件类型以及文件打开选择器添加到声明中。

您可以在 MSDN 文章 File access permissions

中找到所有必要的信息

除了默认位置外,应用还可以通过在应用清单中声明功能来访问其他文件和文件夹(请参阅 App capability declarations), or by calling a file picker to let the user pick files and folders for the app to access (see Open files and folders with a picker)。

因此,如果您想从用户文档文件夹中读取文件,您需要更新您的应用程序 AppXManifest 以请求 Document Library Access capability

您还需要通过声明要访问的文件类型来更新您的 AppXManifest。然后,即使可以访问这些文件夹,您也只能访问一组有限的文件类型。您必须在声明选项卡上指定支持的文件类型

我设置了一个新的文件类型 (.txt) 并让它从那里发挥作用。和代码示例

async void Button_Click_2(object sender, RoutedEventArgs e)
{
    var _Name = "HelloWorld.txt";
    var _Folder = KnownFolders.DocumentsLibrary;
    var _Option = Windows.Storage.CreationCollisionOption.ReplaceExisting;

    // create file 
    var _File = await _Folder.CreateFileAsync(_Name, _Option);

    // write content
    var _WriteThis = "Hello world!";
    await Windows.Storage.FileIO.WriteTextAsync(_File, _WriteThis);

    // acquire file
    try { _File = await _Folder.GetFileAsync(_Name); }
    catch (FileNotFoundException) { /* TODO */ }

    // read content
    var _Content = await FileIO.ReadTextAsync(_File);
    await new Windows.UI.Popups.MessageDialog(_Content).ShowAsync();
}