从网络下载多个图像

时间:2014-01-08 04:20:33

标签: windows-phone-8

我正在开发一个应用程序,我从网上下载图像并将它们存储到隔离存储中。这是我的代码。

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

该项目包含ImageId,ImageUrl。代码只下载我列表的最后一张图片。请指教我如何下载所有图片..

1 个答案:

答案 0 :(得分: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)
            {

            }
        }