2017-04-17 30 views
0

我想通過返回Splat.IBitmap將佈局數據呈現爲與平臺無關的方式的位圖,並且無法輸出結果。我的WPF平臺的代碼如下所示:無法從MemoryStream中加載Splat.IBitmap

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內部異常上打一個AggregateException是與消息No imaging component suitable to complete this operation was found.一個NotSupportedException我一直沒能找到任何相關的文件來解釋這意味着什麼。或者我做錯了什麼。作爲一個測試,我已經嘗試將位圖編碼器保存到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) 
+0

的可能的複製[「適合來完成這個操作沒有成像部件被發現」(http://stackoverflow.com/questions/7292764/no-imaging-component-suitable-to-complete-this-operation-was-found) – bradgonesurfing

+0

相關,但不是重複。該問題的根本原因是無法下載圖像並從無效圖像中拋出異常。 這是肯定可能的,我正在做的事情導致我的'MemoryStream'沒有有效的圖像數據,但正如我所說的,它工作正常,如果我使用'FileStream'。如果你能指出我的錯誤,我將不勝感激。 –

+0

爲什麼不設置斷點並查看引發錯誤的位置。 – bradgonesurfing

回答

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; 
}