簡體   English   中英

從BitmapSources的集合創建位圖

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM