예제 #1
0
        public byte[] Fetch(ImageCriteria criteria)
        {
            var value = _cache.Get <byte[]>(criteria.ToHash());

            if (value == null)
            {
                var image = _source.Fetch(criteria);
                _cache.Set(criteria.ToHash(), image, new MemoryCacheEntryOptions()
                {
                    Size = image.Length
                });

                return(image);
            }

            return(value);
        }
        public byte[] Fetch(ImageCriteria criteria)
        {
            //check if the image is there
            var file = Path.Combine(_imageDirectory, criteria.File);

            if (!File.Exists(file))
            {
                throw new ArgumentException("Invalid file provided [" + criteria.File + "]");
            }

            var imageFormat = GetFormat(criteria.Format);

            using (Image image = Image.Load(file))
            {
                image.Mutate(context =>
                {
                    if (criteria.Width != 0 && criteria.Height != 0)
                    {
                        context = context.Resize(criteria.Width, criteria.Height);
                    }

                    if (criteria.ShouldAddWatermark())
                    {
                        context = ApplyWaterMark(context, criteria.Watermark);
                    }

                    if (criteria.ShouldAddBackgroundColour())
                    {
                        context = ApplyBackgroundColor(context, criteria.RGBBackgroundColour);
                    }
                });

                using (var ms = new MemoryStream())
                {
                    image.Save(ms, imageFormat);
                    return(ms.ToArray());
                }
            }
        }