예제 #1
1
        public void AddImage(BackgroundImageClass image)
        {
            var imageBytes = webClientWrapper.DownloadBytes(image.ImageUrl);
            Bitmap bitmap = null;
            using (var originalBitmap = new Bitmap(new MemoryStream(imageBytes)))
            {
                using (var writer = new SpriteWriter(image.Width ?? originalBitmap.Width, image.Height ?? originalBitmap.Height))
                {
                    var width = image.Width ?? originalBitmap.Width;
                    if (width > originalBitmap.Width)
                        width = originalBitmap.Width;
                    var height = image.Height ?? originalBitmap.Height;
                    if (height > originalBitmap.Height)
                        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;

                    writer.WriteImage(originalBitmap.Clone(new Rectangle(x, y, width, height), originalBitmap.PixelFormat));
                    bitmap = writer.SpriteImage;
                    if ((originalBitmap.Width * originalBitmap.Height) > (bitmap.Width * bitmap.Height))
                        Size += writer.GetBytes("image/png").Length;
                    else
                        Size += imageBytes.Length;
                }
            }
            images.Add(bitmap);
            Width += bitmap.Width;
            if (Height < bitmap.Height) Height = bitmap.Height;
        }
예제 #2
0
 public virtual void Flush()
 {
     if (SpriteContainer.Size > 0)
     {
         RRTracer.Trace("Beginning to Flush sprite");
         using (var spriteWriter = new SpriteWriter(SpriteContainer.Width, SpriteContainer.Height))
         {
             var offset = 0;
             foreach (var image in SpriteContainer)
             {
                 spriteWriter.WriteImage(image.Image);
                 image.Position = offset;
                 offset        += image.Image.Width + 1;
             }
             var    bytes = spriteWriter.GetBytes("image/png");
             byte[] optBytes;
             try
             {
                 optBytes = (config.ImageOptimizationDisabled || !config.IsFullTrust) ? bytes : pngOptimizer.OptimizePng(bytes, config.ImageOptimizationCompressionLevel, config.ImageQuantizationDisabled);
             }
             catch (OptimizationException optEx)
             {
                 optBytes = bytes;
                 RRTracer.Trace(string.Format("Errors optimizing {0}. Received Error: {1}", SpritedCssKey, optEx.Message));
                 if (Registry.CaptureErrorAction != null)
                 {
                     Registry.CaptureErrorAction(optEx);
                 }
             }
             var url = GetSpriteUrl(optBytes);
             store.Save(optBytes, url, null);
             foreach (var image in SpriteContainer)
             {
                 image.Url = url;
                 foreach (var dupImage in SpriteList)
                 {
                     if (dupImage.Key.Equals(image.Metadata) && dupImage.Value.Position == -1)
                     {
                         dupImage.Value.Position = image.Position;
                         dupImage.Value.Url      = image.Url;
                     }
                 }
             }
         }
         RRTracer.Trace("Finished Flushing sprite");
     }
     SpriteContainer = new SpriteContainer(webClientWrapper, config);
 }
예제 #3
0
 public virtual void Flush()
 {
     if(SpriteContainer.Size > 0)
     {
         RRTracer.Trace("Beginning to Flush sprite");
         using (var spriteWriter = new SpriteWriter(SpriteContainer.Width, SpriteContainer.Height))
         {
             var offset = 0;
             foreach (var image in SpriteContainer)
             {
                 spriteWriter.WriteImage(image.Image);
                 image.Position = offset;
                 offset += image.Image.Width + 1;
             }
             var bytes = spriteWriter.GetBytes("image/png");
             byte[] optBytes;
             try
             {
                 optBytes = (config.ImageOptimizationDisabled || !config.IsFullTrust) ? bytes : pngOptimizer.OptimizePng(bytes, config.ImageOptimizationCompressionLevel, config.ImageQuantizationDisabled);
             }
             catch (OptimizationException optEx)
             {
                 optBytes = bytes;
                 RRTracer.Trace(string.Format("Errors optimizing {0}. Received Error: {1}", SpritedCssKey, optEx.Message));
                 if (Registry.CaptureErrorAction != null)
                     Registry.CaptureErrorAction(optEx);
             }
             var url = GetSpriteUrl(optBytes);
             store.Save(optBytes, url, null);
             foreach (var image in SpriteContainer)
             {
                 image.Url = url;
                 foreach (var dupImage in SpriteList)
                 {
                     if (dupImage.Key.Equals(image.Metadata) && dupImage.Value.Position == -1)
                     {
                         dupImage.Value.Position = image.Position;
                         dupImage.Value.Url = image.Url;
                     }
                 }
             }
         }
         RRTracer.Trace("Finished Flushing sprite");
     }
     SpriteContainer = new SpriteContainer(webClientWrapper, config);
 }
예제 #4
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 width = image.Width ?? originalBitmap.Width;
                    if (width > originalBitmap.Width)
                        width = originalBitmap.Width;
                    var height = image.Height ?? originalBitmap.Height;
                    if (height > originalBitmap.Height)
                        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;

                    try
                    {
                        using (var bm = originalBitmap.Clone(new Rectangle(x, y, width, height), originalBitmap.PixelFormat))
                        {
                            writer.WriteImage(bm);
                        }
                    }
                    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;
        }
예제 #5
0
        public virtual void Flush()
        {
            if(SpriteContainer.Size > 0)
            {
                using (var spriteWriter = new SpriteWriter(SpriteContainer.Width, SpriteContainer.Height))
                {
                    foreach (var image in SpriteContainer)
                        spriteWriter.WriteImage(image);

                    var bytes = spriteWriter.GetBytes("image/png");
                    var url = GetSpriteUrl(bytes);
                    store.Save(bytes, url, null);
                    foreach (var sprite in spriteList.Values.Where(x => x.SpriteIndex == spriteIndex))
                        sprite.Url = url;
                }
                ++spriteIndex;
            }
            SpriteContainer.Dispose();
            SpriteContainer = new SpriteContainer(webClientWrapper);
            return;
        }
예제 #6
0
 public virtual void Flush()
 {
     if(SpriteContainer.Size > 0)
     {
         using (var spriteWriter = new SpriteWriter(SpriteContainer.Width, SpriteContainer.Height))
         {
             var offset = 0;
             foreach (var image in SpriteContainer)
             {
                 spriteWriter.WriteImage(image.Image);
                 image.Position = offset;
                 offset += image.Image.Width + 1;
             }
             var bytes = spriteWriter.GetBytes("image/png");
             byte[] optBytes = null;
             try
             {
                 optBytes = (config.ImageOptimizationDisabled || !config.IsFullTrust) ? bytes : pngOptimizer.OptimizePng(bytes, config.ImageOptimizationCompressionLevel, config.ImageQuantizationDisabled);
             }
             catch (OptimizationException optEx)
             {
                 optBytes = bytes;
                 RRTracer.Trace(string.Format("Errors optimizing {0}. Received Error: {1}", SpritedCssKey, optEx.Message));
                 if (RequestReduceModule.CaptureErrorAction != null)
                     RequestReduceModule.CaptureErrorAction(optEx);
             }
             var url = GetSpriteUrl(optBytes);
             store.Save(optBytes, url, null);
             foreach (var image in SpriteContainer)
                 image.Url = url;
         }
     }
     SpriteContainer.Dispose();
     SpriteContainer = new SpriteContainer(webClientWrapper, config);
     return;
 }
예제 #7
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;
        }
예제 #8
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);
        }
예제 #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);
        }