我試圖從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