C#GIF图像到的MemoryStream和背面(失去动画) [英] C# gif Image to MemoryStream and back (lose animation)

查看:132
本文介绍了C#GIF图像到的MemoryStream和背面(失去动画)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,我没有找到任何解决方案。
我想一个GIF转换为一个byte [],然后回一个GIF。我工作正常,但我失去了动画。

I have a small problem and I do not find any solutions. I want to convert a GIF to a byte[] and then back to a GIF. I works fine but I lose the animation.

这是当我开始一个完美的动画GIF(我展示它在图片框元素)。但是转换后我卡住的第一帧。

It is a perfectly animated GIF when I start (I show it in a PictureBox element). But after conversion I get stuck with 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);

我还试图用不RawFormat但 System.Drawing.Imaging.ImageFormat.Gif 。没有改变任何事情。 frames1 是帧的正确的号码。 frames2 1

I also tried to use not RawFormat but System.Drawing.Imaging.ImageFormat.Gif. Didn't change a thing. frames1 is right number of frames. frames2 is 1.

我在我的GUI 2 图片框元素。一是展示 IMG 和其他 IMG2 。但 IMG2 不显示动画,而 IMG 是。什么是错的?

I have 2 PictureBox elements in my GUI. One showing img and the other img2. But img2 is not animated while img is. What is wrong?

我也试过用序列化,而不是创建我的byte []。

I have also tried to use serialization instead to create my byte[].

我序列化的形象,再次反序列化它,它没有任何改变任何东西。这怎么可能?

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

推荐答案

当您从加载图像,在.NET框架检测到GIF是动画。因为它知道,这将是无法颖code动画GIF,它试图保存GIF的原始编码。但这种情况发生的之后的它读取流和德codeD的GIF。因此,当它尝试倒带失败的流和它结束了没有存储原始。

When you load your image from a Stream, the .NET framework detects that the GIF is animated. Since it knows that it will be unable to reencode an animated GIF, it tries to store the original encoding of the GIF. But this happens after it has read the stream and decoded the GIF. So when it tries to rewind the stream this fails and it ends up not storing the original.

当你调用保存()它首先检查它是否具有存储原始编码。但由于该操作失败,它会尝试颖code中的GIF。由于.NET没有为GIF动画的连接codeR,它只是连接codeS的第一帧。

When you call Save() it first checks whether it has the original encoding stored. But since that operation failed, it attempts to reencode the GIF. Since .NET does not have an encoder for animated GIFs, it only encodes the first frame.

如果您使用的是的FileStream ,而不是它的工作原理,因为的FileStream 是可搜索。

If you use a FileStream instead it works, since a FileStream is seekable.

您可以通过先加载的响应到MemoryStream让你的code的工作:

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

// ...
Stream stream = httpWebReponse.GetResponseStream();

MemoryStream memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
memoryStream.Position = 0;
stream = memoryStream;

Image img = Image.FromStream(stream);
// ...

如果你想看看发生了什么,使.NET参考源代码调试,并注意Image.EnsureSave会发生什么()。您还会注意到,图像的实现已经复制流到MemoryStream,使他们能够通过修复问题,而不是原来的流。

If you want to see what happens, enable .NET reference source debugging and note what happens in Image.EnsureSave(). You will also note that the Image-implementation already copies the Stream into a MemoryStream, so that they could fix the problem by using that instead of the original Stream.

这篇关于C#GIF图像到的MemoryStream和背面(失去动画)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆