Display a StorageFile in Windows 8 Metro C#

允我心安 提交于 2019-12-05 02:20:36

问题


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);

回答1:


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;
}



回答2:


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);



回答3:


image.Source = new BitmapImage(new Uri("file://"+ storageFile.Path))



来源:https://stackoverflow.com/questions/12660077/display-a-storagefile-in-windows-8-metro-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!