4

我想在资产的 UI 上显示一个图像文件。我设法将项目存储为StorageFile. 我怎样才能显示它?我试图在 XAML<Image>标记的源中显示它。可以隐瞒StorageFileImage

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

3 回答 3

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;
}
于 2012-11-27T21:40:43.233 回答
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);
于 2012-09-30T09:19:50.310 回答
1

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

于 2013-03-12T14:07:01.190 回答