无法从MemoryStream加载Splat.IBitmap

时间:2017-04-17 14:53:30

标签: wpf cross-platform reactiveui rendertargetbitmap

我试图通过返回Splat.IBitmap以平台无关的方式将布局数据渲染到位图,并且无法输出结果。我的WPF平台代码如下所示:

MemoryStream stream = new MemoryStream();
try
{
    RenderTargetBitmap target = new RenderTargetBitmap(1024, 1024, 300, 300, PixelFormats.Default);
    PreviewView preview = new PreviewView();  // this does sizing and placement

    target.Render(preview);

    previewViewModel.Dispose();
    BitmapEncoder encoder = new BmpBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(target));
    encoder.Save(stream);
    stream.Flush();

    IBitmap bitmap = BitmapLoader.Current.Load(stream, 1024, 1024).Result;
    stream.Dispose();

    return bitmap;
}
catch (Exception e)
{
    // TODO: log error
    return null;
}
finally
{
    stream.Dispose();
}

我一直在AggregateException电话上点击BitmapLoader.Current.Load。内部异常为NotSupportedException,邮件为No imaging component suitable to complete this operation was found.我无法找到任何相关文档解释这意味着什么或我做错了什么。作为测试,我尝试将位图编码器保存到FileStream,这很好。

修改

有人指出,这个问题可能与this issue重复。它们肯定是相关的,但在这种情况下,我想知道为什么保存到MemoryStream无法创建FileStream工作的有效图像数据。

编辑2

根据要求,以下是我在输出中看到的例外情况:

Exception thrown: 'System.NotSupportedException' in PresentationCore.dll
Exception thrown: 'System.NotSupportedException' in PresentationCore.dll
Exception thrown: 'System.AggregateException' in mscorlib.dll

NotSupportedException的内部AggregateException有此消息和stacktrace:

No imaging component suitable to complete this operation was found.
at System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle)
at System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache)
at System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()
at System.Windows.Media.Imaging.BitmapImage.EndInit()
at Splat.PlatformBitmapLoader.withInit(BitmapImage source, Action`1 block) in y:\\code\\paulcbetts\\splat\\Splat\\Xaml\\Bitmaps.cs:line 80
at Splat.PlatformBitmapLoader.<>c__DisplayClass2.<Load>b__0() in y:\\code\\paulcbetts\\splat\\Splat\\Xaml\\Bitmaps.cs:line 25
at System.Threading.Tasks.Task`1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()

虽然 异常内部NotSupportedException只有此消息,并且没有堆栈跟踪:

The component cannot be found. (Exception from HRESULT: 0x88982F50)

1 个答案:

答案 0 :(得分:0)

这不能回答我原来的问题,所以我不会接受这个作为正确的答案,但我找到了一种更正确的方法来处理我想要的东西。我正在写一个MemoryStream,因为我认为这是必要的,但我只需要在WPF中获取一个布局视图并获得一个我可以在其他地方使用的IBitmap。事实证明,实现这一目标比我做出更容易:

try
{
    RenderTargetBitmap target = new RenderTargetBitmap(1024, 1024, 300, 300,
        PixelFormats.Default);
    PreviewView preview = new PreviewView();  // this does sizing and placement

    target.Render(preview);

    // HERE IS AN IMPORTANT BIT. This allows me to access the RenderTargetBitmap from 
    // another thread, for example if calling IBitmap.Save() which happens on another
    // in a separate Task
    target.Freeze();

    previewViewModel.Dispose();

    // HERE IS ANOTHER IMPORTANT BIT. There's no need to capture a frame and save to a 
    // Stream through an Encoder. Splat lets you create bitmaps from a native
    // implementation
    IBitmap bitmap = target.FromNative();

    return bitmap;
}
catch (Exception e)
{
    // TODO: log error
    return null;
}