[英]Weird WriteableBitmap FromStream issue
我拼命尝试使用FromStream
扩展方法加载WriteableBitmap
。
下面的代码在FromStream()
调用中引发System.InvalidOperationException
并显示以下信息:
BitmapImage尚未初始化。 调用BeginInit方法,设置适当的属性,然后调用EndInit方法。
我验证了该图像是否存在,并将其构建操作设置为Resource
。 图像有效(我将其加载到BitmapImage
),并且流具有正确的长度。
var sri = Application.GetResourceStream(new Uri("dummy.jpg", UriKind.RelativeOrAbsolute));
if (sri != null)
{
using (var stream = sri.Stream)
{
// creates the bitmap, part of the writeablebitmapextensions on WPF
var wb = BitmapFactory.New(1, 1);
wb.FromStream(stream); // <--- Exception
}
}
该错误似乎表明扩展方法在内部以不正确的方式使用了BitmapImage
,这让我很难相信。 我正在使用WriteableBitmapEx.Wpf 1.0.14.0
是什么导致此异常? 我的应用程序中没有其他代码在运行。
WriteableBitmapConvertExtensions.cs中的FromStream方法(行367)似乎已损坏。 它缺少BitmapImage上的BeginInit
和EndInit
调用:
public static WriteableBitmap FromStream(this WriteableBitmap bmp, Stream stream)
{
var bmpi = new BitmapImage();
#if SILVERLIGHT
bmpi.SetSource(stream);
bmpi.CreateOptions = BitmapCreateOptions.None;
#elif WPF
bmpi.StreamSource = stream;
#endif
bmp = new WriteableBitmap(bmpi);
return bmp;
}
它看起来应该像这样:
public static WriteableBitmap FromStream(this WriteableBitmap bmp, Stream stream)
{
var bmpi = new BitmapImage();
#if SILVERLIGHT
bmpi.SetSource(stream);
bmpi.CreateOptions = BitmapCreateOptions.None;
#elif WPF
bmpi.BeginInit();
bmpi.StreamSource = stream;
bmpi.EndInit();
#endif
bmp = new WriteableBitmap(bmpi);
return bmp;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.