locked
[XAML] How to Cache a Image of a URL for Image control RRS feed

  • Question

  • Hi,

    How to do a image cache of a url  for Image control ?

    Thanks,



    • Edited by PM16 Thursday, March 3, 2016 4:36 PM
    • Edited by Fred Bao Friday, March 4, 2016 3:23 AM add the tag
    Thursday, March 3, 2016 12:38 PM

Answers

  • The OS will cache a image from the network, otherwise you'll have to save the image to disk and reference the disk version in the source property.

    http://pauliom.wordpress.com

    • Proposed as answer by Krunal Parekh Monday, March 7, 2016 8:22 AM
    • Marked as answer by Fred Bao Tuesday, March 15, 2016 7:21 AM
    Thursday, March 3, 2016 1:49 PM
  • You cannot rely on the http cache when offline, it might have the image it might not. If you want to guarantee it then you must download the image to a local store location and then use a direct path to the local storage, NOT the original URI. BTW CacheMode isn't what you think here.

    http://pauliom.wordpress.com

    Friday, March 4, 2016 12:19 PM

All replies

  • The OS will cache a image from the network, otherwise you'll have to save the image to disk and reference the disk version in the source property.

    http://pauliom.wordpress.com

    • Proposed as answer by Krunal Parekh Monday, March 7, 2016 8:22 AM
    • Marked as answer by Fred Bao Tuesday, March 15, 2016 7:21 AM
    Thursday, March 3, 2016 1:49 PM
  • Thanks for you reply.

    If the OS Caches the image from the network, Once the network is not available Will the same Image be loaded for the Same URL?

    Thanks,

    Thursday, March 3, 2016 1:55 PM
  • Hi,

    In my Windows store app i have displayed a picture from Web URL in Image Control  programmaticallyas follows:

    In my xaml  file

     <Image   CacheMode="BitmapCache" x:Name="imgFlower" Height="150"  VerticalAlignment="Center"/>

     In my xaml.cs file I ahve set source programatically as follows:

                BitmapImage bi = new BitmapImage();
                 bi.CreateOptions = BitmapCreateOptions.None;
                 bi.UriSource = new Uri("https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQVV19fxxWN-h6ZX4uWAEk68i4naosjqCWskUuBsMBAEf4bjnf8Bg", UriKind.Absolute);

                 imgFlower.Source = bi;  

    I could see the cached file in my application's local packages folder. But That image is not displayed when the internet connection is not available. I need to fix this issue immediately.

    How could i solve this issue?

    Thanks,
    Friday, March 4, 2016 2:08 AM
  • You cannot rely on the http cache when offline, it might have the image it might not. If you want to guarantee it then you must download the image to a local store location and then use a direct path to the local storage, NOT the original URI. BTW CacheMode isn't what you think here.

    http://pauliom.wordpress.com

    Friday, March 4, 2016 12:19 PM
  • Than you for your reply pkr.

    In my application,
    I have displayed items in a GridView where each item has a variable called ImageUrl & it has web url.

    Those images are cached & displayed though there is not internet connection available. But if i try the same in another page for a single Image control it will not be happened.

    I could not find what could be the reason for this different behaviour in the same application.

    Can you please suggest me what could be wrong in it .

    Monday, March 7, 2016 5:19 AM
  • Hello PM16,

    >>I could not find what could be the reason for this different behaviour in the same application. Can you please suggest me what could be wrong in it .

    Without reproduction sample it is difficult to tell but i would also not recommend to rely on such method as @pkr2000 suggested it would be better to download the images locally and then set them as image source. you can use NetworkInformation class to find if it is connected to the internet or not (see: NetworkInformation.GetInternetConnectionProfile) and put conditions accordigly. 

    With Regards,

    Krunal Parekh 


    Thanks MSDN Community Support Please remember to Mark as Answer the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, March 7, 2016 8:39 AM
  • Thank you Krunal.

    I will proceed with downloading images to local folder :)

    Monday, March 7, 2016 1:03 PM