下载动画GIF [英] Download animated GIF
问题描述
我正在使用以下内容从网络上获取动画gif.但是,将其保存到磁盘后,我丢失了gif中的帧,并且不再进行动画处理.不确定问题是否出在下面的方法中,或者是在我保存时,但是对于以下原因无法解决的任何反馈,我们深表感谢.该方法有效-只是没有创建适当的动画gif文件.
I'm using the below to grab an animated gif off the web. However, upon saving this to disk, I lose the frames in the gif, and it no longer animates. Not sure whether the problem is in the below method, or whether it's when I'm saving it, but any feedback on why the below wouldn't work appreciated. The method is working - just not creating a proper animated gif file.
public Image getImage(String url)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = httpWebReponse.GetResponseStream();
return Image.FromStream(stream, true, true);
}
Image im = getImage(url)
im.Save(pth,ImageFormat.Gif);
推荐答案
您不应从流中创建 Image
( Stream.CopyTo
方法,将内容复制到为目标文件创建的 FileStream
.
You should not create an Image
from the stream (it will only store the first frame). Instead, you should write the contents of the Stream
directly to disk using, for example, the Stream.CopyTo
method to copy the content to a FileStream
you created for the destination file.
using (Stream stream = httpWebReponse.GetResponseStream())
using (FileStream fs = File.Create(path))
{
stream.CopyTo(fs);
}
这篇关于下载动画GIF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!