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.
Rasmus Faber Jan 17 '12 at 20:00 2012-01-17 20:00
source share