在Windows 8 Metro C#中显示StorageFile

时间:2012-09-30 09:09:55

标签: c# windows-8 microsoft-metro

我想在资产管理系统的UI上显示图像文件。我设法将项目存储为StorageFile。我该如何显示它?我试图在XAML <Image>标记的源代码中显示它。是否可以将StorageFile转换为Image

string path = @"Assets\mypicture.png";
StorageFile file = await InstallationFolder.GetFileAsync(path);

3 个答案:

答案 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))