×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Noida,India
    Posts
    4,026

    Download & save multiple images

    Trying to download multiple images from server & save them to isolated storage along with updating the UI. I have utilized the HTTPWebRequest API in place of WebClient, so that UI remains responsive while the images are getting downloaded.

    The problem i am facing is that after the response from server is received, a call to a function which saves the image is called but every-time the Stream object is accessed an exception : System.objectDisposedException is raised, implying that stream object is closed & when we are trying to access it(am i right?)

    Code:
    void ReadWebRequestCallbackImg(IAsyncResult callbackResult)
        {
            if (iChImgCnt < appinstance.ChnlViewModel.Count)
            {
                iChImgCnt++;
                strImageName = "ChmImgFolder\\" + "chlImg_" + iChImgCnt + ".png";
                try
                {
                    HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
                    HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);
                    using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
                    {
                        
                         Stream resp = myResponse.GetResponseStream();
                        Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                          
                         SaveToJpeg(myResponse.GetResponseStream());
                        })); 
                    }
                }
                catch (Exception e1)
                {
                    string crash = e1.ToString();
                    iIconCnt++;
                    CreateImage();
                }
    
            }
           
          }
    Code:
     private void SaveToJpeg(Stream stream)
        {
      
            using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
            {
    
                if (iso.FileExists(strImageName))
                {
                    iso.DeleteFile(strImageName);
                }
                using (IsolatedStorageFileStream isostream = iso.CreateFile(strImageName))
                {
                    
                      BitmapImage bitmap = new BitmapImage();
                      bitmap.SetSource(stream);                        System.ObjectDisposedException occurs here
                      WriteableBitmap wb = new WriteableBitmap(bitmap);
    
                      // Encode WriteableBitmap object to a JPEG stream.
                      //wb.SaveJpeg(isostream, wb.PixelWidth, wb.PixelHeight, 0, 85);
                     // wb.LoadJpeg(stream);
                      if (iIconCnt < appinstance.ChnlViewModel.Count())
                      {
    
                          appinstance.ChnlViewModel[iIconCnt].Iconimage = bitmap;
                          iIconCnt++;
                      }
                  
                }
            }
        }
    So any resolution for the same, or possibly any other approach to download multiple images without blocking the UI ( have tried with WebClient, but as it runs on UI thread only, it certainly blocks the UI)

    (utilizing Windows Phone 8 SDK)
    Last edited by vineet.jain; 2013-08-30 at 11:18.

  2. #2
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Noida,India
    Posts
    4,026

    Re: Download & save multiple images

    Thanks for your reply Pooja. I am aware about this code but my need is to save the images & then show them on UI but in the code example the image download path is directly mentioned in XAML(implying that images will be downloaded each time app is launched) which i cannot do.
    Last edited by vineet.jain; 2013-08-30 at 11:32.

  3. #3
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    India
    Posts
    1,173

    Re: Download & save multiple images

    Just check out the section "Load Images" in the article by Pasi
    http://developer.nokia.com/Community...#Load_a_images

  4. #4
    Nokia Developer Champion
    Join Date
    Feb 2013
    Location
    Dublin, Ireland
    Posts
    552

    Re: Download & save multiple images

    I have a couple of suggestions.

    1. Use WebClient() or HttpClient() Portable. They both have simple APIs, and a lot less code is required compared to HttpWebRequest. They shouldn't be doing their work on the UI thread, but if you have a large amount of work to do then wrap the code in the following...

    Code:
    await Task.Run(() => { your code here })
    2. When running code on a background (non-UI thread), some components MUST be run on the UI thread, BitmapImage is one of those...

    Code:
    Dispatcher.BeginInvoke(() => { UI thread work here });

  5. #5
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Noida,India
    Posts
    4,026

    Re: Download & save multiple images

    Quote Originally Posted by theothernt View Post
    I have a couple of suggestions.

    1. Use WebClient() or HttpClient() Portable. They both have simple APIs, and a lot less code is required compared to HttpWebRequest. They shouldn't be doing their work on the UI thread, but if you have a large amount of work to do then wrap the code in the following...

    Code:
    await Task.Run(() => { your code here })
    2. When running code on a background (non-UI thread), some components MUST be run on the UI thread, BitmapImage is one of those...

    Code:
    Dispatcher.BeginInvoke(() => { UI thread work here });

    I think WebClient returns to the UI thread only while HTTPWebRequest returns to the thread through which its started(as it runs on its own thread not UI ) , thereby i opted for it as Webclient was really blocking the UI till the time all the images got loaded.

    Ok, i have to admit i wrote WP8 SDK only but i am targeting WP7 as well, so i actually utilized await Task.Run(() =>(downloaded the Microsoft.bcl.Async package for WP7 support) but then when i ran the code on a WP7 emulator the UI started to hang after which i opted HTTPWebRequest thinking those thread things. Probably i again need to look at the things in code as which part was blocking.

    The UI updation code is written inside Dispatcher.BeginInvoke(() => only when HTTPWebRequest returns the response (as visible in the first function i posted), anyways i'll analyze the code more & update accordingly. Thanks for the help.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    34

    Re: Download & save multiple images

    I would use the HttpClient. The Microsoft HTTP Client Libraries are also supported in 7.5.

    Just be carefull not to block the UI thread (How to (not) deadlock your UI using async-await). And ConfigureAwait is your friend.

  7. #7
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,688

    Re: Download & save multiple images

    As you want to store the images anyway, it might be more straightforward and safer at the same time to save the data first, and create the bitmaps later.

Similar Threads

  1. How to save images in the mobile(using JSR-75)?
    By panwoo in forum Mobile Java General
    Replies: 16
    Last Post: 2011-01-05, 04:14
  2. How to download multiple images from a website
    By sparrow007 in forum Mobile Java Media (Graphics & Sounds)
    Replies: 14
    Last Post: 2010-05-19, 13:09
  3. save captured images
    By ozoz in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2009-05-14, 05:11
  4. I can't save images using JSR75
    By Sory in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2006-11-21, 18:12
  5. Save images to phone
    By adneimocne in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2004-05-31, 14:52

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×