2012-09-30 19 views
0

在ApplicationData.Current.RomanigFolder中,我保存了一个jpg文件。可以在流或MemoryStream中读取此文件的内容并将其设置为ImageSource?在Metro应用程序中从FileSystem设置ImageSource

我用它在WPF应用中使用.NET 4.0 follwing代码:(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的,怎么能我一个图像文件设置为一个图像控制?

回答

1

为文“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; 
} 

参考:http://www.codingbeta.com/programatically-setting-image-source-in-metro-style-apps-with-wpf/

+2

有没有一种方式,当文件被加载到内存控制? (就像BitmapCacheOption在WPF中给我们的) – Tristan

相关问题