我正在尝试通过返回 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.
我无法找到任何相关文档来解释这意味着什么或我做错了什么。作为测试,我尝试将位图编码器保存到 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
的内部NotSupportedException
有AggregateException
这个消息和堆栈跟踪:
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)