2012-08-02 30 views
1

在Silverlight应用程序,我有一个的BitmapImage定义为System.Windows.Media.Imaging.BitmapImage和它作为一个调用的方法“SetSource”我在哪里可以设置这样的源:加载WPF中的BitmapImage的来源?

BitmapImage bitmap = new BitmapImage(); 
System.IO.Stream stream = _scene.GetStream(); 
if (stream == null) return; 
bitmap.SetSource(stream); 

在WPF应用程序我也有一个 位图图像定义为System.Windows.Media.Imaging.BitmapImage,但没有SetSource方法。我如何在Silverlight应用程序中设置WPF应用程序中的源代码?

此外,它是一个流,而不是一个字符串。它不是一个URI。所以“UriSource”方法不起作用。我试过这个:

 System.IO.Stream stream = _scene.GetStream(); 
     if (stream == null) return; 
     BitmapImage bitmap = new BitmapImage(); 

     bitmap.UriSource = new Uri(stream.ToString()); 

而在运行时,它抛出一个错误,无法确定URI。该URI是Intranet的标识符吗?你确定这不是银光的事吗?我做一个WPF应用程序

回答

8

你必须设置BitmapImage.StreamSource属性:

BitmapImage bitmap = new BitmapImage(); 
bitmap.BeginInit(); 
bitmap.StreamSource = _scene.GetStream(); 
bitmap.EndInit(); 

如果您想要在创建位图后立即关闭流,您也可以h可以设置BitmapCacheOption.OnLoad选项:

using (Stream stream = _scene.GetStream()) 
{ 
    bitmap.BeginInit(); 
    bitmap.CacheOption = BitmapCacheOption.OnLoad; 
    bitmap.StreamSource = stream; 
    bitmap.EndInit(); 
}