Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download animated 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);
like image 351
Squiggs. Avatar asked Mar 29 '13 23:03

Squiggs.


People also ask

How do I download an animated GIF?

Search for a GIF on Google Images via your mobile browser. Once found, tap on the GIF to open it as much as possible (so you are not viewing it in search results). Hold your finger down on the GIF until a menu pops up. From the menu, click on “Save Image.”

How do I download a GIF from Google?

Download and install the GIPHY app from the Google Play Store. Use the search bar at the top of the screen to look for a GIF image. Out of all the relevant results, tap on the one you'd like to download. Press and hold on the GIF image and press Yes to save the image to your device.


1 Answers

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);
}
like image 88
Daniel A.A. Pelsmaeker Avatar answered Sep 30 '22 09:09

Daniel A.A. Pelsmaeker