首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从Splat.IBitmap加载MemoryStream

无法从Splat.IBitmap加载MemoryStream
EN

Stack Overflow用户
提问于 2017-04-17 14:53:30
回答 1查看 602关注 0票数 0

我试图通过返回一个Splat.IBitmap将布局数据呈现到与平台无关的位图中,并且输出结果有困难。我的WPF平台代码如下所示:

代码语言:javascript
复制
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 call.The上点击一个NotSupportedException,内部例外是一个带有消息No imaging component suitable to complete this operation was found.NotSupportedException,我没有找到任何相关的文档来解释这意味着什么或者我做错了什么。作为一种测试,我尝试将位图编码器保存到一个FileStream中,它工作得很好。

编辑

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

编辑2

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

代码语言:javascript
复制
Exception thrown: 'System.NotSupportedException' in PresentationCore.dll
Exception thrown: 'System.NotSupportedException' in PresentationCore.dll
Exception thrown: 'System.AggregateException' in mscorlib.dll

NotSupportedException的内部AggregateException具有以下消息和堆栈跟踪:

代码语言:javascript
复制
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只具有此消息,但没有堆栈跟踪:

代码语言:javascript
复制
The component cannot be found. (Exception from HRESULT: 0x88982F50)
EN

回答 1

Stack Overflow用户

发布于 2017-04-29 13:03:41

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

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43453837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档