Convict Moody Convict Moody - 1 year ago 245
C# Question

C# WinForms Clear http images from cache

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.

Answer Source

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.,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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download