title: Loading an image in a background thread in WPF
body: <p>There are a bunch of questions about this already on this site and other forums, but I've yet to find a solution that actually works.</p>
  
  <p>Here's what I want to do:</p>
  
  <ul>
  <li>In my WPF app, I want to load an image.</li>
  <li>The image is from an arbitrary URI on the web.</li>
  <li>The image could be in any format.</li>
  <li>If I load the same image more than once, I want to use the standard windows internet cache.</li>
  <li>Image loading and decoding should happen synchronously, but not on the UI Thread.</li>
  <li>In the end I should end up with something that I can apply to an &lt;Image&gt;'s source property.</li>
  </ul>
  
  <p>Things I have tried:</p>
  
  <ul>
  <li>Using WebClient.OpenRead() on a BackgroundWorker. Works fine, but doesn't use the cache. WebClient.CachePolicy only affects that particular WebClient instance.</li>
  <li>Using WebRequest on the Backgroundworker instead of WebClient, and setting WebRequest.DefaultCachePolicy. This uses the cache properly, but I've not seen an example that doesn't give me corrupted-looking images half the time.</li>
  <li>Creating a BitmapImage in a BackgroundWorker, setting BitmapImage.UriSource and trying to handle BitmapImage.DownloadCompleted. This seems to use the cache if BitmapImage.CacheOption is set, but there doesn't seem to be away to handle DownloadCompleted since the BackgroundWorker returns immediately.</li>
  </ul>
  
  <p>I've been struggling with this off-and-on for literally months and I'm starting to think it's impossible, but you're probably smarter than me. What do you think?</p>