C# 是否正在WPF中加载BitmapImage的源?
在silverlight应用程序中,我将BitmapImage定义为C# 是否正在WPF中加载BitmapImage的源?,c#,wpf,bitmapimage,C#,Wpf,Bitmapimage,在silverlight应用程序中,我将BitmapImage定义为System.Windows.Media.Imaging.BitmapImage,并将其作为名为“SetSource”的方法,在该方法中我可以如下设置源代码: BitmapImage bitmap = new BitmapImage(); System.IO.Stream stream = _scene.GetStream(); if (stream == null) return; bitmap.SetSource(strea
System.Windows.Media.Imaging.BitmapImage
,并将其作为名为“SetSource
”的方法,在该方法中我可以如下设置源代码:
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是内部网的标识符吗?你确定这不是silverlight事件吗?我正在执行WPF应用程序您可以使用来指定
或者您可以使用属性设置StreamSource您必须设置属性:
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = _scene.GetStream();
bitmap.EndInit();
如果要在创建位图后立即关闭流,还必须设置BitmapCacheOption.OnLoad
选项:
using (Stream stream = _scene.GetStream())
{
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.EndInit();
}
using (Stream stream = _scene.GetStream())
{
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.EndInit();
}