作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 silverlight 应用程序中,我有一个定义为 System.Windows.Media.Imaging.BitmapImage
的 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 应用程序
最佳答案
您必须设置 BitmapImage.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();
}
关于c# - 在 WPF 中加载 BitmapImage 的源代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11771223/
我是一名优秀的程序员,十分优秀!