Exemplo n.º 1
0
 public virtual IList <Sprite> Flush()
 {
     if (SpriteContainer.Size > 0)
     {
         Tracer.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;
                 var message = string.Format("Errors optimizing. Received Error: {0}", optEx.Message);
                 Tracer.Trace(message);
                 Errors.Add(new SpriteException(message, optEx));
             }
             var url = generateSpriteUrl(optBytes);
             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;
                     }
                 }
             }
             finalSprites.Add(new Sprite(url, optBytes));
         }
         Tracer.Trace("Finished Flushing sprite");
     }
     SpriteContainer = new SpriteContainer(ImageLoader, config);
     return(finalSprites);
 }
Exemplo n.º 2
0
        public SpritedImage AddImage(BackgroundImageClass image)
        {
            byte[] imageBytes;
            if (DownloadedImages.ContainsKey(image.ImageUrl) && image.IsSprite)
            {
                imageBytes = DownloadedImages[image.ImageUrl];
            }
            else
            {
                imageBytes = ImageLoader.GetImageBytes(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     = settings.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);
        }
Exemplo n.º 3
0
        public SpritedImage AddImage (BackgroundImageClass image)
        {
            byte[] imageBytes;
            if(DownloadedImages.ContainsKey(image.ImageUrl) && image.IsSprite)
                imageBytes = DownloadedImages[image.ImageUrl];
            else
            {
                imageBytes = ImageLoader.GetImageBytes(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 = settings.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;
        }
Exemplo n.º 4
0
 public virtual IList<Sprite> Flush()
 {
     if(SpriteContainer.Size > 0)
     {
         Tracer.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;
                 var message = string.Format("Errors optimizing. Received Error: {0}", optEx.Message);
                 Tracer.Trace(message);
                 Errors.Add(new SpriteException(message, optEx));
             }
             var url = generateSpriteUrl(optBytes);
             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;
                     }
                 }
             }
             finalSprites.Add(new Sprite(url, optBytes));
         }
         Tracer.Trace("Finished Flushing sprite");
     }
     SpriteContainer = new SpriteContainer(ImageLoader, config);
     return finalSprites;
 }