0

我正在尝试通过返回 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();
}

我一直AggregateExceptionBitmapLoader.Current.Load打电话。内部异常是一条NotSupportedException消息,No imaging component suitable to complete this operation was found.我无法找到任何相关文档来解释这意味着什么或我做错了什么。作为测试,我尝试将位图编码器保存到 a FileStream,效果很好。

编辑

有人指出,本期可能与本期重复。它们当然是相关的,但在这种情况下,我想知道为什么保存到MemoryStream失败的地方无法创建有效的图像数据FileStream

编辑 2

根据要求,以下是我在输出中看到的异常:

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

的内部NotSupportedExceptionAggregateException这个消息和堆栈跟踪:

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)
4

1 回答 1

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;
}
于 2017-04-29T13:03:41.633 回答