Esempio n. 1
0
        /* private static void PropertyChangedCallback(DependencyObject dependencyObject,
                                                    DependencyPropertyChangedEventArgs
                                                        dependencyPropertyChangedEventArgs)*/
        private void load_image(String url)
        {
            if (_workItem != null && !_workItem.IsCompleted)
                return;
            _workItem = StaticInstance.ThreadPool.QueueWorkItem(() =>
                                                                {
                                                                    // var instance = dependencyObject as CachedBitmap;
                                                                    //SetLoading(true, instance);

                                                                    HashAlgorithm hashAlg = new SHA512CryptoServiceProvider();

                                                                    Crc64 crc64 = new Crc64(Crc64Iso.Iso3309Polynomial);

                                                                    string urlcrc64 =
                                                                        BitConverter.ToString(crc64.ComputeHash(Encoding.ASCII.GetBytes(url)))
                                                                                    .Replace("-", "").ToLower();

                                                                    string cachePath = Path.Combine(Path.GetTempPath(), "sjupdater", "imagechache");
                                                                    string filepath = Path.Combine(cachePath, urlcrc64.ToLower() + ".imagecache");

                                                                    Action downloadAndSetAndCache = () =>
                                                                                                    {
                                                                                                        using (MemoryStream ms = DownloadData(url))
                                                                                                        {
                                                                                                            if (ms == null) return;

                                                                                                            BitmapSource bitmapSource = BitmapImageFromStream(ms);
                                                                                                            dispatcher.Invoke(() =>
                                                                                                                              {

                                                                                                                                  ImageSource = bitmapSource;
                                                                                                                              });

                                                                                                            CacheData(filepath, ms, hashAlg);
                                                                                                        }
                                                                                                    };

                                                                    if (File.Exists(filepath))
                                                                    {
                                                                        byte[] cacheHash;

                                                                        MemoryStream ms = GetCachedData(filepath, hashAlg, out cacheHash);
                                                                        BitmapImage image = BitmapImageFromStream(ms);
                                                                        //  SetLoading(false, instance);

                                                                        dispatcher.Invoke(() =>
                                                                                          {
                                                                                              ImageSource = image;
                                                                                          });

                                                                        MemoryStream data = DownloadData(url);
                                                                        if (data == null)
                                                                            return;

                                                                        byte[] downloadHash = new byte[hashAlg.HashSize / 8];
                                                                        downloadHash = hashAlg.ComputeHash(data);

                                                                        if (!cacheHash.Memcmp(downloadHash))
                                                                        {
                                                                            // SetLoading(true, instance);
                                                                            downloadAndSetAndCache();
                                                                            //  SetLoading(false, instance);
                                                                        }
                                                                    }
                                                                    else
                                                                    {
                                                                        // SetLoading(true, instance);
                                                                        downloadAndSetAndCache();
                                                                        //  SetLoading(false, instance);
                                                                    }
                                                                }, true, ThreadPriority.Lowest);
        }
Esempio n. 2
0
        /* private static void PropertyChangedCallback(DependencyObject dependencyObject,
         *                                           DependencyPropertyChangedEventArgs
         *                                               dependencyPropertyChangedEventArgs)*/
        private void load_image(String url)
        {
            if (_workItem != null && !_workItem.IsCompleted)
            {
                return;
            }
            _workItem = StaticInstance.ThreadPool.QueueWorkItem(() =>
            {
                // var instance = dependencyObject as CachedBitmap;
                //SetLoading(true, instance);


                HashAlgorithm hashAlg = new SHA512CryptoServiceProvider();

                Crc64 crc64 = new Crc64(Crc64Iso.Iso3309Polynomial);

                string urlcrc64 =
                    BitConverter.ToString(crc64.ComputeHash(Encoding.ASCII.GetBytes(url)))
                    .Replace("-", "").ToLower();

                string cachePath = Path.Combine(Path.GetTempPath(), "sjupdater", "imagechache");
                string filepath  = Path.Combine(cachePath, urlcrc64.ToLower() + ".imagecache");

                Action downloadAndSetAndCache = () =>
                {
                    using (MemoryStream ms = DownloadData(url))
                    {
                        if (ms == null)
                        {
                            return;
                        }

                        BitmapSource bitmapSource = BitmapImageFromStream(ms);
                        dispatcher.Invoke(() =>
                        {
                            ImageSource = bitmapSource;
                        });

                        CacheData(filepath, ms, hashAlg);
                    }
                };

                if (File.Exists(filepath))
                {
                    byte[] cacheHash;

                    MemoryStream ms   = GetCachedData(filepath, hashAlg, out cacheHash);
                    BitmapImage image = BitmapImageFromStream(ms);
                    //  SetLoading(false, instance);

                    dispatcher.Invoke(() =>
                    {
                        ImageSource = image;
                    });

                    MemoryStream data = DownloadData(url);
                    if (data == null)
                    {
                        return;
                    }

                    byte[] downloadHash = new byte[hashAlg.HashSize / 8];
                    downloadHash        = hashAlg.ComputeHash(data);

                    if (!cacheHash.Memcmp(downloadHash))
                    {
                        // SetLoading(true, instance);
                        downloadAndSetAndCache();
                        //  SetLoading(false, instance);
                    }
                }
                else
                {
                    // SetLoading(true, instance);
                    downloadAndSetAndCache();
                    //  SetLoading(false, instance);
                }
            }, true, ThreadPriority.Lowest);
        }