代码之家  ›  专栏  ›  技术社区  ›  Naruto

使用StreamReader复制图像时损坏

  •  1
  • Naruto  · 技术社区  · 16 年前

    是代码中的问题。。 我正在尝试读取.gif文件并写入另一个.gif文件,如果我这样做。。。新的cretaed.gif文件将不会显示正确的图像而不是垃圾图像,因为这是代码中的错误。

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

    从服务器读取图像并写入的代码在这里

    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();
        }
    
    4 回复  |  直到 16 年前
        1
  •  7
  •   Marc Gravell    16 年前

    二进制数据(如图像)在.NET字符串中不起作用;你想要(假设)这样的东西吗 File.Copy 这不是一个选项):

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

    这会将图像视为二进制图像( byte[] ),并使用缓冲区/循环以避免在图像较大时爆炸(当 File.ReadAllBytes 可能会很贵)。

        2
  •  2
  •   richardtallent    16 年前

    无法将GIF文件(二进制)读入字符串变量。

    您需要读入字节数组。

        3
  •  2
  •   Matt Greer    16 年前

    您不想使用ReadToEnd(),即文本文件。尝试File.ReadAllBytes(),它将二进制文件读入字节数组。然后可以使用file.writealBytes()将该文件写回磁盘。

        4
  •  0
  •   Vasyl Boroviak    16 年前

    string

    这意味着您的代码将ASCII转换为UTF16:)

    你也不明白这句话的意思 @ 签名如果你想的话,把它放在琴弦前面 双反斜杠。你的代码 @"C:\\Hi.gif" 应该是 "C:\\Hi.gif" @"C:\Hi.gif" .

    推荐文章