public static BitmapImage DownloadWebImage(Uri url)
{
// TODO: this method needs to gracefully handle unresolvable URLs and connection time-outs
BitmapImage bitmap = new BitmapImage(url,
new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.CacheIfAvailable));
// To make the BitmapImages thread-safe for the BackgroundWorker, they need to
// be frozen (bitmap.Freeze()), but they shouldn't be frozen until they're done downloading.
// We have to force the UI thread to wait until the image is downloaded so we can freeze it.
// Otherwise, we get images in the grid that are partially-downloaded.
// TODO: this is poor design, but after much searching, there may not be a better solution.
// according to MSDN, DispatcherFrames can be implemented for
// "Short running, very specific frames that exit when an important criteria is met."
while (bitmap.IsDownloading) { DoEvents(); };
bitmap.Freeze(); // Bitmap is now thread-safe and can be operated on by the backgroundworker
return bitmap;
}