C# 在Metro应用程序中从文件系统设置ImageSource

C# 在Metro应用程序中从文件系统设置ImageSource,c#,windows-8,windows-runtime,winrt-xaml,C#,Windows 8,Windows Runtime,Winrt Xaml,在ApplicationData.Current.RomanigFolder中,我保存了一个jpg文件。是否可以在流或内存流中读取此文件的内容并将其设置为ImageSource 我将其用于.NET4.0的WPF应用程序,代码如下:(img是一个XAML图像控件 BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.CacheOption = BitmapCacheOption.OnLoad; StreamReader sr = new St

在ApplicationData.Current.RomanigFolder中,我保存了一个jpg文件。是否可以在流或内存流中读取此文件的内容并将其设置为ImageSource

我将其用于.NET4.0的WPF应用程序,代码如下:(img是一个XAML图像控件

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
StreamReader sr = new StreamReader(data.message.imageUrl);
bi.StreamSource = sr.BaseStream;
bi.EndInit();
img.Source = bi;
sr.Close();

对于Metro应用程序,我看不到将StreamSource设置为BitmapImage的方法。如何将图像文件设置为图像控件?

对于编写“Metro风格应用程序”或为Windows 8构建的应用程序,可以在WPF项目中设置图像源

代码如下:

// Usage
myImage.Source = ImageFromRelativePath(this, "relative_path_to_file_make_sure_build_set_to_content");

public static BitmapImage ImageFromRelativePath(FrameworkElement parent, string path)
{
    var uri = new Uri(parent.BaseUri, path);
    BitmapImage result = new BitmapImage();
    result.UriSource = uri;
    return result;
}

参考资料:

是否有方法控制文件何时加载到内存中?(如WPF中BitmapCacheOption提供的)