Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a StorageFile in Windows 8 Metro C#

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);
like image 458
Lgn Avatar asked Dec 16 '22 18:12

Lgn


2 Answers

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;
}
like image 152
softarn Avatar answered Mar 01 '23 04:03

softarn


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);
like image 30
Legoless Avatar answered Mar 01 '23 03:03

Legoless