0

In my WP7 application I have downloaded 200 images from Web and saved in isolated storage .When debug all the images are loaded in panorama view by queue method and I can view when it is connected to pc. after disconnect it from pc when i open the application and navigate the images it shows some images and terminated.

    if (i < 150)
    {

        WebClient m_webClient = new WebClient();             
        Uri m_uri = new Uri("http://d1mu9ule1cy7bp.cloudfront.net/2012//pages/p_" + i + "/mobile_high.jpg");
        m_webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
        m_webClient.OpenReadAsync(m_uri);

    }        

void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    int count;

    try
    {
        Stream stream = e.Result;              
        byte[] buffer = new byte[1024];

        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        {

           //isf.Remove();

            using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("IMAGES" + loop2(k) + ".jpg", FileMode.Create, isf))
            {
                count = 0;

                while (0 < (count = stream.Read(buffer, 0, buffer.Length)))
                {
                    isfs.Write(buffer, 0, count);
                }

                stream.Close();
                isfs.Close();
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}
Robaticus
  • 22,857
  • 5
  • 54
  • 63
SENTHIL KUMAR
  • 647
  • 4
  • 17
  • Could you give us some codes of the way you use isolatedStorage, ...? – ChapMic Mar 08 '12 at 12:30
  • Yes in real device only..I have loaded the images in panorama view first added 3 images from iso store and using selection changed event to remove first image and add the 4th image vicecersa.. – SENTHIL KUMAR Mar 08 '12 at 12:41

3 Answers3

1

I think that your problem is that if you load too many images at once in a loop the moment you go out of the loop and give focus back to the UI thread all the Garbage Collection on the bitmap images is done.

This article explains it a bit better and provides with a solution.

I also had this problem and came up with my own solution. I had a dictonairy with image url that needed to be loaded, but you can easily alter this for your scenario.

This SO question is also about this problem (loading multiple images and crash (Exception)). It also has Microsofts response to it, I based my solution on their response.

In my solution I use the dispatcher to return to the UI thread and thus making sure the garbage of the image and bitmaps used was cleaned.

private void LoadImages(List<string> sources)
{
    List<string>.Enumerator iterator = sources.GetEnumerator();
    this.Dispatcher.BeginInvoke(() => { LoadImage(iterator); });
}

private void LoadImage(List<string>.Enumerator iterator)
{
    if (iterator.MoveNext())
    {
        //TODO: Load the image from iterator.Current

        //Now load the next image
        this.Dispatcher.BeginInvoke(() => { LoadImage(iterator); });
    }
    else
    {
        //Done loading images
    }
}
Community
  • 1
  • 1
SynerCoder
  • 12,493
  • 4
  • 47
  • 78
  • Did you say that there is no need of using iso store here..for my aplication its the must for me to have a local directory and filepaths – SENTHIL KUMAR Mar 08 '12 at 12:53
  • I did not say that, I said you can easily alter my solution to fit your scenario. – SynerCoder Mar 08 '12 at 12:58
  • I can rewrite my code to fit your problem, but then I need to know how you loop it. What does `loop2(k)` do? What is `k`? What is the actual loading loop? – SynerCoder Mar 08 '12 at 13:00
  • its just increment the integer value one by one from 0 to 150..the image url is in serial way http://d1mu9ule1cy7bp.cloudfront.net/2012//pages/p_0/mobile_high.jpg http://d1mu9ule1cy7bp.cloudfront.net/2012//pages/p_1/mobile_high.jpg...http://d1mu9ule1cy7bp.cloudfront.net/2012//pages/p_150/mobile_high.jpg – SENTHIL KUMAR Mar 08 '12 at 13:48
  • yes for to increment using this method public int loop2(int i) { k = i + 1; return k - 1; } you can use for loop also – SENTHIL KUMAR Mar 08 '12 at 13:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8672/discussion-between-senthil-kumar-and-synercoder) – SENTHIL KUMAR Mar 08 '12 at 13:53
0

After talking on Skype I reviewed his code and found out his problem was with his Isolated Storage Explorer. It couldnt connect to his pc so it gave an error. Had nothing to do with the image loading.

SynerCoder
  • 12,493
  • 4
  • 47
  • 78
0

I'd be very wary of the memory implications of loading 200 images at once. Have you been profiling the memory usage? Using too much memory could cause your application to be terminated.

Matt Lacey
  • 65,560
  • 11
  • 91
  • 143