2012-05-15 3 views
0
public void GetAllImages() 
     { 

      // Bing Image Result for Cat, First Page 
      string url = "http://www.bing.com/images/search?q=cat&go=&form=QB&qs=n"; 


      // For speed of dev, I use a WebClient 
      WebClient client = new WebClient(); 
      string html = client.DownloadString(url); 

      // Load the Html into the agility pack 
      HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 
      doc.LoadHtml(html); 

      // Now, using LINQ to get all Images 
      /*List<HtmlNode> imageNodes = null; 
      imageNodes = (from HtmlNode node in doc.DocumentNode.SelectNodes("//img") 
          where node.Name == "img" 
          && node.Attributes["class"] != null 
          && node.Attributes["class"].Value.StartsWith("sg_t") 
          select node).ToList();*/ 

      var imageLinks = doc.DocumentNode.Descendants("img") 
    .Where(n => n.Attributes["class"].Value == "sg_t") 
    .Select(n => HttpUtility.ParseQueryString(n.Attributes["src"].Value)["amp;url"]).ToList(); 





      foreach (string node in imageLinks) 
      { 
       y++; 
       //Console.WriteLine(node.Attributes["src"].Value); 
       richTextBox1.Text += node + Environment.NewLine; 
       Image t = DownloadImage(node); 
       t.Save(@"d:\test\" + y.ToString("D6" + ".jpg")); 

      } 


     } 


     public Image DownloadImage(string _URL) 
     { 
      Image _tmpImage = null; 

      try 
      { 
       // Open a connection 
       System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(_URL); 

       _HttpWebRequest.AllowWriteStreamBuffering = true; 

       // You can also specify additional header values like the user agent or the referer: (Optional) 
       //_HttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"; 
       //_HttpWebRequest.Referer = "http://www.google.com/"; 

       // set timeout for 20 seconds (Optional) 
       _HttpWebRequest.Timeout = 60000; 

       // Request response: 
       System.Net.WebResponse _WebResponse = _HttpWebRequest.GetResponse(); 

       // Open data stream: 
       System.IO.Stream _WebStream = _WebResponse.GetResponseStream(); 

       // convert webstream to image 
       _tmpImage = Image.FromStream(_WebStream); 

       // Cleanup 
       _WebResponse.Close(); 
       _WebResponse.Close(); 
      } 
      catch (Exception _Exception) 
      { 
       // Error 
       Console.WriteLine("Exception caught in process: {0}", _Exception.ToString()); 
       return null; 
      } 

      return _tmpImage; 
     } 

지금은 단지 richTextBox1을 사용하기 위해 다운로드 시간과 다운로드 시간을 표시해야합니다.어떻게하면 각 파일을 다운로드 할 때까지 남은 시간을 계산하고 표시 할 수 있습니까?

그리고 파일을 다운로드하는 빠른 방법이 있습니까, 아니면 httprequest로 충분합니까?

+1

제목이 질문과 일치하지 않는 것 같습니다. 무엇이 누락 되었습니까? –

답변

0

당신은 병렬로 다운로드 할 수 있습니다 :

Parallel.ForEach (하기 imagelinks, 노드 => {
이미지 t = DownloadImage (노드) 잠금 (Y) { t.Save (@ " D : \ 테스트 \ "+ (Y ++) ToString. ("D6 "+".JPG ")); } } 당신은 ASP.NET 응용 프로그램의 내부를 다운로드하는 경우

또는, 당신은 더 나은 그들을 다운로드 수 ajax 호출을 통해 클라이언트에서.

+0

Joshua Drake, 내부의 질문은 하위 질문입니다. 주요 질문은 남은 시간과 남은 시간을 계산하는 방법입니다. 결국 질문은 또 다른 질문입니다. – user1363119

관련 문제