예제 #1
0
 /// <summary>
 /// The add cache item.
 /// </summary>
 /// <param name="url">The url.</param>
 /// <param name="imageCacheItem">The image cache item.</param>
 internal static void AddCacheItem(string url, ImageCacheItem imageCacheItem)
 {
     lock (ImageCacheLock)
     {
         imageCacheDictionary.Add(url, imageCacheItem);
     }
 }
예제 #2
0
        /// <summary>
        /// The get image.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <returns>The System.Windows.Media.Imaging.BitmapImage.</returns>
        public static BitmapImage GetImage(BitmapImage image)
        {
            var url = image.UriSource.ToString();

            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                image.UriSource = new Uri(url);
                return(image);
            }

            if (imageCacheDictionary == null)
            {
                lock (ImageCacheLock)
                {
                    if (imageCacheDictionary == null)
                    {
                        LoadCachedImageInfo();
                    }
                }
            }

            ImageCacheItem cacheItem;

            if (imageCacheDictionary != null && imageCacheDictionary.TryGetValue(url, out cacheItem))
            {
                if (DateTime.Compare(DateTime.Now, cacheItem.Expiration) >= 0)
                {
                    /* Item has expired. */
                    ImageDownloader.DownloadImage(url, image, cacheItem);
                }
                else
                {
                    string imageFileName = cacheItem.LocalFilename;
                    var    storageFile   = IsolatedStorageFile.GetUserStoreForApplication();
                    var    imageFile     = storageFile.OpenFile(imageFileName, FileMode.Open);

                    image.SetSource(imageFile);
                }
            }
            else
            {
                var item = new ImageCacheItem();
                ImageDownloader.DownloadImage(url, image, item);
            }

            return(image);
        }
예제 #3
0
        /// <summary>
        /// The download image.
        /// </summary>
        /// <param name="imageUrl">The image url.</param>
        /// <param name="image">The image.</param>
        /// <param name="imageCacheItem">The image cache item.</param>
        public static void DownloadImage(string imageUrl, BitmapImage image, ImageCacheItem imageCacheItem)
        {
            var filename = CreateUniqueFilename(imageUrl);
            imageCacheItem.LocalFilename = filename;

            var asyncDataTransfer = new AsyncDataTransfer
            {
                ImageCacheItem = imageCacheItem,
                Image = image
            };

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(imageUrl);
            if (imageCacheItem.ImageId != null)
            {
                httpWebRequest.Headers["If-None-Match"] = imageCacheItem.ImageId;
            }

            asyncDataTransfer.Request = httpWebRequest;

            httpWebRequest.BeginGetResponse(RequestCallback, asyncDataTransfer);
        }
예제 #4
0
        /// <summary>
        /// The download image.
        /// </summary>
        /// <param name="imageUrl">The image url.</param>
        /// <param name="image">The image.</param>
        /// <param name="imageCacheItem">The image cache item.</param>
        public static void DownloadImage(string imageUrl, BitmapImage image, ImageCacheItem imageCacheItem)
        {
            var filename = CreateUniqueFilename(imageUrl);

            imageCacheItem.LocalFilename = filename;

            var asyncDataTransfer = new AsyncDataTransfer
            {
                ImageCacheItem = imageCacheItem,
                Image          = image
            };

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(imageUrl);

            if (imageCacheItem.ImageId != null)
            {
                httpWebRequest.Headers["If-None-Match"] = imageCacheItem.ImageId;
            }

            asyncDataTransfer.Request = httpWebRequest;

            httpWebRequest.BeginGetResponse(RequestCallback, asyncDataTransfer);
        }
예제 #5
0
        /// <summary>
        /// The get image.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <returns>The System.Windows.Media.Imaging.BitmapImage.</returns>
        public static BitmapImage GetImage(BitmapImage image)
        {
            var url = image.UriSource.ToString();

            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                image.UriSource = new Uri(url);
                return image;
            }

            if (imageCacheDictionary == null)
            {
                lock (ImageCacheLock)
                {
                    if (imageCacheDictionary == null)
                    {
                        LoadCachedImageInfo();
                    }
                }
            }

            ImageCacheItem cacheItem;
            if (imageCacheDictionary != null && imageCacheDictionary.TryGetValue(url, out cacheItem))
            {
                if (DateTime.Compare(DateTime.Now, cacheItem.Expiration) >= 0)
                {
                    /* Item has expired. */
                    ImageDownloader.DownloadImage(url, image, cacheItem);
                }
                else
                {
                    string imageFileName = cacheItem.LocalFilename;
                    var storageFile = IsolatedStorageFile.GetUserStoreForApplication();
                    var imageFile = storageFile.OpenFile(imageFileName, FileMode.Open);

                    image.SetSource(imageFile);
                }
            }
            else
            {
                var item = new ImageCacheItem();
                ImageDownloader.DownloadImage(url, image, item);
            }

            return image;
        }
예제 #6
0
 /// <summary>
 /// The add cache item.
 /// </summary>
 /// <param name="url">The url.</param>
 /// <param name="imageCacheItem">The image cache item.</param>
 internal static void AddCacheItem(string url, ImageCacheItem imageCacheItem)
 {
     lock (ImageCacheLock)
     {
         imageCacheDictionary.Add(url, imageCacheItem);
     }
 }