我试图从FilePicker
中选择一个StorageFile
后显示图像。由于Image
的Source
必须是URI
或ImageSource
,因此我试图从StorageFile
中获取其中的任一个。WinRT将图像绑定到字符串或StorageFile
我很难获取数据绑定在XAML中的Image
上工作。我曾尝试以下:
<Image Width="300"
Height="300"
x:Name="LogoImage">
<Image.Source>
<BitmapImage UriSource="{Binding ImagePath}" />
</Image.Source>
</Image>
这样是不行的,作为StorageFile
的Path
属性不是URI
。另外,我不能直接绑定到StorageFile
本身,因为它不是ImageSource
。
我试图用这个方法:
public async Task<Image> GetImageAsync(StorageFile storageFile)
{
BitmapImage bitmapImage = new BitmapImage();
FileRandomAccessStream stream = (FileRandomAccessStream)await storageFile.OpenAsync(FileAccessMode.Read);
bitmapImage.SetSource(stream);
Image image = new Image();
image.Source = bitmapImage;
return image;
}
但是,它返回一个Task<Image>
,这也不是一个ImageSource
或URI
。它似乎应该比我想要做的更直接,但我只是没有看到它。另外,我试过在XAML中为Image.Source
指定一个文件,它工作正常。我只是无法根据FilePicker
中选定的文件进行链接。
我的最终目标是:从FilePicker
中选择一个文件,更新显示的Image
的ImageSource
,编码为base64以存储在数据库中。然后,从数据库加载现有的base64字符串,转换回Image
进行显示。
编辑:
我能够完成我使用下面张贴的解决了这个任务。非常感谢杰里·尼克松的博客文章:http://blog.jerrynixon.com/2014/11/reading-and-writing-base64-in-windows.html
这对内存和CPU使用来说都很糟糕... – ManIkWeet 2016-09-17 22:46:22
我同意。我肯定会赞赏一些更有效的方式来实现这一目标的反馈。 – 2016-09-19 01:13:35
我还没有找到答案,可悲的是... – ManIkWeet 2016-09-19 06:36:49