views:

22

answers:

1

Hello

I would like to create the Bitmap from BitmapSource Collection and Each source source should be one frame.

I wrote the following code

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

and

private void OnFrameChangedInMainThread()
{
    DrawingCtrl.ImageAnimator.UpdateFrames(_Bitmap);
    Source = GetBitmapSource(_Bitmap);
    InvalidateVisual();
}

But it shows "Exception has been thrown by the target of an invocation.". Could anyone help me?

A: 

I don’t know about BitmapSources in WPF, but I notice an error in how you use MemoryStream, so maybe this is the issue:

enCoder.Save(memStream);

This will write the content to the memory stream and leave the stream pointer at the end.

_Bitmap = new DrawingCtrl.Bitmap(memStream);

This will then try to read the bitmap from the stream, starting at the end of the stream. Of course that can’t work. Try adding a seek in between:

enCoder.Save(memStream);
memStream.Seek(0, SeekOrigin.Begin);
_Bitmap = new DrawingCtrl.Bitmap(memStream);
Timwi
I tried your answer.But after creating bitmap again it shows only one frame. but i need the collection of frames.