views:

172

answers:

4

Hi, Were is the problem in code.. i am trying to read the .gif file and writing to the another .gif, if i do so... the newly cretaed .gif file will not show correct image insted junk image comes were is the mistake in code.

 private void ReadFile()
    {
        StreamReader MyReader = new StreamReader(@"C:\\Users\\admin\\Desktop\\apache_pb22_ani.gif");
        string ReadFile= MyReader.ReadToEnd();
        MyReader .Close ();

        StreamWriter MYWriter = new StreamWriter(@"C:\\Hi.gif");
        MYWriter.Write(ReadFile);
        MYWriter.Close();

        //throw new NotImplementedException();
    }

if i read image from server and if i write to the image file also same problem appears, what is the problem... code of reading image from server and writing is here

StringBuilder sb = new StringBuilder();
        // used on each read operation
        byte[] buf = new byte[8192];
        // prepare the web page we will be asking for
        HttpWebRequest request = (HttpWebRequest)
                WebRequest.Create("http://10.10.21.178/Untitled.jpg");
        // execute the request
        HttpWebResponse response = (HttpWebResponse)
                request.GetResponse();
        // we will read data via the response stream
        Stream resStream = response.GetResponseStream();

        string tempString = null;
        int count = 0;

        StreamWriter FileWriter = new StreamWriter("C:\\Testing.jpg");


        do
        {
            // fill the buffer with data
            count = resStream.Read(buf, 0, buf.Length);
            // make sure we read some data
            if (count != 0)
            {
                // translate from bytes to ASCII text. 
                // Not needed if you'll get binary content.

                tempString = Encoding.ASCII.GetString(buf, 0, count);
                FileWriter.Write(tempString);
                // continue building the string
                sb.Append(tempString);
            }
        }
        while (count > 0); // any more data to read?

        FileWriter.Close();

        // print out page source
       // Console.WriteLine(sb.ToString());
        //throw new NotImplementedException();
    }
+2  A: 

You can't read a GIF file (binary) into a string variable.

You need to read into an array of bytes.

richardtallent
+1  A: 

You don't want to use ReadToEnd(), that is for text files. Try File.ReadAllBytes(), it will read a binary file into a byte array. Then you can write that file back to disk with File.WriteAllBytes().

Matt Greer
+4  A: 

Binary data (such as images) doesn't work in a .NET string; you want something like (assuming File.Copy isn't an option):

using(Stream source = File.OpenRead(fromPath))
using(Stream dest = File.Create(toPath)) {
    byte[] buffer = new byte[1024];
    int bytes;
    while((bytes = source.Read(buffer, 0, buffer.Length)) > 0) {
        dest.Write(buffer, 0, bytes);
    }
}

This treats the image as binary (byte[]), and uses a buffer/loop to avoid blowing up if you have a big image (when File.ReadAllBytes could be expensive).

Marc Gravell
Thanks.. it worked :-)
Shadow
A: 

string uses UTF16. Am I right?

This means your code converts ASCII to UTF16. :)

Also you do not understand the meaning of the @ sign. Place it in front of strings if you want to avoid double backslashes. You code @"C:\\Hi.gif" should be either "C:\\Hi.gif" or @"C:\Hi.gif".

Vasiliy Borovyak