我想在资产管理系统的UI上显示图像文件。我设法将项目存储为StorageFile
。我该如何显示它?我试图在XAML <Image>
标记的源代码中显示它。是否可以将StorageFile
转换为Image
?
string path = @"Assets\mypicture.png";
StorageFile file = await InstallationFolder.GetFileAsync(path);
答案 0 :(得分:9)
尝试此功能
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;
}
答案 1 :(得分:2)
尝试以下方法:
public async Task<BitmapImage> GetBitmapAsync(StorageFile storageFile)
{
BitmapImage bitmap = new BitmapImage();
IAsyncOperation<IRandomAccessStream> read = storageFile.OpenReadAsync();
IRandomAccessStream stream = await read;
bitmap.SetSource(stream);
return bitmap;
}
以这种方式调用函数:
Image image = new Image();
image.Source = await GetBitmapAsync (file);
答案 2 :(得分:1)
image.Source = new BitmapImage(new Uri(“file://”+ storageFile.Path))