コード例 #1
0
        public virtual void Add(BackgroundImageClass image)
        {
            var imageKey = new ImageMetadata(image);

            if (RRContainer.Current.GetAllInstances <IFilter>().Where(x => x is SpriteFilter).FirstOrDefault(y => y.IgnoreTarget(new SpriteFilterContext(image))) != null)
            {
                return;
            }
            if (SpriteList.Any(x => x.Key.Equals(imageKey)))
            {
                var originalImage = SpriteList.First(x => x.Key.Equals(imageKey)).Value;
                var clonedImage   = new SpritedImage(originalImage.AverageColor, image, originalImage.Image)
                {
                    Position = originalImage.Position, Url = originalImage.Url, Metadata = imageKey
                };
                //SpriteContainer.AddImage(clonedImage);
                SpriteList.Add(new KeyValuePair <ImageMetadata, SpritedImage>(imageKey, clonedImage));
                return;
            }
            SpritedImage spritedImage;
            var          sprite = SpriteList.FirstOrDefault(x => x.Value.CssClass.ImageUrl == image.ImageUrl);

            if (sprite.Value != null)
            {
                image.IsSprite = true;
                sprite.Value.CssClass.IsSprite = true;
            }
            try
            {
                spritedImage          = SpriteContainer.AddImage(image);
                spritedImage.Metadata = imageKey;
            }
            catch (Exception ex)
            {
                var message          = string.Format("There were errors reducing {0}", image.ImageUrl);
                var wrappedException =
                    new ApplicationException(message, ex);
                RRTracer.Trace(message);
                RRTracer.Trace(ex.ToString());
                if (Registry.CaptureErrorAction != null)
                {
                    Registry.CaptureErrorAction(wrappedException);
                }
                return;
            }
            SpriteList.Add(new KeyValuePair <ImageMetadata, SpritedImage>(imageKey, spritedImage));
            if (SpriteContainer.Size >= config.SpriteSizeLimit || (SpriteContainer.Colors >= config.SpriteColorLimit && !config.ImageQuantizationDisabled && !config.ImageOptimizationDisabled))
            {
                Flush();
            }
        }
コード例 #2
0
 public virtual void Add(BackgroundImageClass image)
 {
     var imageKey = new ImageMetadata(image);
     if (RRContainer.Current.GetAllInstances<IFilter>().Where(x => x is SpriteFilter).FirstOrDefault(y => y.IgnoreTarget(new SpriteFilterContext(image))) != null)
         return;
     if (SpriteList.Any(x => x.Key.Equals(imageKey)))
     {
         var originalImage = SpriteList.First(x => x.Key.Equals(imageKey)).Value;
         var clonedImage = new SpritedImage(originalImage.AverageColor, image, originalImage.Image) { Position = originalImage.Position, Url = originalImage.Url, Metadata = imageKey };
         //SpriteContainer.AddImage(clonedImage);
         SpriteList.Add(new KeyValuePair<ImageMetadata, SpritedImage>(imageKey, clonedImage));
         return;
     }
     SpritedImage spritedImage;
     var sprite = SpriteList.FirstOrDefault(x => x.Value.CssClass.ImageUrl == image.ImageUrl);
     if(sprite.Value != null)
     {
         image.IsSprite = true;
         sprite.Value.CssClass.IsSprite = true;
     }
     try
     {
         spritedImage = SpriteContainer.AddImage(image);
         spritedImage.Metadata = imageKey;
     }
     catch (Exception ex)
     {
         var message = string.Format("There were errors reducing {0}", image.ImageUrl);
         var wrappedException =
             new ApplicationException(message, ex);
         RRTracer.Trace(message);
         RRTracer.Trace(ex.ToString());
         if (Registry.CaptureErrorAction != null)
             Registry.CaptureErrorAction(wrappedException);
         return;
     }
     SpriteList.Add(new KeyValuePair<ImageMetadata, SpritedImage>(imageKey, spritedImage));
     if (SpriteContainer.Size >= config.SpriteSizeLimit || (SpriteContainer.Colors >= config.SpriteColorLimit && !config.ImageQuantizationDisabled && !config.ImageOptimizationDisabled))
         Flush();
 }
コード例 #3
0
        public SpritedImage AddImage (BackgroundImageClass image)
        {
            byte[] imageBytes;
            if(DownloadedImages.ContainsKey(image.ImageUrl) && image.IsSprite)
                imageBytes = DownloadedImages[image.ImageUrl];
            else
            {
                imageBytes = webClientWrapper.DownloadBytes(image.ImageUrl);
                if(image.IsSprite)
                    DownloadedImages.Add(image.ImageUrl, imageBytes);
            }
            Bitmap bitmap;

            using (var originalBitmap = new Bitmap(new MemoryStream(imageBytes)))
            {
                var width = image.Width ?? originalBitmap.Width; //cliped width of original image
                var height = image.Height ?? originalBitmap.Height;
                using (var writer = new SpriteWriter(width, height))
                {
                    var x = image.XOffset.Offset < 0 ? Math.Abs(image.XOffset.Offset) : 0; //offset on original
                    var y = image.YOffset.Offset < 0 ? Math.Abs(image.YOffset.Offset) : 0;
                    var imageWidth = width; //canvas width
                    if (width + x > originalBitmap.Width)   
                        width = originalBitmap.Width - x;
                    var imageHeight = height;
                    if (height + y > originalBitmap.Height)
                        height = originalBitmap.Height - y;
                    var offsetX = 0;
                    var offsetY = 0;
                    if (image.XOffset.PositionMode == PositionMode.Direction)
                    {
                        switch (image.XOffset.Direction)
                        {
                            case Direction.Left:
                                image.XOffset = new Position { PositionMode = PositionMode.Percent, Offset = 0};
                                break;
                            case Direction.Center:
                                image.XOffset = new Position { PositionMode = PositionMode.Percent, Offset = 50};
                                break;
                            case Direction.Right:
                                image.XOffset = new Position { PositionMode = PositionMode.Percent, Offset = 100 };
                                break;
                        }
                    }
                    if (image.YOffset.PositionMode == PositionMode.Direction)
                        switch (image.YOffset.Direction)
                        {
                            case Direction.Top:
                                image.YOffset = new Position { PositionMode = PositionMode.Percent, Offset = 0 };
                                break;
                            case Direction.Center:
                                image.YOffset = new Position { PositionMode = PositionMode.Percent, Offset = 50 };
                                break;
                            case Direction.Bottom:
                                image.YOffset = new Position { PositionMode = PositionMode.Percent, Offset = 100 };
                                break;
                        }
                    if (image.XOffset.PositionMode == PositionMode.Percent)
                    {
                        if (originalBitmap.Width > imageWidth)
                            x = (int)Math.Round((originalBitmap.Width - imageWidth) * (image.XOffset.Offset / 100f), 0);
                        else
                            offsetX = (int)Math.Round((imageWidth - originalBitmap.Width) * (image.XOffset.Offset / 100f), 0);
                    }
                    else if (image.XOffset.PositionMode == PositionMode.Unit && image.XOffset.Offset > 0)
                    {
                        offsetX = image.XOffset.Offset;
                        if (originalBitmap.Width + offsetX > imageWidth)
                            width = imageWidth - offsetX;
                    }

                    if (image.YOffset.PositionMode == PositionMode.Percent)
                    {
                        if (originalBitmap.Height > imageHeight)
                            y = (int)Math.Round((originalBitmap.Height - height) * (image.YOffset.Offset / 100f), 0);
                        else
                            offsetY = (int)Math.Round((imageHeight - originalBitmap.Height) * (image.YOffset.Offset / 100f), 0);
                    }

                    try
                    {
                        using (var bm = originalBitmap.Clone(new Rectangle(x, y, width, height), originalBitmap.PixelFormat))
                        {
                            writer.WriteImage(bm, offsetX, offsetY);
                        }
                    }
                    catch (OutOfMemoryException)
                    {
                        throw new InvalidOperationException(string.Format("Unable to Add {0} to Sprite.", image.ImageUrl));
                    }
                    bitmap = writer.SpriteImage;
                    if ((originalBitmap.Width * originalBitmap.Height) > (bitmap.Width * bitmap.Height))
                        Size += writer.GetBytes("image/png").Length;
                    else
                        Size += imageBytes.Length;
                }
            }
            var avgColor = rrConfiguration.IsFullTrust ? GetColors(bitmap) : 0;
            var spritedImage = new SpritedImage(avgColor, image, bitmap);
            Images.Add(spritedImage);
            Width += bitmap.Width + 1;
            if (Height < bitmap.Height) Height = bitmap.Height;
            return spritedImage;
        }
コード例 #4
0
 public void AddImage (SpritedImage image)
 {
     Images.Add(image);
 }
コード例 #5
0
 public string InjectSprite(string originalCss, SpritedImage image)
 {
     return(originalCss.Replace(image.CssClass.OriginalClassString, image.Render()));
 }
コード例 #6
0
        public SpritedImage AddImage(BackgroundImageClass image)
        {
            var imageBytes = webClientWrapper.DownloadBytes(image.ImageUrl);
            Bitmap bitmap;

            using (var originalBitmap = new Bitmap(new MemoryStream(imageBytes)))
            {
                using (var writer = new SpriteWriter(image.Width ?? originalBitmap.Width, image.Height ?? originalBitmap.Height))
                {
                    var x = image.XOffset.Offset < 0 ? Math.Abs(image.XOffset.Offset) : 0;
                    var y = image.YOffset.Offset < 0 ? Math.Abs(image.YOffset.Offset) : 0;
                    var width = image.Width ?? originalBitmap.Width;
                    if (width + x > originalBitmap.Width)
                        width = originalBitmap.Width - x;
                    var height = image.Height ?? originalBitmap.Height;
                    if (height + y > originalBitmap.Height)
                        height = originalBitmap.Height - y;
                    var offsetX = 0;
                    var offsetY = 0;
                    if (image.XOffset.Direction == Direction.Right)
                    {
                        if (originalBitmap.Width > width)
                            x = originalBitmap.Width - width;
                        else
                            offsetX = (image.Width ?? 0) - originalBitmap.Width;
                    }
                    else if (image.XOffset.Direction == Direction.Center && image.XOffset.PositionMode == PositionMode.Direction)
                    {
                        if (originalBitmap.Width > width)
                            x = (originalBitmap.Width - width)/2;
                        else
                            offsetX = ((image.Width ?? 0) - originalBitmap.Width)/2;
                    }
                    if (image.YOffset.Direction == Direction.Bottom)
                    {
                        if (originalBitmap.Height > height)
                            y = originalBitmap.Height - height;
                        else
                            offsetY = (image.Height ?? 0) - originalBitmap.Height;
                    }
                    else if (image.YOffset.Direction == Direction.Center && image.YOffset.PositionMode == PositionMode.Direction)
                    {
                        if (originalBitmap.Height > height)
                            y = (originalBitmap.Height - height) / 2;
                        else
                            offsetY = ((image.Height ?? 0) - originalBitmap.Height) / 2;
                    }

                    try
                    {
                        using (var bm = originalBitmap.Clone(new Rectangle(x, y, width, height), originalBitmap.PixelFormat))
                        {
                            writer.WriteImage(bm, offsetX, offsetY);
                        }
                    }
                    catch (OutOfMemoryException)
                    {
                        throw new InvalidOperationException(string.Format("Unable to Add {0} to Sprite.", image.OriginalImageUrl));
                    }
                    bitmap = writer.SpriteImage;
                    if ((originalBitmap.Width * originalBitmap.Height) > (bitmap.Width * bitmap.Height))
                        Size += writer.GetBytes("image/png").Length;
                    else
                        Size += imageBytes.Length;
                }
            }
            var avgColor = rrConfiguration.IsFullTrust ? GetColors(bitmap) : 0;
            var spritedImage = new SpritedImage(avgColor, image, bitmap);
            images.Add(spritedImage);
            Width += bitmap.Width + 1;
            if (Height < bitmap.Height) Height = bitmap.Height;
            return spritedImage;
        }
コード例 #7
0
        public SpritedImage AddImage(BackgroundImageClass image)
        {
            var    imageBytes = webClientWrapper.DownloadBytes(image.ImageUrl);
            Bitmap bitmap;

            using (var originalBitmap = new Bitmap(new MemoryStream(imageBytes)))
            {
                using (var writer = new SpriteWriter(image.Width ?? originalBitmap.Width, image.Height ?? originalBitmap.Height))
                {
                    var x     = image.XOffset.Offset < 0 ? Math.Abs(image.XOffset.Offset) : 0;
                    var y     = image.YOffset.Offset < 0 ? Math.Abs(image.YOffset.Offset) : 0;
                    var width = image.Width ?? originalBitmap.Width;
                    if (width + x > originalBitmap.Width)
                    {
                        width = originalBitmap.Width - x;
                    }
                    var height = image.Height ?? originalBitmap.Height;
                    if (height + y > originalBitmap.Height)
                    {
                        height = originalBitmap.Height - y;
                    }
                    var offsetX = 0;
                    var offsetY = 0;
                    if (image.XOffset.Direction == Direction.Right)
                    {
                        if (originalBitmap.Width > width)
                        {
                            x = originalBitmap.Width - width;
                        }
                        else
                        {
                            offsetX = (image.Width ?? 0) - originalBitmap.Width;
                        }
                    }
                    else if (image.XOffset.Direction == Direction.Center && image.XOffset.PositionMode == PositionMode.Direction)
                    {
                        if (originalBitmap.Width > width)
                        {
                            x = (originalBitmap.Width - width) / 2;
                        }
                        else
                        {
                            offsetX = ((image.Width ?? 0) - originalBitmap.Width) / 2;
                        }
                    }
                    if (image.YOffset.Direction == Direction.Bottom)
                    {
                        if (originalBitmap.Height > height)
                        {
                            y = originalBitmap.Height - height;
                        }
                        else
                        {
                            offsetY = (image.Height ?? 0) - originalBitmap.Height;
                        }
                    }
                    else if (image.YOffset.Direction == Direction.Center && image.YOffset.PositionMode == PositionMode.Direction)
                    {
                        if (originalBitmap.Height > height)
                        {
                            y = (originalBitmap.Height - height) / 2;
                        }
                        else
                        {
                            offsetY = ((image.Height ?? 0) - originalBitmap.Height) / 2;
                        }
                    }

                    try
                    {
                        using (var bm = originalBitmap.Clone(new Rectangle(x, y, width, height), originalBitmap.PixelFormat))
                        {
                            writer.WriteImage(bm, offsetX, offsetY);
                        }
                    }
                    catch (OutOfMemoryException)
                    {
                        throw new InvalidOperationException(string.Format("Unable to Add {0} to Sprite.", image.OriginalImageUrl));
                    }
                    bitmap = writer.SpriteImage;
                    if ((originalBitmap.Width * originalBitmap.Height) > (bitmap.Width * bitmap.Height))
                    {
                        Size += writer.GetBytes("image/png").Length;
                    }
                    else
                    {
                        Size += imageBytes.Length;
                    }
                }
            }
            var avgColor     = rrConfiguration.IsFullTrust ? GetColors(bitmap) : 0;
            var spritedImage = new SpritedImage(avgColor, image, bitmap);

            images.Add(spritedImage);
            Width += bitmap.Width + 1;
            if (Height < bitmap.Height)
            {
                Height = bitmap.Height;
            }
            return(spritedImage);
        }
コード例 #8
0
 public string InjectSprite(string originalCss, SpritedImage image)
 {
     return originalCss.Replace(image.CssClass.OriginalClassString, image.Render());
 }
コード例 #9
0
        public SpritedImage AddImage(BackgroundImageClass image)
        {
            byte[] imageBytes;
            if (DownloadedImages.ContainsKey(image.ImageUrl) && image.IsSprite)
            {
                imageBytes = DownloadedImages[image.ImageUrl];
            }
            else
            {
                imageBytes = webClientWrapper.DownloadBytes(image.ImageUrl);
                if (image.IsSprite)
                {
                    DownloadedImages.Add(image.ImageUrl, imageBytes);
                }
            }
            Bitmap bitmap;

            using (var originalBitmap = new Bitmap(new MemoryStream(imageBytes)))
            {
                var width  = image.Width ?? originalBitmap.Width; //cliped width of original image
                var height = image.Height ?? originalBitmap.Height;
                using (var writer = new SpriteWriter(width, height))
                {
                    var x          = image.XOffset.Offset < 0 ? Math.Abs(image.XOffset.Offset) : 0; //offset on original
                    var y          = image.YOffset.Offset < 0 ? Math.Abs(image.YOffset.Offset) : 0;
                    var imageWidth = width;                                                         //canvas width
                    if (width + x > originalBitmap.Width)
                    {
                        width = originalBitmap.Width - x;
                    }
                    var imageHeight = height;
                    if (height + y > originalBitmap.Height)
                    {
                        height = originalBitmap.Height - y;
                    }
                    var offsetX = 0;
                    var offsetY = 0;
                    if (image.XOffset.PositionMode == PositionMode.Direction)
                    {
                        switch (image.XOffset.Direction)
                        {
                        case Direction.Left:
                            image.XOffset = new Position {
                                PositionMode = PositionMode.Percent, Offset = 0
                            };
                            break;

                        case Direction.Center:
                            image.XOffset = new Position {
                                PositionMode = PositionMode.Percent, Offset = 50
                            };
                            break;

                        case Direction.Right:
                            image.XOffset = new Position {
                                PositionMode = PositionMode.Percent, Offset = 100
                            };
                            break;
                        }
                    }
                    if (image.YOffset.PositionMode == PositionMode.Direction)
                    {
                        switch (image.YOffset.Direction)
                        {
                        case Direction.Top:
                            image.YOffset = new Position {
                                PositionMode = PositionMode.Percent, Offset = 0
                            };
                            break;

                        case Direction.Center:
                            image.YOffset = new Position {
                                PositionMode = PositionMode.Percent, Offset = 50
                            };
                            break;

                        case Direction.Bottom:
                            image.YOffset = new Position {
                                PositionMode = PositionMode.Percent, Offset = 100
                            };
                            break;
                        }
                    }
                    if (image.XOffset.PositionMode == PositionMode.Percent)
                    {
                        if (originalBitmap.Width > imageWidth)
                        {
                            x = (int)Math.Round((originalBitmap.Width - imageWidth) * (image.XOffset.Offset / 100f), 0);
                        }
                        else
                        {
                            offsetX = (int)Math.Round((imageWidth - originalBitmap.Width) * (image.XOffset.Offset / 100f), 0);
                        }
                    }
                    else if (image.XOffset.PositionMode == PositionMode.Unit && image.XOffset.Offset > 0)
                    {
                        offsetX = image.XOffset.Offset;
                        if (originalBitmap.Width + offsetX > imageWidth)
                        {
                            width = imageWidth - offsetX;
                        }
                    }

                    if (image.YOffset.PositionMode == PositionMode.Percent)
                    {
                        if (originalBitmap.Height > imageHeight)
                        {
                            y = (int)Math.Round((originalBitmap.Height - height) * (image.YOffset.Offset / 100f), 0);
                        }
                        else
                        {
                            offsetY = (int)Math.Round((imageHeight - originalBitmap.Height) * (image.YOffset.Offset / 100f), 0);
                        }
                    }

                    try
                    {
                        using (var bm = originalBitmap.Clone(new Rectangle(x, y, width, height), originalBitmap.PixelFormat))
                        {
                            writer.WriteImage(bm, offsetX, offsetY);
                        }
                    }
                    catch (OutOfMemoryException)
                    {
                        throw new InvalidOperationException(string.Format("Unable to Add {0} to Sprite.", image.ImageUrl));
                    }
                    bitmap = writer.SpriteImage;
                    if ((originalBitmap.Width * originalBitmap.Height) > (bitmap.Width * bitmap.Height))
                    {
                        Size += writer.GetBytes("image/png").Length;
                    }
                    else
                    {
                        Size += imageBytes.Length;
                    }
                }
            }
            var avgColor     = rrConfiguration.IsFullTrust ? GetColors(bitmap) : 0;
            var spritedImage = new SpritedImage(avgColor, image, bitmap);

            Images.Add(spritedImage);
            Width += bitmap.Width + 1;
            if (Height < bitmap.Height)
            {
                Height = bitmap.Height;
            }
            return(spritedImage);
        }
コード例 #10
0
 public void AddImage(SpritedImage image)
 {
     Images.Add(image);
 }