在Metroapp中显示存储在存储文件中的图片 [英] Displaying a picture stored in Storage file in a metroapp
问题描述
我想通过绑定显示存储在StorageFile中的图片的内容,但是我尝试执行的操作似乎无效.
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;
然后通过绑定将获得的路径(文件的完整绝对路径)返回到源Property,
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;
}
稍后将最终的bitmapImage返回到我的绑定属性.
Returning the final bitmapImage later to my binded property.
这些方法都不起作用..
None of these methods works ..
有人有主意吗?
修复
这是解决问题的代码:
BitmapImage img = new BitmapImage() { UriSource = new Uri( LOCAL_REPOSITORY.Path + "/Assets/gfx/cards/" + FormatImageNameToFileName(imageName) + ".jpg", UriKind.RelativeOrAbsolute) };
我混合了上述两个示例:我从图片的绝对URI创建了bitmapImage(LOCAL_REPOSITORY包含对本地存储的引用:ApplicationData.Current.LocalFolder
)
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
)
我仍然不知道为什么其他两种方式失败了:我通常将我的图像直接绑定到Uri,字符串或BitmapImage,并且它可以正常工作..
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 ..
推荐答案
下面的代码显示了如何在应用程序中使用loadimage方法:创建空白应用程序,在主页面上添加图像和按钮. >
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中显示存储在存储文件中的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!