简体   繁体   English

如何在Windows Universal App中显示网络文件夹或本地驱动器中的图像

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

I'm having trouble displaying an jpg in an Image control in a Windows Universal App. 我在Windows通用应用程序的图像控件中显示jpg时遇到问题。 (I had the same problem trying to create a Windows 8 Store app as well) (我在尝试创建Windows 8商店应用时遇到了同样的问题)

I have a simple form with an Image control on it. 我有一个带有Image控件的简单表单。 All I want to do is be able to open images in a folder on my local drive or a network drive on my local network and display them. 我想要做的就是能够打开本地驱动器上的文件夹或本地网络上的网络驱动器中的图像并显示它们。 But I am not having any luck. 但我没有运气。 The only thing I get is E_NETWORK_ERROR, with no additional information. 我唯一得到的是E_NETWORK_ERROR,没有其他信息。

I assume it probably has something to do with security, but surely there must be a setting or permission to allow me to do it. 我认为它可能与安全有关,但肯定必须有一个设置或许可允许我这样做。 I tried enabling Private Networks in the Capabilities tab of the manifest, but that didn't help. 我尝试在清单的功能选项卡中启用专用网络,但这没有帮助。 I don't see anything in Declarations that sounds like what I need. 我没有在声明中看到任何听起来像我需要的东西。

I know UWP apps are somewhat sandboxed, but if you can't even access local files, what good are they? 我知道UWP应用程序有点沙盒,但如果你甚至无法访问本地文件,它们有什么用处?

Here is a sample of code I have tried. 这是我尝试过的代码示例。 I've done other iterations as well, but they all have the same end result. 我也完成了其他迭代,但它们都有相同的最终结果。

Xaml: XAML:

<Image Name="Image1"/>

Code behind: 代码背后:

    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);
    }

When I run it, I end up in the Bitmap_ImageFailed event and the ErrorMessage property is simply "E_NETWORK_ERROR", and nothing is displayed in the Image. 当我运行它时,我最终在Bitmap_ImageFailed事件中,而ErrorMessage属性只是“E_NETWORK_ERROR”,并且图像中没有显示任何内容。 Not very helpful. 不是很有帮助。

What am I missing? 我错过了什么? It has to be something simple and obvious that I am overlooking. 它必须是一个简单而明显的东西,我在俯视。

Update: 更新:

Thanks to all the suggestions here I was able to get it going. 感谢这里的所有建议,我能够顺利实现。 The part I was failing to get through my skull was that you can't just give it a folder and expect it to read a file, even as a "quick & dirty test". 我没有通过我的头骨的部分是,你不能只是给它一个文件夹,并期望它读取文件,即使是“快速和肮脏的测试”。 You have to go through "proper channels" to get there. 你必须经过“适当的渠道”才能到达那里。 I pieced it all together and came up with this example which displays the first image in the selected folder: 我将它拼凑在一起并想出了这个示例,它显示了所选文件夹中的第一个图像:

    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;
        }
    }

In addition, I had to add the file types for .jpg, .tif and .png as well as File Open Picker to the Declarations. 另外,我必须将.jpg,.tif和.png以及File Open Picker的文件类型添加到声明中。

You can figure out all necessary information in MSDN article File access permissions 您可以在MSDN文章文件访问权限中找出所有必要的信息

In addition to the default locations, an app can access additional files and folders by declaring capabilities in the app manifest (see 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 ). 除了默认位置,应用程序还可以通过在应用程序清单中声明功能(请参阅应用程序功能声明 )或通过调用文件选择器来让用户选择要访问的应用程序的文件和文件夹来访问其他文件和文件夹(请参阅使用选择器打开文件和文件夹

So if you want to read a file from users document folder you need to update your applications AppXManifest to request the Document Library Access capability . 因此,如果要从用户文档文件夹中读取文件,则需要更新应用程序AppXManifest以请求文档库访问功能

You also need to update your AppXManifest by declaring what file type(s) you want to access. 您还需要通过声明要访问的文件类型来更新AppXManifest。 Then, even with access to the folders, you only have access to a limited set of file types. 然后,即使访问文件夹,您也只能访问一组有限的文件类型。 You have to specify supported files types on Declarations tab 您必须在声明选项卡上指定支持的文件类型

在此输入图像描述

I set a new file type (.txt) and let it role from there. 我设置了一个新的文件类型(.txt)并让它从那里开始。 And code example 和代码示例

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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何显示存储在本地驱动器或网络驱动器上的图像? - How to display image which is stored on Local drive or network drive? Visual Studio2015。C#。 如何将图像从本地驱动器设置为图像源窗口通用应用程序 - Visual Studio 2015. C#. How to set image from local drive to image.Source window universal app 如何将从url(服务器)下载的图像保存到Windows应用商店中的本地文件夹 - how to save image downloaded from url(server) to local folder in windows store app 如何从 Windows 服务访问连接到本地网络上路由器的 USB 驱动器 - How to access a USB drive attached to router on local network from windows service Xamarin 显示来自网络共享文件夹的图像 - Xamarin display an image from a network shared folder 显示本地文件夹中的图像,其中图像名称作为来源从Windows Phone 8应用程序中的sqlite数据库获取 - display image from local folder where image name as source get from sqlite database in windows phone 8 apps 如何在通用Windows应用程序中将IP配置设置为网络适配器? - How to set IP config to network adapter in Universal windows app? 如何在本地项目文件夹中的Datagridview Windows窗体中绑定图像 - How to bind image in datagridview windows forms from local project folder 在Universal App Windows 10中从.NET IDE访问Assets文件夹 - Access the Assets folder from the .NET IDE in Universal App Windows 10 如何从Winform启动Windows Universal App - How to launch a Windows Universal App from winform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM