Esempio n. 1
0
        protected virtual string Resize(string id, Func <Image> factory, int width, int height)
        {
            var fileName = default(string);

            if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
            {
                return(fileName);
            }
            //TODO: Setting throwOnTimeout = false so we ignore synchronization timeout.
            //TODO: I think there exists a deadlock bug in KeyLock but I haven't been able to prove it.
            using (KeyLock.Lock(id, TIMEOUT, false))
            {
                if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
                {
                    return(fileName);
                }
                using (var image = new Bitmap(width, height))
                {
                    using (var graphics = Graphics.FromImage(image))
                    {
                        graphics.SmoothingMode     = SmoothingMode.HighQuality;
                        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        graphics.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                        this.Resize(graphics, factory, width, height);
                    }
                    using (var stream = new MemoryStream())
                    {
                        image.Save(stream, ImageFormat.Png);
                        stream.Seek(0, SeekOrigin.Begin);
                        return(FileMetaDataStore.Write(PREFIX, id, stream));
                    }
                }
            }
        }
Esempio n. 2
0
 public ImageSource CreateImageSource(LibraryHierarchyNode libraryHierarchyNode, int width, int height, bool cache)
 {
     try
     {
         var id = this.GetImageId(libraryHierarchyNode, width, height);
         if (cache)
         {
             var fileName = default(string);
             if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
             {
                 return(this.ImageLoader.Load(fileName, 0, 0, true));
             }
         }
         using (KeyLock.Lock(id))
         {
             if (cache)
             {
                 var fileName = default(string);
                 if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
                 {
                     return(this.ImageLoader.Load(fileName, 0, 0, true));
                 }
             }
             return(this.CreateImageSourceCore(libraryHierarchyNode, width, height, cache));
         }
     }
     catch (Exception e)
     {
         Logger.Write(this, LogLevel.Error, "Error creating image source: {0}", e.Message);
         return(null);
     }
 }
Esempio n. 3
0
        protected virtual string Resize(string id, Func <Image> factory, int width, int height)
        {
            var fileName = default(string);

            if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
            {
                return(fileName);
            }
            using (KeyLock.Lock(id))
            {
                if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
                {
                    return(fileName);
                }
                using (var image = new Bitmap(width, height))
                {
                    using (var graphics = Graphics.FromImage(image))
                    {
                        graphics.SmoothingMode     = SmoothingMode.HighQuality;
                        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        graphics.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                        this.Resize(graphics, factory, width, height);
                    }
                    using (var stream = new MemoryStream())
                    {
                        image.Save(stream, ImageFormat.Png);
                        stream.Seek(0, SeekOrigin.Begin);
                        return(FileMetaDataStore.Write(PREFIX, id, stream));
                    }
                }
            }
        }
Esempio n. 4
0
        public static string IfNotExists(string prefix, string id, Func <string, string> factory, bool always = false)
        {
            var result = default(string);

            if (!always && Exists(prefix, id, out result))
            {
                return(result);
            }
            using (KeyLock.Lock(GetFileName(prefix, id)))
            {
                if (!always && Exists(prefix, id, out result))
                {
                    return(result);
                }
                return(factory(result));
            }
        }
Esempio n. 5
0
        public async Task <MetaDataItem[]> GetMetaDatas(MetaDataCacheKey key, Func <Task <IEnumerable <MetaDataItem> > > factory)
        {
            var value = default(Lazy <MetaDataItem[]>);

            if (this.Values.TryGetValue(key, out value))
            {
                return(value.Value);
            }
            using (KeyLock.Lock(key))
            {
                if (this.Values.TryGetValue(key, out value))
                {
                    return(value.Value);
                }
                var metaDataItems = await factory().ConfigureAwait(false);

                return(this.Values.GetOrAdd(key, _key => new Lazy <MetaDataItem[]>(() => metaDataItems.ToArray())).Value);
            }
        }
Esempio n. 6
0
        public ImageSource CreateImageSource(LibraryHierarchyNode libraryHierarchyNode, int width, int height)
        {
            var id       = this.GetImageId(libraryHierarchyNode, width, height);
            var fileName = default(string);

            if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
            {
                return(this.ImageLoader.Load(id, fileName, 0, 0, true));
            }
            //TODO: Setting throwOnTimeout = false so we ignore synchronization timeout.
            //TODO: I think there exists a deadlock bug in KeyLock but I haven't been able to prove it.
            using (KeyLock.Lock(id, TIMEOUT, false))
            {
                if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
                {
                    return(this.ImageLoader.Load(id, fileName, 0, 0, true));
                }
                return(this.CreateImageSourceCore(libraryHierarchyNode, width, height));
            }
        }
 public ImageSource CreateImageSource(LibraryHierarchyNode libraryHierarchyNode, int width, int height, bool cache)
 {
     var id = this.GetImageId(libraryHierarchyNode, width, height);
     if (cache)
     {
         var fileName = default(string);
         if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
         {
             return this.ImageLoader.Load(fileName, 0, 0, true);
         }
     }
     using (KeyLock.Lock(id))
     {
         if (cache)
         {
             var fileName = default(string);
             if (FileMetaDataStore.Exists(PREFIX, id, out fileName))
             {
                 return this.ImageLoader.Load(fileName, 0, 0, true);
             }
         }
         return this.CreateImageSourceCore(libraryHierarchyNode, width, height, cache);
     }
 }