0

I am having this issue with picture box, where it have to load images for categories from the web, the problem is when the image is changed on the server, it still load the cached copy.

Convict Moody
  • 781
  • 1
  • 9
  • 28
  • @rene I don't want it to get changed automatically, i got a list of categories if u want, and each category have a button, when u press the category it loads the image assigned to that category from the server, and u can update the image manually from this application, whereas when u click the button of a category that you have changed its photo it shows the old photo, even if u restarted the app, it would fetch the new photo after 5 mins or so.... – Convict Moody Aug 14 '16 at 15:09

1 Answers1

2

The PictureBox class uses a WebClient instance to load the image from the server as you can see in the always usefule source reference from Microsoft.

http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/PictureBox.cs,d1a979fa214db76f

If the WebClient.CachePolicy property is not explicitly set (which is not done in PictureBox.Load) then WebRequest.DefaultCachePolicy is used.

So depending on your application you can choose one of these approaches:

  • Clearing the cache for this url before setting the PictureBox.ImageLocation. This is explained in How to clear the cache of HttpWebRequest
  • Download the image with your own WebClient instance with set CachePolicy and initialize the PictureBox from the downloaded content stream like Load Picturebox Image From Memory?

  • Change WebRequest.DefaultCachePolicy globally (as a last resort) e.g.

    WebRequest.DefaultCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);

Hope this was helpful.

Community
  • 1
  • 1
Ralf Bönning
  • 14,515
  • 5
  • 49
  • 67
  • Thank you for passing by, clearing the cache from internet explorer didn't clear it for PictureBox, so i changed DefaultCachePolicy since the images are the only http requests used in the app. Thank you. – Convict Moody Aug 14 '16 at 15:42