I would like to display an image file on the UI from the Assets. I managed to store the item as a StorageFile
. How can I display it? I've tried to display it in a XAML <Image>
tag's Source. Is it possible to covert StorageFile
to Image
?
string path = @"Assets\mypicture.png";
StorageFile file = await InstallationFolder.GetFileAsync(path);
Try this function
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;
}
Try the following:
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;
}
Call the function this way:
Image image = new Image();
image.Source = await GetBitmapAsync (file);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With