2014-01-08 77 views
0

我正在開發一個應用程序,我從網上下載圖像並將它們存儲到獨立存儲中。這是我的代碼。從網下載多張圖片

private void LoadImage(List<ProductImageList> item) 
     { 
      BitmapImage bi = new BitmapImage(); 

      foreach (var product in item) 
      { 
       string a = product.ImageUrl; 
       string b = a.Substring(1, a.Length - 2); 
       Uri uri = new Uri(b, UriKind.RelativeOrAbsolute); 
       using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
       { 
        //load image from Isolated Storage if it already exist 
        name = System.IO.Path.GetFileName(b); 
        if (myIsolatedStorage.FileExists(name)) 
        { 

        } 


        else 
        { 
         WebClient wc = new WebClient(); 
         wc.OpenReadCompleted += new OpenReadCompletedEventHandler(DownloadCompleted); 
         wc.OpenReadAsync(uri, wc); 
        } 

       } 
      } 

     } 

     private void DownloadCompleted(object sender, OpenReadCompletedEventArgs e) 
     { 
      try{ 
        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
        { 
         IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(name); 

         BitmapImage bitmap = new BitmapImage(); 
         bitmap.SetSource(e.Result); 
         WriteableBitmap wb = new WriteableBitmap(bitmap); 

         // Encode WriteableBitmap object to a JPEG stream. 
         System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85); 
         fileStream.Close(); 
        } 

     } 
     catch (Exception ex) 
     { 
      //Exception handle appropriately for your app 
     } 
    } 

該項目包含圖像標識,ImageUrl.The代碼只下載我的列表的最後圖像的所有time.Please建議我我如何可以下載所有的圖片..

回答

0

您應該使用的HttpRequest,而不是這裏的WebClient就是這個例子

private void LoadImage(List<ProductImageList> item) 
     { 
      BitmapImage bi = new BitmapImage(); 
      foreach (var product in item) 
      { 
       string a = product.ImageUrl; 
       string b = a.Substring(1, a.Length - 2); 
       Uri uri = new Uri(b, UriKind.RelativeOrAbsolute); 
       using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
       { 
        //load image from Isolated Storage if it already exist 
        name = System.IO.Path.GetFileName(b); 
        if (myIsolatedStorage.FileExists(name)) 
        { 

        } 
        else 
        { 
         HttpWebRequest imageRequest = HttpWebRequest.CreateHttp(); 
         imageRequest.Headers["ImageName"] = name; 
         imageRequest.BeginGetResponse(Imageresponse, imageRequest); 
        } 
       } 
      } 
     } 

     private void Imageresponse(IAsyncResult asyncResult) 
     { 
      try 
      { 
       string name = string.Empty; 
       HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState; 
       HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult); 
       using (Stream data = response.GetResponseStream()) 
       { 
        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
        { 
         name = request.Headers["ImageName"]; 
         if (!string.IsNullorEmpty(name)) 
         { 
          using (IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(name)) 
          { 

           BitmapImage bitmap = new BitmapImage(); 
           bitmap.SetSource(data); 
           WriteableBitmap wb = new WriteableBitmap(bitmap); 
           // Encode WriteableBitmap object to a JPEG stream. 
           System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85); 

          } 
         } 
        } 
       } 

      } 
      catch (Exception ex) 
      { 

      } 
     }