在metroapp中显示存储在Storage文件中的图片 [英] Displaying a picture stored in Storage file in a metroapp

查看:28
本文介绍了在metroapp中显示存储在Storage文件中的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过绑定显示存储在 StorageFile 中的图片的内容,但无论我尝试做什么似乎都不起作用.

以下是我已经测试过的两种解决方案:

string img =( await CompetencesFolder.GetFileAsync(FormatImageNameToFileName(imageName))).Path;

然后将获取的路径(文件的完整绝对路径)通过绑定返回给源属性,并且:

 BitmapImage img = await LoadImage(await CompetencesFolder.GetFileAsync(FormatImageNameToFileName(imageName)));私有静态异步任务加载图像(存储文件文件){BitmapImage bitmapImage = new BitmapImage();FileRandomAccessStream stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read);bitmapImage.SetSource(stream);返回位图图像;}

稍后将最终的 bitmapImage 返回到我的绑定属性.

这些方法都不起作用..

有人有想法吗?

修复

这是解决问题的代码:

BitmapImage img = new BitmapImage() { UriSource = new Uri( LOCAL_REPOSITORY.Path + "/Assets/gfx/cards/" + FormatImageNameToFileName(imageName) + ".jpg", UriKind.RelativeOrAbsolute) };

我混合了上面的 2 个示例:我从图片的绝对 URI 创建了我的 bitmapImage(LOCAL_REPOSITORY 包含对本地存储的引用:ApplicationData.Current.LocalFolder)

我仍然不明白为什么其他 2 种方法失败了:我通常将我的图像直接绑定到一个 Uri、一个字符串或一个 BitmapImage,并且它有效..

解决方案

下面的代码展示了如何在应用程序中使用 loadimage 方法:创建一个空白应用程序,向主页添加一个图像和一个按钮.

>

 private async void Button_Click_1(object sender, RoutedEventArgs e){//从文档库加载文件.注意:在功能中选择文档库并声明.png文件类型字符串文件名 = "Logo.png";Windows.Storage.StorageFile sampleFile = await Windows.Storage.KnownFolders.DocumentsLibrary.GetFileAsync(filename);//从本地文件夹加载文件//Windows.Storage.StorageFile sampleFile = sampleFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("Assets\Logo.png");BitmapImage img = new BitmapImage();img = 等待加载图像(样本文件);myImage.Source = img;}私有静态异步任务加载图像(存储文件文件){BitmapImage bitmapImage = new BitmapImage();FileRandomAccessStream stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read);bitmapImage.SetSource(stream);返回位图图像;}

I would like to display the content of a picture stored in a StorageFile through a binding, but whatever I'm trying to do it doesn't seem to work.

Here are the two solutions I have already tested :

string img =( await CompetencesFolder.GetFileAsync(FormatImageNameToFileName(imageName))).Path;

And then returning the path obtained (a full absolute path to the file) to the source Property through binding, and :

 BitmapImage img = await LoadImage(await CompetencesFolder.GetFileAsync(FormatImageNameToFileName(imageName)));

  private static async Task<BitmapImage> LoadImage(StorageFile file)
        {
            BitmapImage bitmapImage = new BitmapImage();
            FileRandomAccessStream stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read);

            bitmapImage.SetSource(stream);


            return bitmapImage;

        }

Returning the final bitmapImage later to my binded property.

None of these methods works ..

Do anyone have an idea?

EDIT : FIX

Here is the code that solved the problem :

BitmapImage img = new BitmapImage() { UriSource = new Uri( LOCAL_REPOSITORY.Path + "/Assets/gfx/cards/" + FormatImageNameToFileName(imageName) + ".jpg", UriKind.RelativeOrAbsolute) };

I did a mix of the 2 samples above : I created my bitmapImage from the absolute URI of the picture (LOCAL_REPOSITORY contains a reference to the local storage : ApplicationData.Current.LocalFolder)

I still can't figure why the 2 other ways failed : I usually bind my image directly to an Uri, a string or a BitmapImage, and it works ..

解决方案

The code below shows how you can use the loadimage method in an app: create a blank app, add an Image and a Button to the main page.

    private async void  Button_Click_1(object sender, RoutedEventArgs e)
    {
        // load file from document library. Note: select document library in capabilities and declare .png file type
        string filename = "Logo.png";
        Windows.Storage.StorageFile sampleFile = await Windows.Storage.KnownFolders.DocumentsLibrary.GetFileAsync(filename);
        // load file from a local folder
        //Windows.Storage.StorageFile sampleFile = sampleFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("Assets\Logo.png");

        BitmapImage img = new BitmapImage();
        img = await LoadImage(sampleFile);
        myImage.Source = img;
    }

    private static async Task<BitmapImage> LoadImage(StorageFile file)
    {
        BitmapImage bitmapImage = new BitmapImage();
        FileRandomAccessStream stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read);

        bitmapImage.SetSource(stream);

        return bitmapImage;

    }

这篇关于在metroapp中显示存储在Storage文件中的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆