1

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?

2
  • 1
    You're printing binary information as text. Jul 13, 2012 at 16:54
  • 1
    you have to receive it as binary. this is just a text representation of the binary data and you can not make gif out of it..
    – user915331
    Jul 13, 2012 at 16:54

4 Answers 4

8

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.

4

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

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.