how to save image file from stream?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wastman
    New Member
    • Apr 2009
    • 17

    how to save image file from stream?

    Code:
                   
    Stream st;
    FileStream f = new FileStream(@"D:\samp\rt.jpg", FileMode.Create);
    byte[] b = new byte[10000];
                    
    st.Read(b, 0, b.Length);
                    
    st.Close();
    f.Write(b, 0, b.Length);
    f.Flush();
    f.Close();
    i doesn't show error.
    but it gets noting stored.why?
    but other files would be saved.but incase of image file,its happening thus.
  • wastman
    New Member
    • Apr 2009
    • 17

    #2
    how to save image file from stream?

    Code:
    Stream st=fromsomesource;
     FileStream f = new FileStream(@"D:\samp\rt.jpg", FileMode.Create);
    byte[] b = new byte[10000];
    st.Read(b, 0, b.Length);  
    st.Close();
    f.Write(b, 0, b.Length);
    f.Flush();
    f.Close();
    i doesn't show error.
    but it gets noting stored.why?
    but other files would be saved.but incase of image file,its happening thus.
    Last edited by Frinavale; May 1 '09, 01:53 PM.

    Comment

    • PRR
      Recognized Expert Contributor
      • Dec 2007
      • 750

      #3
      FileStream Read
      Code:
      //Read image file 
      byte[] b = null;
                  using (FileStream f = new FileStream(@"C:\Bernese Oberland.jpg", FileMode.Open))
                  {
                      b = new byte[f.Length];
      
                      f.Read(b, 0, b.Length);
                  }
      
      // Write to file ...
      using (FileStream fs = new FileStream(@"C:\Bernese Oberland1.jpg", FileMode.Create))
                  {
                      fs.Write(b, 0, b.Length);
                      
                  }

      Comment

      • wastman
        New Member
        • Apr 2009
        • 17

        #4
        No. stream is coming from webresponse.get Stream() method.
        so i need a solution for that one.

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Well, there's probably an easier way, but if you wanted you could use the ReadByte method of the stream and add each byte you read to a System.Collecti osn.Generic.Lis t<byte> until the byte you read is -1 (end of stream) and then convert the List into a byte[].

          Lots of ways to do it though.

          Comment

          • wastman
            New Member
            • Apr 2009
            • 17

            #6
            Originally posted by insertAlias
            Well, there's probably an easier way, but if you wanted you could use the ReadByte method of the stream and add each byte you read to a System.Collecti osn.Generic.Lis t<byte> until the byte you read is -1 (end of stream) and then convert the List into a byte[].

            Lots of ways to do it though.
            but image does not get stored.should i follow something like sequential access?

            Comment

            • PRR
              Recognized Expert Contributor
              • Dec 2007
              • 750

              #7
              Could you post more sample code? and maybe flow of your program...

              Comment

              • wastman
                New Member
                • Apr 2009
                • 17

                #8
                Originally posted by DeepBlue
                Could you post more sample code? and maybe flow of your program...
                Code:
                        ArrayList addrs = new ArrayList();
                        //addrs.Add("http://iis02.northcomp.com");
                        //addrs.Add("http://iis03.northcomp.com");
                        //addrs.Add("http://ncs01.northcomp.com");
                        addrs.Add("http://c4.mobimgs.com/swarea/assets/ad56e59c/164446_scr.gif");
                        foreach (string s in addrs)
                        {
                            try
                            {
                                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(s);
                                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                                //HttpResponse rd = new HttpResponse();
                                
                                Stream st = resp.GetResponseStream();
                                FileStream f = new FileStream(@"f:\pic\samp\rt.jpg", FileMode.Create);
                                byte[] b = new byte[10000];
                                
                                st.Read(b, 0, b.Length);
                                
                                st.Close();
                                f.Write(b, 0, b.Length);
                                f.Flush();
                                f.Close();
                                //StreamReader sr = new StreamReader(st);
                
                                //string buffer = sr.ReadToEnd();
                                //int startPos, endPos;
                                //startPos = buffer.IndexOf("&lt;title>",
                                //   StringComparison.CurrentCultureIgnoreCase) + 7;
                                //endPos = buffer.IndexOf("&lt;/title>",
                                //   StringComparison.CurrentCultureIgnoreCase);
                                //string title = buffer.Substring(startPos, endPos - startPos);
                
                                //Console.WriteLine("Response code from {0}: {1}", s,
                                //                  resp.StatusCode);
                                //Console.WriteLine("Page title: {0}", title);
                                //sr.Close();
                                //st.Close();
                            }
                            catch (Exception ex)
                            {
                                System.Windows.Forms.MessageBox.Show("Error connecting to " + s + "\nException:" + ex.ToString());
                            }
                        }
                        System.Windows.Forms.MessageBox.Show("Web site check completed");

                Comment

                • wastman
                  New Member
                  • Apr 2009
                  • 17

                  #9
                  Hi all experts! can any body answer to my post?

                  Give me a solution,i don't want to waste my time though my name is wastman.

                  Comment

                  • wastman
                    New Member
                    • Apr 2009
                    • 17

                    #10
                    ok. i simplify my code.

                    Code:
                    string s="http://c4.mobimgs.com/swarea/assets/ad56e59c/164446_scr.gif";
                            try
                                {
                                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(s);
                                    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                                    Stream st = resp.GetResponseStream();
                                    FileStream f = new FileStream(@"f:\pic\samp\rt.jpg", FileMode.Create);
                    
                                    byte[] b = new byte[10000];
                                    st.Read(b, 0, b.Length);
                                    st.Close();
                    
                                    f.Write(b, 0, b.Length);
                                    f.Flush();
                                    f.Close();
                                }
                                catch (Exception ex)
                                {
                                    System.Windows.Forms.MessageBox.Show("Error connecting to " + s + "\nException:" + ex.ToString());
                                }
                     
                            System.Windows.Forms.MessageBox.Show("Web site check completed");
                    Please get me out of a minor mistake which might be.

                    Comment

                    • PRR
                      Recognized Expert Contributor
                      • Dec 2007
                      • 750

                      #11
                      Try this
                      Code:
                      public static void Image()
                              {
                                  ArrayList addrs = new ArrayList();
                      
                      
                                  //addrs.Add("http://iis02.northcomp.com");
                                  //addrs.Add("http://iis03.northcomp.com");
                                  //addrs.Add("http://ncs01.northcomp.com");
                                  addrs.Add("http://images.devshed.com/af/stories/C_Sharp_Filestream/4.jpg");//http://c4.mobimgs.com/swarea/assets/ad56e59c/164446_scr.gif");
                                  foreach (string s in addrs)
                                  {
                                      try
                                      {                    
                                          HttpWebRequest req = (HttpWebRequest)WebRequest.Create(s);
                                         //req.ContentType = "image/gif";
                                          
                      
                                          HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                                          long l = resp.ContentLength;
                                          byte[] b = new byte[l]; 
                                          
                      
                                          using (Stream st = resp.GetResponseStream())
                                          {                     
                                                  using (FileStream f = new FileStream(@"C:\one.jpg", FileMode.OpenOrCreate))
                                                  {
                                                      //long len = resp.ContentLength;
                                                                                    
                      
                                                      st.Read(b, 0, b.Length);
                      
                                                      st.Close();
                                                      f.Write(b, 0, b.Length);
                                                      f.Flush();
                                                      f.Close();
                                                  }
                                              
                                          }
                                          //StreamReader sr = new StreamReader(st);
                      
                                          //string buffer = sr.ReadToEnd();
                                          //int startPos, endPos;
                                          //startPos = buffer.IndexOf("&lt;title>",
                                          //   StringComparison.CurrentCultureIgnoreCase) + 7;
                                          //endPos = buffer.IndexOf("&lt;/title>",
                                          //   StringComparison.CurrentCultureIgnoreCase);
                                          //string title = buffer.Substring(startPos, endPos - startPos);
                      
                                          //Console.WriteLine("Response code from {0}: {1}", s,
                                          //                  resp.StatusCode);
                                          //Console.WriteLine("Page title: {0}", title);
                                          //sr.Close();
                                          //st.Close();
                                      }
                                      catch (Exception ex)
                                      {
                                          System.Windows.Forms.MessageBox.Show("Error connecting to " + s + "\nException:" + ex.ToString());
                                      }
                                  }
                                  System.Windows.Forms.MessageBox.Show("Web site check completed");
                      
                      
                              }
                              #endregion

                      Comment

                      • wastman
                        New Member
                        • Apr 2009
                        • 17

                        #12
                        No.
                        The Actual Image:


                        but the output image:


                        so that i have got to guess if i have to check some thing like sequential access.
                        Please help me.
                        Attached Files

                        Comment

                        • PRR
                          Recognized Expert Contributor
                          • Dec 2007
                          • 750

                          #13
                          use this function
                          Code:
                          ArrayList addrs = new ArrayList();
                          
                          addrs.Add("http://www.charlespetzold.com/key/keycs.jpg");
                                      addrs.Add("http://www.nbcindia.com/Booksimages/1931841306.gif");
                          
                          
                          int j = 0;
                                      foreach (string s in addrs)
                                      {
                                          try
                                          {                    
                                              HttpWebRequest req = (HttpWebRequest)WebRequest.Create(s);
                          HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                          
                          using (Stream st = resp.GetResponseStream())
                                              {
                                                  MemoryStream ms =(MemoryStream) CopyStream(st);
                                                  long len = ms.Length;
                                                  byte[] b = new byte[len];
                                                  
                                                      //using (FileStream f = new FileStream(@"C:\one"+j+".jpg", //FileMode.OpenOrCreate))
                                                      {
                                                          ++j;
                                                          //long len = resp.ContentLength;
                          
                                                          using (Image image = Image.FromStream(ms))
                                                          {
                                                              //st.Read(b, 0, b.Length);
                          
                                                              image.Save(@"C:\one" + j + ".jpg");
                                                          }
                          }
                          }
                          Code:
                          // Author Jon Skeet
                          private static Stream CopyStream(Stream inputStream)
                                  {
                                      const int readSize = 256;
                                      byte[] buffer = new byte[readSize];
                                      MemoryStream ms = new MemoryStream();
                                      int count = inputStream.Read(buffer, 0, readSize);
                                      while (count > 0)
                                      {
                                          ms.Write(buffer, 0, count);
                                          count = inputStream.Read(buffer, 0, readSize);
                                      }
                                      ms.Seek(0, SeekOrigin.Begin);
                                      return ms;
                                  }
                          I tried with 5-6 images ... almost all are "completely " saved ....

                          Comment

                          Working...