Download animated GIF

橙三吉。 提交于 2021-02-07 14:52:17

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/15712872/download-animated-gif

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!