奇怪的 WriteableBitmap FromStream 问题

Weird WriteableBitmap FromStream issue

我正在拼命尝试使用 FromStream 扩展方法加载 WriteableBitmap

下面的代码在 FromStream() 调用中抛出一个 System.InvalidOperationException,并包含以下信息:

BitmapImage has not been initialized. Call the BeginInit method, set the appropriate properties, and then call the EndInit method.

我确认图像存在并且它的构建操作设置为 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

什么可能导致此异常?我的申请中没有其他代码运行。

第 367 行 WriteableBitmapConvertExtensions.cs 中的 FromStream 方法似乎已损坏。它缺少对 BitmapImage 的 BeginInitEndInit 调用:

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;
}