Saving a GIF from a URL

I want to save a GIF driven from an internet address. When I request the file from the browser, I receive these characters:

GIF87a½€ÿÿÿ,½þ„©Ëí£œ´Ú‹³Þ¼û†âH–æ‰Ádk
ÇòòµuÏú>æùôãEX¯–Š¨²µ‚‰"<}O)Ò¨¬:kÜ'Õª]þ¢`ìc
­Ú”ìvöhe;Ï]¸;oßÂyþ|Þ‡µe§'hÄÔdhW÷§H#&gö¨ð…G™Gy(‰ x÷j†øyÙˆI8uJZZiª
™ªÚº¹i  {ç‰Zj™ˆwšµ7
©;é›9Æy¤VÄ$ú;œË›ÙÛI¬,œ=}|,l¼Û˧¸ìæ%]h~n»8»ÌíIv—Fe6¿‹¸®0YÉ
J7k8”•pÕá C¬4Èráâ;zü2¤È‘$Kš

Can I retrieve GIF format with these characters?

Answers


A GIF is a binary file. You shouldn't be reading it as a string. Read the GIF into a byte[], and then save those bytes to a file.


Just do this:

string url = "http://address.com/getFile?s=1234";
string filePath = @"c:\MyFolder\file.gif";

using (WebClient client = new WebClient())
{
     client.DownloadFile(url, filePath);
}

and do not forget to add this at the beginning:

using System.Net;

thanks for your answers. I've tried a way and I found the Issue

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Accept = "text/html, text/plain";
request.AllowAutoRedirect = true;
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
Bitmap bmp = new Bitmap(Image.FromStream(stream, true, false));
bmp.Save("image.gif");

Judging from what I know about file formats and the web, the characters are representations ASCII or another encoding of binary. You may still be able to save it using the BinaryWriter.Write() Method.


Need Your Help

How to dynamically choose and connect to Oracle through C# (Dev Vs Test Vs Production Server)

c# oracle connection-string

Our company is looking to change from a legacy system to rebuilding everything in C#. Over the years we changed from our original database to having an Oracle backend. And now we have a 3 layers

How to horizontally flip an image

javascript html canvas

How can i draw only two images with reversing I don't know how to reverse. Pls help.