【发布时间】:2017-04-17 14:53:30
【问题描述】:
我正在尝试通过返回 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();
}
我在BitmapLoader.Current.Load 呼叫中不断点击AggregateException。内部异常是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
AggregateException 的内部NotSupportedException 有这个消息和堆栈跟踪:
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)
【问题讨论】:
-
相关,但不重复。该问题的根本原因是无法下载图像并从无效图像中引发异常。我正在做的事情肯定有可能导致我的
MemoryStream没有有效的图像数据,尽管正如我所提到的,如果我使用FileStream它可以正常工作。如果您能指出我的错误是什么,我将不胜感激。 -
为什么不设置断点,看看哪里出错了。
-
不幸的是,我无法确定异常发生的确切位置。我能想到的最好的结果是它发生在 Bitmap.cs 内的
source.EndInit();,PlatformBitmapLoader.withInit()。我无法从中获得更多信息。我正在使用 Splat 1.6.2 的 nuget 版本,无法对其进行调试。我最好的是拉 Splat 分支,它与原始 dll 不完全相同,所以我的调试选项是有限的。 -
应该和nuget包一样。 github.com/paulcbetts/splat/commit/…
标签: wpf cross-platform reactiveui rendertargetbitmap