C # gif Image to MemoryStream and vice versa (lose animation)

I have a little problem and I have not found any solutions. I want to convert GIF to byte [] and then return to GIF. I work great, but I'm losing animation.

This is a great animated GIF when I start (I display it in the PictureBox ). But after the conversion, I get stuck in the first frame.

 HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("creativetechs.com/i/tip_images/ExampleAnimation.gif"); HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse(); Stream stream = httpWebReponse.GetResponseStream(); Image img = Image.FromStream(stream); MemoryStream ms = new MemoryStream(); img.Save(ms,img.RawFormat); byte [] bytes = ms.ToArray(); Image img2 = Image.FromStream(new MemoryStream(bytes)); int frames1 = img.GetFrameCount(System.Drawing.Imaging.FrameDimension.Time); int frames2 = img2.GetFrameCount(System.Drawing.Imaging.FrameDimension.Time); 

I also tried using RawFormat, but System.Drawing.Imaging.ImageFormat.Gif . Nothing has changed. frames1 - the correct number of frames. frames2 is 1.

I have 2 PictureBox elements in my GUI. One shows img and the other img2 . But img2 not animated, but img is. What's wrong?

I also tried using serialization to create my byte [].

I serialized the image and deserialized it again, and it didn't change anything. How is this possible?

+13
c # stream animation gif
Jan 06 2018-12-12T00:
source share
2 answers

When downloading images from Stream the .NET platform detects that the GIF is animated. Since he knows that he will not be able to transcode the animated GIF, he tries to preserve the original GIF encoding. But this happens after he read the stream and decrypted the GIF. Therefore, when he tries to rewind the thread, it fails, and he does not save the original.

When you call Save() , it first checks to see if the source encoding is stored. But since this operation failed, she tries to reinstall the GIF. Since .NET does not have an encoder for animated GIFs, it encodes only the first frame.

If you use FileStream , it works, since a FileStream is searchable.

You can get your code to work by first loading the response into a MemoryStream:

 // ... Stream stream = httpWebReponse.GetResponseStream(); MemoryStream memoryStream = new MemoryStream(); stream.CopyTo(memoryStream); memoryStream.Position = 0; stream = memoryStream; Image img = Image.FromStream(stream); // ... 

If you want to know what happens, turn on the .NET source code debugging feature and pay attention to what happens in Image.EnsureSave (). You will also notice that the Image implementation already copies Stream to a MemoryStream, so that they can fix the problem by using this instead of the original stream.

+9
Jan 17 '12 at 20:00
source share

GDI + does not contain an animated GIF encoder (decoder only). This way your img.Save will reset the animation. But you can try the following: http://www.codeproject.com/KB/GDI-plus/NGif.aspx

+2
Jan 06 2018-12-12T00:
source share



All Articles