locked
Converter Uri to BitmapImage - download image from web RRS feed

  • Question

  • I have problem with converter from Uri to BitmapImage. Uri is url of image on web. I use this converter on item in listbox. I download image from webpage and create from this stream BitampImage Problem is if listbox consist about 100 - 250 items, app freeze, I try call WebRequestMethod in another thread but it don’t work.

     

    Here is old version of code which work. Here I create BitmapImage from Uri.

              //imgUri is type of Uri
    
              var image = new BitmapImage();
              image.CreateOptions = BitmapCreateOptions.None;
              image.CacheOption = BitmapCacheOption.OnLoad;
    
              image.BeginInit();
    
              image.UriSource = imgUri;
    
              image.EndInit();
    
    
              return image;
    

    So I try download image with WebRequest class:

    First version is here:

              WebRequest webRequest = WebRequest.CreateDefault(imgUri);
              webRequest.ContentType = "image/jpeg";
              WebResponse webResponse = webRequest.GetResponse();
    
              var image = new BitmapImage();
              image.CreateOptions = BitmapCreateOptions.None;
              image.CacheOption = BitmapCacheOption.OnLoad;
              image.BeginInit();
    
              image.StreamSource = webResponse.GetResponseStream();
              image.EndInit();
    
     //if I don’t close webResponse it work, but app is freezing
              webResponse.Close();
    
              return image;

    I try class webRequest in another thread, because I would like avoid  freezing of my app.

     

    Second version.

     

                BitmapImage image = null;
      
                Task.Factory.StartNew(() =>
                             {
                               WebRequest webRequest = WebRequest.CreateDefault(imgUri);
                               webRequest.ContentType = "image/jpeg";
                               WebResponse webResponse = webRequest.GetResponse();
      
                               image = new BitmapImage();
                               image.CreateOptions = BitmapCreateOptions.None;
                               image.CacheOption = BitmapCacheOption.OnLoad;
                               image.BeginInit();
                               image.StreamSource = webResponse.GetResponseStream();
                               image.EndInit();
                               return image;
      
                               //((System.Action)(() =>
                               //{
      
      
                               //  //webResponse.Close();
      
                               //})).OnUIThread();
      
                             });
      
      
                return image;
    

    My aim is download image from web, create BitamImage object from him and return as Source of Image control, but I need avoid app freezing.

    Any advice?

    Wednesday, February 9, 2011 7:56 PM

Answers

  • Hi Janc1,

    So in your second version did you set a backgroundwork thread to "put the task into another thread"? I'd like to know more information about this. Would you post some code of it?

    And by saying it doesn't work do you mean the Source of Image control was not converted successfully?

    Best regards


    Yves Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Sheldon _Xiao Monday, March 7, 2011 6:01 AM
    • Marked as answer by Yves.Z Monday, March 7, 2011 6:41 AM
    Sunday, February 13, 2011 5:59 PM

All replies

  • Hi Janc1,

    So in your second version did you set a backgroundwork thread to "put the task into another thread"? I'd like to know more information about this. Would you post some code of it?

    And by saying it doesn't work do you mean the Source of Image control was not converted successfully?

    Best regards


    Yves Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Sheldon _Xiao Monday, March 7, 2011 6:01 AM
    • Marked as answer by Yves.Z Monday, March 7, 2011 6:41 AM
    Sunday, February 13, 2011 5:59 PM
  • Hi Janc1,

    To resove the your problem "app freeze", I think you have to do as Yves said, using another thread, based on my experience, this direction could resolve the "App freeze" problem.


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 7, 2011 6:01 AM