Example #1
0
        public static void Load()
        {
            picturesCache = new PicturesCache();
            try
            {
                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (store.FileExists(FileName))
                    {
                        using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(FileName, FileMode.Open, store))
                        {
                            DataContractSerializer  serializer = new DataContractSerializer(typeof(List <PictureItemStore>));
                            List <PictureItemStore> items      = (List <PictureItemStore>)serializer.ReadObject(stream);

                            foreach (var itemStore in items)
                            {
                                WriteableBitmap _bitmap = new WriteableBitmap(itemStore.Width, itemStore.Height);
                                int             count   = _bitmap.Pixels.Length * sizeof(int);
                                Buffer.BlockCopy(itemStore.Pixels, 0, _bitmap.Pixels, 0, count);
                                PictureCacheItem picItem = new PictureCacheItem(_bitmap, itemStore.PictureId, itemStore.Hash);
                                CacheItem        item    = new CacheItem(picItem, itemStore.ValidDuration, itemStore.CreationDate);
                                picturesCache.Cache.Add(itemStore.PictureId.ToString(), item);
                            }
                        }
                    }
                }
            }
            catch
            {
            }
        }
Example #2
0
 public void AddToCache(PictureCacheItem image)
 {
     CacheManager.PictureCache.Add(image.PictureId.ToString(), image, TimeSpan.FromDays(10));
     if (downloadingPictures.ContainsKey(image.PictureId))
     {
         downloadingPictures.Remove(image.PictureId);
     }
 }
Example #3
0
        public PictureCacheItem GetImage(PictureInfoDTO pictureInfo)
        {
            PictureCacheItem picture = (PictureCacheItem)CacheManager.PictureCache.Get(pictureInfo.PictureIdk__BackingField.ToString());

            if (picture == null || (picture.Image != null && picture.Hash != pictureInfo.Hashk__BackingField))
            {
                loadImage(pictureInfo);
            }
            return(picture);
        }
Example #4
0
        public void Save()
        {
            if (CacheManager.PictureCache.CacheItems.Values.Count == 0)
            {
                return;
            }
            try
            {
                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(FileName, FileMode.Create, store))
                    {
                        List <PictureItemStore> items = new List <PictureItemStore>();

                        foreach (var item in CacheManager.PictureCache.CacheItems.Values)
                        {
                            PictureItemStore storeItem = new PictureItemStore();
                            PictureCacheItem cacheItem = (PictureCacheItem)item.Data;
                            WriteableBitmap  _bitmap   = (WriteableBitmap)cacheItem.Image;
                            if (cacheItem.Image == null)
                            {
                                continue;
                            }
                            storeItem.Width         = cacheItem.Image.PixelWidth;
                            storeItem.Height        = cacheItem.Image.PixelHeight;
                            storeItem.PictureId     = cacheItem.PictureId;
                            storeItem.ValidDuration = item.ValidDuration;
                            storeItem.CreationDate  = item.CreationDate;
                            storeItem.Hash          = cacheItem.Hash;
                            int count = _bitmap.Pixels.Length * sizeof(int);
                            storeItem.Pixels = new byte[count];
                            Buffer.BlockCopy(_bitmap.Pixels, 0, storeItem.Pixels, 0, count);
                            items.Add(storeItem);
                        }
                        DataContractSerializer serializer = new DataContractSerializer(typeof(List <PictureItemStore>));
                        serializer.WriteObject(stream, items);
                    }
                }
            }
            catch (Exception)
            {
            }
        }
        public void Execute()
        {
            try
            {
                var m = new ServiceManager <GetImageCompletedEventArgs>(delegate(BodyArchitectAccessServiceClient client1, EventHandler <GetImageCompletedEventArgs> operationCompleted)
                {
                    using (OperationContextScope ocs = new OperationContextScope(client1.InnerChannel))
                    {
                        OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("SessionId", "http://MYBASERVICE.TK/", ApplicationState.Current.SessionData.Token.SessionId));
                        OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("PictureId", "http://MYBASERVICE.TK/", picture.PictureIdk__BackingField));
                        OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Hash", "http://MYBASERVICE.TK/", picture.Hashk__BackingField));
                        ApplicationState.AddCustomHeaders();
                        client1.GetImageCompleted -= operationCompleted;
                        client1.GetImageCompleted += operationCompleted;
                        client1.GetImageAsync();
                    }
                });
                m.OperationCompleted += (s, a) =>
                {
                    if (a.Error != null)
                    {
                        PictureCacheItem item = new PictureCacheItem(null, picture.PictureIdk__BackingField, picture.Hashk__BackingField);
                        PicturesCache.Instance.AddToCache(item);
                        PicturesCache.Instance.Notify(new PictureInfoDTO()
                        {
                            PictureIdk__BackingField = picture.PictureIdk__BackingField, Hashk__BackingField = picture.Hashk__BackingField
                        });
                    }
                    else if (a.Result != null)
                    {
                        MemoryStream  stream = new MemoryStream(a.Result.Result);
                        ExtendedImage desert = new ExtendedImage();
                        desert.SetSource(stream);
                        desert.LoadingFailed += delegate(object sender, UnhandledExceptionEventArgs e)
                        {
                            PictureCacheItem item = new PictureCacheItem(null, picture.PictureIdk__BackingField, picture.Hashk__BackingField);
                            PicturesCache.Instance.AddToCache(item);
                            PicturesCache.Instance.Notify(new PictureInfoDTO()
                            {
                                PictureIdk__BackingField = picture.PictureIdk__BackingField, Hashk__BackingField = picture.Hashk__BackingField
                            });
                        };
                        desert.LoadingCompleted += delegate
                        {
                            ApplicationState.SynchronizationContext.Post(delegate
                            {
                                var image             = desert.ToBitmap();
                                PictureCacheItem item = new PictureCacheItem(image, picture.PictureIdk__BackingField, picture.Hashk__BackingField);
                                PicturesCache.Instance.AddToCache(item);
                                PicturesCache.Instance.Notify(picture);
                                //if (OperationCompleted != null)
                                //{
                                //    OperationCompleted(this, EventArgs.Empty);
                                //}
                            }, null);
                        };
                    }
                };

                m.Run();
            }
            catch (Exception)
            {
                PictureCacheItem item = new PictureCacheItem(null, picture.PictureIdk__BackingField, picture.Hashk__BackingField);
                PicturesCache.Instance.AddToCache(item);
                PicturesCache.Instance.Notify(new PictureInfoDTO()
                {
                    PictureIdk__BackingField = picture.PictureIdk__BackingField, Hashk__BackingField = picture.Hashk__BackingField
                });
            }
        }