[英]Constant garbage collection from converting Bitmap to BitmapImage
[英]Create Bitmap from Collection of BitmapSources
我想從BitmapSource集合創建Bitmap,每個源源應為一幀。
我寫了下面的代碼
MemoryStream memStream = new MemoryStream();
BitmapEncoder enCoder = new GifBitmapEncoder();
foreach (BitmapSource source in BitmapSources)
enCoder.Frames.Add(BitmapFrame.Create(source));
enCoder.Save(memStream);
_Bitmap = new DrawingCtrl.Bitmap(memStream);
DrawingCtrl.ImageAnimator.Animate(_Bitmap, OnFrameChanged);
和
private void OnFrameChangedInMainThread()
{
DrawingCtrl.ImageAnimator.UpdateFrames(_Bitmap);
Source = GetBitmapSource(_Bitmap);
InvalidateVisual();
}
但是它顯示"Exception has been thrown by the target of an invocation."
。 有人可以幫我嗎?
我不知道WPF中的BitmapSources,但我注意到您使用MemoryStream
存在錯誤,因此可能是以下問題:
enCoder.Save(memStream);
這會將內容寫入內存流,並將流指針留在末尾。
_Bitmap = new DrawingCtrl.Bitmap(memStream);
然后,它將嘗試從流的末尾開始,從流中讀取位圖。 當然那行不通。 嘗試在以下之間添加尋道:
enCoder.Save(memStream);
memStream.Seek(0, SeekOrigin.Begin);
_Bitmap = new DrawingCtrl.Bitmap(memStream);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.