Exemple #1
0
        /// <summary>
        /// Returns a rectangle representing the given ring - if it is an axis-parallel rectangle. Otherwise returns null;
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public RectangleF?GetRingAsRectF(string name)
        {
            var poly  = this[name];
            var rect  = PolygonMath.GetBoundingBox(poly);
            var poly2 = PolygonMath.ToPoly(rect);

            return(PolygonMath.ArraysEqual(poly, poly2) ? (RectangleF?)rect : null);
        }
Exemple #2
0
        /// <summary>
        /// Gets a bounding box that encloses all rings that don't have ExcludeFromBoundingBox set.
        /// </summary>
        /// <returns></returns>
        public RectangleF GetBoundingBox()
        {
            List <PointF> points = new List <PointF>(ring.Count * 5);

            foreach (PointSet val in ringList)
            {
                if (val.flags == PointFlags.Ring)
                {
                    points.AddRange(val.points);
                }
            }
            return(PolygonMath.GetBoundingBox(points.ToArray()));
        }
        /// <summary>
        /// Copies layout information from the given image state to the current instance.
        /// Does not populate message or 'features' variables
        /// </summary>
        /// <param name="s"></param>
        public void PopulateFrom(ImageState s)
        {
            this.ow    = s.originalSize.Width;
            this.oh    = s.originalSize.Height;
            this.cropx = s.copyRect.X;
            this.cropy = s.copyRect.Y;
            this.cropw = s.copyRect.Width;
            this.croph = s.copyRect.Height;
            RectangleF dest = PolygonMath.GetBoundingBox(s.layout["image"]);

            this.dx = dest.X;
            this.dy = dest.Y;
            this.dw = dest.Width;
            this.dh = dest.Height;
        }
Exemple #4
0
        protected override RequestedAction PreFlushChanges(ImageState s)
        {
            if (s.destGraphics == null)
            {
                return(RequestedAction.None);
            }


            Interlocked.Increment(ref requestCount); //Track request count

            string             mode = c.get("trial.watermarkMode", "After500");
            TrialWatermarkMode m    = TrialWatermarkMode.After500;

            if ("always".Equals(mode, StringComparison.OrdinalIgnoreCase))
            {
                m = TrialWatermarkMode.Always;
            }
            if ("randomly".Equals(mode, StringComparison.OrdinalIgnoreCase))
            {
                m = TrialWatermarkMode.Randomly;
            }

            bool applyWatermark = (m == TrialWatermarkMode.Always);

            if (m == TrialWatermarkMode.After500 && requestCount > 500)
            {
                applyWatermark = true;
            }
            if (m == TrialWatermarkMode.Randomly)
            {
                applyWatermark = (new Random(requestCount).Next(0, 41) < 10);                                   //25% chance
            }
            if (!applyWatermark)
            {
                return(RequestedAction.None);
            }

            DrawString(PolygonMath.GetBoundingBox(s.layout["image"]), s.destGraphics, "Unlicensed", FontFamily.GenericSansSerif, Color.FromArgb(70, Color.White));



            return(RequestedAction.None);
        }
Exemple #5
0
        public PolyRect(PointF[] points)
        {
            var rect = PolygonMath.GetBoundingBox(points);

            this.SetBounds(rect.X, rect.Y, rect.Width, rect.Height);

            // PolygonMath.IsUnrotated can tell us that the points follow a
            // particular pattern, but in order to represent 90-degree rotations
            // and flips, we want to consider them to be *non-rectangle* points.
            // Therefore, we use a much more strict definition: there can only
            // be 4 points, and they must be in the canonical order.
            var right  = this.x + this.width;
            var bottom = this.y + this.height;

            this.rect = points.Length == 4 &&
                        points[0].X == this.x && points[0].Y == this.y &&
                        points[1].X == right && points[1].Y == this.y &&
                        points[2].X == right && points[2].Y == bottom &&
                        points[3].X == this.x && points[3].Y == bottom;

            this.points = points.Select(p => new float[] { p.X, p.Y }).ToArray();
        }
        protected override RequestedAction LayoutImage(ImageState s)
        {
            //Only activated if both width and height are specified, and mode=crop.
            if (s.settings.Mode != FitMode.Crop || s.settings.Width < 0 || s.settings.Height < 0)
            {
                return(RequestedAction.None);
            }

            //Calculate bounding box for all coordinates specified.
            double[] focus = NameValueCollectionExtensions.GetList <double>(s.settings, "c.focus", null, 2, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72);
            if (focus == null)
            {
                return(RequestedAction.None);
            }
            RectangleF box = PolygonMath.GetBoundingBox(focus);

            var bounds = new RectangleF(new PointF(0, 0), s.originalSize);

            //Clip box to original image bounds
            box = PolygonMath.ClipRectangle(box, bounds);

            var targetSize = new SizeF(s.settings.Width, s.settings.Height);

            SizeF copySize;

            //Now, we can either crop as closely as possible or as loosely as possible.
            if (NameValueCollectionExtensions.Get <bool>(s.settings, "c.zoom", false) && box.Width > 0 && box.Height > 0)
            {
                //Crop close
                copySize = PolygonMath.ScaleOutside(box.Size, targetSize);
            }
            else
            {
                //Crop minimally
                copySize = PolygonMath.ScaleInside(targetSize, bounds.Size);
                //Ensure it's outside the box
                if (!PolygonMath.FitsInside(box.Size, copySize))
                {
                    copySize = PolygonMath.ScaleOutside(box.Size, copySize);
                }
            }
            //Clip to bounds.
            box = PolygonMath.ClipRectangle(PolygonMath.ExpandTo(box, copySize), bounds);

            s.copyRect = box;

            ///What is the vertical and horizontal aspect ratio different in result pixels?
            var padding = PolygonMath.ScaleInside(box.Size, targetSize);

            padding = new SizeF(targetSize.Width - padding.Width, targetSize.Height - padding.Height);


            //So, if we haven't met the aspect ratio yet, what mode will we pass on?
            var finalmode = NameValueCollectionExtensions.Get <FitMode>(s.settings, "c.finalmode", FitMode.Pad);

            //Crop off 1 or 2 pixels instead of padding without worrying too much
            if (finalmode == FitMode.Pad && padding.Width + padding.Height < 3)
            {
                finalmode = FitMode.Crop;
            }

            s.settings.Mode = finalmode;

            return(RequestedAction.None);
        }
Exemple #7
0
        protected override RequestedAction RenderImage(ImageState s)
        {
            //Skip this when we are doing simulations
            if (s.destGraphics == null)
            {
                return(RequestedAction.None);
            }

            //If there's pre-rendering involved this optimization is utterly pointless.
            if (s.preRenderBitmap != null)
            {
                return(RequestedAction.None);
            }

            //Find out what the speed setting is.
            int speed = 0;

            if (string.IsNullOrEmpty(s.settings["speed"]) || !int.TryParse(s.settings["speed"], NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out speed))
            {
                speed = 0;
            }

            if (speed < 1)
            {
                return(RequestedAction.None);
            }

            s.destGraphics.CompositingMode    = CompositingMode.SourceCopy;
            s.destGraphics.CompositingQuality = CompositingQuality.HighSpeed;
            if (speed == 1)
            {
                s.destGraphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            }
            else
            {
                s.destGraphics.InterpolationMode = InterpolationMode.Bilinear;
            }

            s.destGraphics.PixelOffsetMode = PixelOffsetMode.HighSpeed;
            s.destGraphics.SmoothingMode   = SmoothingMode.HighSpeed;

            s.copyAttibutes.SetWrapMode(WrapMode.TileFlipXY);

            if (speed < 3)
            {
                s.destGraphics.DrawImage(s.sourceBitmap, PolygonMath.getParallelogram(s.layout["image"]), s.copyRect, GraphicsUnit.Pixel, s.copyAttibutes);
            }
            else if (speed < 4)
            {
                Rectangle midsize = PolygonMath.ToRectangle(PolygonMath.GetBoundingBox(s.layout["image"]));

                using (Image thumb = s.sourceBitmap.GetThumbnailImage(midsize.Width, midsize.Height, delegate() { return(false); }, IntPtr.Zero)) {
                    double     xfactor  = (double)thumb.Width / (double)s.sourceBitmap.Width;
                    double     yfactor  = (double)thumb.Height / (double)s.sourceBitmap.Height;
                    RectangleF copyPart = new RectangleF((float)(s.copyRect.Left * xfactor),
                                                         (float)(s.copyRect.Top * yfactor),
                                                         (float)(s.copyRect.Width * xfactor),
                                                         (float)(s.copyRect.Height * yfactor));
                    if (Math.Floor(copyPart.Height) == thumb.Height || Math.Ceiling(copyPart.Height) == thumb.Height)
                    {
                        copyPart.Height = thumb.Height;
                    }
                    if (Math.Floor(copyPart.Width) == thumb.Width || Math.Ceiling(copyPart.Width) == thumb.Width)
                    {
                        copyPart.Width = thumb.Width;
                    }
                    s.destGraphics.DrawImage(thumb, PolygonMath.getParallelogram(s.layout["image"]), copyPart, GraphicsUnit.Pixel, s.copyAttibutes);
                }
            }
            else
            {
                RectangleF box = PolygonMath.GetBoundingBox(PolygonMath.getParallelogram(s.layout["image"]));
                s.destGraphics.CompositingMode = CompositingMode.SourceCopy;
                s.destGraphics.DrawImage(s.sourceBitmap, box.Left, box.Top, box.Width, box.Height);
            }

            return(RequestedAction.Cancel);
        }
        protected bool LegacyDrawWatermark(ImageState s)
        {
            Graphics g = s.destGraphics;

            if (g == null)
            {
                return(false);
            }

            string watermark;

            if (!LegacyParseWatermark(s.settings, out watermark))
            {
                return(false);
            }

            RectangleF imageBox = PolygonMath.GetBoundingBox(s.layout["image"]);

            //Floor and ceiling values to prevent fractional placement.
            imageBox.Width  = (float)Math.Floor(imageBox.Width);
            imageBox.Height = (float)Math.Floor(imageBox.Height);
            imageBox.X      = (float)Math.Ceiling(imageBox.X);
            imageBox.Y      = (float)Math.Ceiling(imageBox.Y);

            SizeF watermarkSize      = this.watermarkSize;
            SizeF topLeftPadding     = this.topLeftPadding;
            SizeF bottomRightPadding = this.bottomRightPadding;

            //Load the file specified in the querystring,
            Bitmap wb = GetMemCachedBitmap(watermark);

            lock (wb) {
                //If percentages, resolve to pixels
                if (valuesPercentages)
                {
                    //Force into 0..1 range, inclusive.
                    watermarkSize.Height      = Math.Max(0, Math.Min(1, watermarkSize.Height));
                    watermarkSize.Width       = Math.Max(0, Math.Min(1, watermarkSize.Width));
                    topLeftPadding.Height     = Math.Max(0, Math.Min(1, topLeftPadding.Height));
                    topLeftPadding.Width      = Math.Max(0, Math.Min(1, topLeftPadding.Width));
                    bottomRightPadding.Height = Math.Max(0, Math.Min(1, bottomRightPadding.Height));
                    bottomRightPadding.Width  = Math.Max(0, Math.Min(1, bottomRightPadding.Width));

                    //Make sure everything adds up to 1
                    double totalWidth = watermarkSize.Width + topLeftPadding.Width + bottomRightPadding.Width;
                    if (totalWidth > 1)
                    {
                        totalWidth                = 1 / totalWidth; //Turn it into the factor we have to multiple by to make everything fit.
                        watermarkSize.Width      *= (float)totalWidth;
                        topLeftPadding.Width     *= (float)totalWidth;
                        bottomRightPadding.Width *= (float)totalWidth;
                    }
                    double totalHeight = watermarkSize.Height + topLeftPadding.Height + bottomRightPadding.Height;
                    if (totalHeight > 1)
                    {
                        totalHeight                = 1 / totalHeight; //Turn it into the factor we have to multiply by to make everything fit.
                        watermarkSize.Height      *= (float)totalHeight;
                        topLeftPadding.Height     *= (float)totalHeight;
                        bottomRightPadding.Height *= (float)totalHeight;
                    }

                    //Now, we can resolve the percentages to pixels.
                    watermarkSize.Height      *= imageBox.Height;
                    watermarkSize.Width       *= imageBox.Width;
                    topLeftPadding.Height     *= imageBox.Height;
                    topLeftPadding.Width      *= imageBox.Width;
                    bottomRightPadding.Height *= imageBox.Height;
                    bottomRightPadding.Width  *= imageBox.Width;
                }

                //Keep aspect ratio, shrinking further if needed.
                if (keepAspectRatio)
                {
                    watermarkSize = PolygonMath.DownScaleInside(wb.Size, watermarkSize);
                }


                //Floor all values to avoid rounding errors and blurry lines.
                watermarkSize      = new SizeF((float)Math.Floor(watermarkSize.Width), (float)Math.Floor(watermarkSize.Height));
                topLeftPadding     = new SizeF((float)Math.Floor(topLeftPadding.Width), (float)Math.Floor(topLeftPadding.Height));
                bottomRightPadding = new SizeF((float)Math.Floor(bottomRightPadding.Width), (float)Math.Floor(bottomRightPadding.Height));


                //Check boundingbox
                SizeF watermarkBoundingBox = new SizeF(watermarkSize.Width + topLeftPadding.Width + bottomRightPadding.Width,
                                                       watermarkSize.Height + topLeftPadding.Height + bottomRightPadding.Height);

                //Don't draw the watermark if it is too small.
                if (!PolygonMath.FitsInside(watermarkBoundingBox, imageBox.Size))
                {
                    if (hideIfTooSmall)
                    {
                        return(true);
                    }
                    else
                    {
                        SizeF oldSize = watermarkBoundingBox;
                        watermarkBoundingBox  = PolygonMath.DownScaleInside(watermarkBoundingBox, imageBox.Size);
                        watermarkSize.Width  -= (oldSize.Width - watermarkBoundingBox.Width);
                        watermarkSize.Height -= (oldSize.Height - watermarkBoundingBox.Height);
                    }
                }
                //Floor all values again
                watermarkSize      = new SizeF((float)Math.Floor(watermarkSize.Width), (float)Math.Floor(watermarkSize.Height));
                topLeftPadding     = new SizeF((float)Math.Floor(topLeftPadding.Width), (float)Math.Floor(topLeftPadding.Height));
                bottomRightPadding = new SizeF((float)Math.Floor(bottomRightPadding.Width), (float)Math.Floor(bottomRightPadding.Height));



                float innerWidth  = (float)Math.Floor(imageBox.Width - Math.Abs(topLeftPadding.Width) - Math.Abs(bottomRightPadding.Width));
                float innerHeight = (float)Math.Floor(imageBox.Height - Math.Abs(topLeftPadding.Height) - Math.Abs(bottomRightPadding.Height));

                float x = 0;
                float y = 0;

                if (align == ContentAlignment.BottomCenter || align == ContentAlignment.BottomLeft || align == ContentAlignment.BottomRight)
                {
                    y = (innerHeight - watermarkSize.Height) + topLeftPadding.Height;
                }

                if (align == ContentAlignment.MiddleCenter || align == ContentAlignment.MiddleLeft || align == ContentAlignment.MiddleRight)
                {
                    y = (innerHeight - watermarkSize.Height) / 2 + topLeftPadding.Height;
                }

                if (align == ContentAlignment.TopCenter || align == ContentAlignment.TopLeft || align == ContentAlignment.TopRight)
                {
                    y = topLeftPadding.Height;
                }


                if (align == ContentAlignment.BottomRight || align == ContentAlignment.MiddleRight || align == ContentAlignment.TopRight)
                {
                    x = (innerWidth - watermarkSize.Width) + topLeftPadding.Width;
                }

                if (align == ContentAlignment.BottomCenter || align == ContentAlignment.MiddleCenter || align == ContentAlignment.TopCenter)
                {
                    x = (innerWidth - watermarkSize.Width) / 2 + topLeftPadding.Width;
                }

                if (align == ContentAlignment.BottomLeft || align == ContentAlignment.MiddleLeft || align == ContentAlignment.TopLeft)
                {
                    x = topLeftPadding.Width;
                }

                //Draw watermark
                g.DrawImage(wb, new Rectangle((int)(x + imageBox.X), (int)(y + imageBox.Y), (int)watermarkSize.Width, (int)watermarkSize.Height));
            }
            return(true);
        }
        public static WpfImageSettings WpfDestinationImageSettings(this ImageState imageState, ResizeSettings settings)
        {
            WpfImageSettings wpfImageSettings = new WpfImageSettings();
            Rectangle        imageDest        = PolygonMath.ToRectangle(PolygonMath.GetBoundingBox(imageState.layout["image"]));


            /* test - provo a ricavare i dati di resize che mi servono direttamente dagli oggetti Drawing sottostanti */
            SizeF imageAreaSizes = PolygonMath.getParallelogramSize(imageState.layout["image"]);


            wpfImageSettings.DestinationImageWidth  = imageAreaSizes.Width;
            wpfImageSettings.DestinationImageHeight = imageAreaSizes.Height;



            // Correct the settings.Mode according to the documentation
            if (settings.Mode == FitMode.None && (settings.Width != -1 && settings.Height != -1))
            {
                settings.Mode = FitMode.Pad;
            }
            else if (settings.Mode == FitMode.None && (settings.MaxWidth != -1 && settings.MaxHeight != -1))
            {
                settings.Mode = FitMode.Max;
            }


            #region -- Manage the image dimensions --

            //double widthToApply = (settings.Width == -1 ? (double)settings.MaxWidth : (double)settings.Width);
            //double heightToApply = (settings.Height == -1 ? (double)settings.MaxHeight : (double)settings.Height);
            //var proportionWidth = (double)imageState.originalSize.Width / widthToApply;
            //var proportionHeight = (double)imageState.originalSize.Height / heightToApply;

            switch (settings.Mode)
            {
            case FitMode.None:
                break;

            case FitMode.Carve:
            // TODO
            //case FitMode.Pad:
            case FitMode.Max:
                //if (proportionWidth > proportionHeight)
                //{
                //    wpfImageSettings.DestinationImageHeight = Convert.ToInt32(imageState.originalSize.Height / proportionWidth);
                //    wpfImageSettings.DestinationImageWidth = Convert.ToInt32(widthToApply);
                //}
                //else
                //{
                //    wpfImageSettings.DestinationImageWidth = Convert.ToInt32(imageState.originalSize.Width / proportionHeight);
                //    wpfImageSettings.DestinationImageHeight = Convert.ToInt32(heightToApply);
                //}

                /*
                 *
                 *
                 *
                 * TODO:
                 *
                 * verificare la necessità di calcolare gli offset per il PAD
                 *
                 *
                 *
                 *
                 */



                break;

            case FitMode.Crop:
            case FitMode.Pad:
                //int scaleWidth, scaleHeight;
                //scaleWidth = scaleHeight = 0;

                //wpfImageSettings.DestinationImageCanvasWidth = scaleWidth = imageDest.Width;
                //wpfImageSettings.DestinationImageCanvasHeight = scaleHeight = imageDest.Height;

                //// If only a dimension is missing make it square
                //if (wpfImageSettings.DestinationImageCanvasWidth == 0 || wpfImageSettings.DestinationImageCanvasHeight == 0)
                //{
                //    wpfImageSettings.DestinationImageCanvasWidth = wpfImageSettings.DestinationImageCanvasHeight = Math.Max(wpfImageSettings.DestinationImageCanvasWidth, wpfImageSettings.DestinationImageCanvasHeight);
                //}

                //double originalProportions = (double)imageState.originalSize.Width / (double)imageState.originalSize.Height;
                //double viewportProportions = (double)wpfImageSettings.DestinationImageCanvasWidth / (double)wpfImageSettings.DestinationImageCanvasHeight;

                //// Calculates the new scale proportions to make touche-from-inside crop
                //if ((originalProportions > 1 && viewportProportions <= 1) || (originalProportions < 1 && viewportProportions > 1))
                //{
                //    scaleHeight = Math.Max(wpfImageSettings.DestinationImageCanvasHeight, wpfImageSettings.DestinationImageCanvasWidth);
                //    scaleWidth = Convert.ToInt32(((float)(scaleHeight) / (float)(imageState.originalSize.Height)) * imageState.originalSize.Width);
                //}
                //else
                //{
                //    scaleWidth = Math.Max(wpfImageSettings.DestinationImageCanvasHeight, wpfImageSettings.DestinationImageCanvasWidth);
                //    scaleHeight = Convert.ToInt32(((float)(scaleWidth) / (float)(imageState.originalSize.Width)) * imageState.originalSize.Height);
                //}

                //wpfImageSettings.DestinationImageWidth = scaleWidth;
                //wpfImageSettings.DestinationImageHeight = scaleHeight;

                //if ((imageState.copyRect.Y == 0) && (imageState.copyRect.X != 0))
                if ((imageState.originalSize.Width / imageState.originalSize.Height) >= (imageDest.Width / imageDest.Height))
                {
                    wpfImageSettings.DestinationImageWidth = (imageState.originalSize.Width * imageDest.Height) / imageState.copyRect.Height;

                    if (settings.Mode == FitMode.Pad)
                    {
                        wpfImageSettings.DestinationImageHeight = imageState.originalSize.Height;
                    }
                    else
                    {
                        wpfImageSettings.DestinationImageHeight = imageDest.Height;
                    }

                    wpfImageSettings.OffsetX = -(wpfImageSettings.DestinationImageWidth * imageState.copyRect.X) / imageState.originalSize.Width;
                    wpfImageSettings.OffsetY = 0;
                }
                else     // if ((imageState.copyRect.X == 0) && (imageState.copyRect.Y != 0))
                {
                    if (settings.Mode == FitMode.Pad)
                    {
                        wpfImageSettings.DestinationImageWidth = imageState.originalSize.Width;
                    }
                    else
                    {
                        wpfImageSettings.DestinationImageWidth = imageDest.Width;
                    }

                    wpfImageSettings.DestinationImageHeight = (imageState.originalSize.Height * imageDest.Width) / imageState.copyRect.Width;
                    wpfImageSettings.OffsetX = 0;
                    wpfImageSettings.OffsetY = -(wpfImageSettings.DestinationImageHeight * imageState.copyRect.Y) / imageState.originalSize.Height;
                }

                /*else
                 * {
                 *
                 * }*/

                break;

            //case FitMode.Pad:
            //    wpfImageSettings.DestinationImageHeight = Convert.ToInt32(imageState.layout["image"][3].Y - imageState.layout["image"][0].Y);
            //    wpfImageSettings.DestinationImageWidth = Convert.ToInt32(imageState.layout["image"][1].X - imageState.layout["image"][3].X);
            //    break;

            case FitMode.Stretch:
                //wpfImageSettings.DestinationImageWidth = Convert.ToInt32(widthToApply);
                //wpfImageSettings.DestinationImageHeight = Convert.ToInt32(heightToApply);
                break;

            default:
                wpfImageSettings.DestinationImageWidth  = imageState.originalSize.Width;
                wpfImageSettings.DestinationImageHeight = imageState.originalSize.Height;
                break;
            }

            #endregion

            #region -- Manage the allignments --

            switch (settings.Mode)
            {
            case FitMode.None:
            case FitMode.Crop:
            case FitMode.Pad:
                RectangleF croppedSize = settings.getCustomCropSourceRect(imageState.originalSize);

                if ((croppedSize.X != 0) || (croppedSize.Y != 0))
                {
                    wpfImageSettings.OffsetX = -Convert.ToInt32(croppedSize.X);
                    wpfImageSettings.OffsetY = -Convert.ToInt32(croppedSize.Y);

                    wpfImageSettings.DestinationImageCanvasWidth  = croppedSize.Right - croppedSize.Left;
                    wpfImageSettings.DestinationImageCanvasHeight = croppedSize.Bottom - croppedSize.Top;
                }
                else
                {
                    wpfImageSettings.OffsetX = imageState.layout["image"][0].X;
                    wpfImageSettings.OffsetY = imageState.layout["image"][0].Y;
                }



                //wpfImageSettings.DestinationImageCanvasWidth = imageDest.Width;
                //wpfImageSettings.DestinationImageCanvasHeight = imageDest.Height;

                //// In crop or pad I've to calculate the Offsets
                //switch (settings.Anchor)
                //{
                //    case ContentAlignment.BottomCenter:
                //        wpfImageSettings.OffsetX = (int)Math.Floor((double)(imageState.finalSize.Width - wpfImageSettings.DestinationImageWidth) / 2);
                //        wpfImageSettings.OffsetY = imageState.finalSize.Height - wpfImageSettings.DestinationImageHeight;
                //        break;
                //    case ContentAlignment.BottomLeft:
                //        wpfImageSettings.OffsetX = 0;
                //        wpfImageSettings.OffsetY = imageState.finalSize.Height - wpfImageSettings.DestinationImageHeight;
                //        break;
                //    case ContentAlignment.BottomRight:
                //        wpfImageSettings.OffsetX = imageState.finalSize.Width - wpfImageSettings.DestinationImageWidth;
                //        wpfImageSettings.OffsetY = imageState.finalSize.Height - wpfImageSettings.DestinationImageHeight;
                //        break;
                //    case ContentAlignment.MiddleCenter:
                //        wpfImageSettings.OffsetX = (int)Math.Floor((double)(imageState.finalSize.Width - wpfImageSettings.DestinationImageWidth) / 2);
                //        wpfImageSettings.OffsetY = (int)Math.Floor((double)(imageState.finalSize.Height - wpfImageSettings.DestinationImageHeight) / 2);
                //        break;
                //    case ContentAlignment.MiddleLeft:
                //        wpfImageSettings.OffsetX = 0;
                //        wpfImageSettings.OffsetY = (int)Math.Floor((double)(imageState.finalSize.Height - wpfImageSettings.DestinationImageHeight) / 2);
                //        break;
                //    case ContentAlignment.MiddleRight:
                //        wpfImageSettings.OffsetX = imageState.finalSize.Width - wpfImageSettings.DestinationImageWidth;
                //        wpfImageSettings.OffsetY = (int)Math.Floor((double)(imageState.finalSize.Height - wpfImageSettings.DestinationImageHeight) / 2);
                //        break;
                //    case ContentAlignment.TopCenter:
                //        wpfImageSettings.OffsetX = (int)Math.Floor((double)(imageState.finalSize.Width - wpfImageSettings.DestinationImageWidth) / 2);
                //        wpfImageSettings.OffsetY = 0;
                //        break;
                //    case ContentAlignment.TopLeft:
                //        wpfImageSettings.OffsetX = 0;
                //        wpfImageSettings.OffsetY = 0;
                //        break;
                //    case ContentAlignment.TopRight:
                //        wpfImageSettings.OffsetX = imageState.finalSize.Width - wpfImageSettings.DestinationImageWidth;
                //        wpfImageSettings.OffsetY = 0;
                //        break;
                //    default:
                //        break;
                //}
                break;

            //case FitMode.Crop:

            //    break;
            default:
                /*
                 *
                 *
                 * TODO: risistemare!!!
                 *
                 *
                 */

                // Supposing I'm on manual cropping, I'll use the underlying calculations
                //wpfImageSettings.DestinationImageWidth = imageState.originalSize.Width;
                //wpfImageSettings.DestinationImageHeight = imageState.originalSize.Height;

                //RectangleF croppedSize = settings.getCustomCropSourceRect(imageState.originalSize);

                //wpfImageSettings.OffsetX = -Convert.ToInt32(croppedSize.X);
                //wpfImageSettings.OffsetY = -Convert.ToInt32(croppedSize.Y);

                //wpfImageSettings.DestinationImageCanvasWidth = croppedSize.Right - croppedSize.Left;
                //wpfImageSettings.DestinationImageCanvasHeight = croppedSize.Bottom - croppedSize.Top;
                break;
            }

            #endregion

            if ((settings.Rotate % 360) != 0)
            {
                wpfImageSettings.OffsetX = (imageState.finalSize.Width - wpfImageSettings.DestinationImageWidth) / 2;
                wpfImageSettings.OffsetY = (imageState.finalSize.Height - wpfImageSettings.DestinationImageHeight) / 2;
            }

            return(wpfImageSettings);
        }
Exemple #10
0
        protected override RequestedAction BuildJob(ImageJob job)
        {
            if (!"wpf".Equals(job.Settings["builder"]))
            {
                return(RequestedAction.None);
            }

            // Estrazione delle ResizeSettings
            ResizeSettings settings = job.Settings;


            Stream s                     = null;
            bool   disposeStream         = !(job.Source is Stream);
            long   originalPosition      = 0;
            bool   restoreStreamPosition = false;

            string path;

            s = c.CurrentImageBuilder.GetStreamFromSource(job.Source, job.Settings, ref disposeStream, out path, out restoreStreamPosition);
            if (s == null)
            {
                return(RequestedAction.None);           //We don't support the source object!
            }
            if (job.ResetSourceStream)
            {
                restoreStreamPosition = true;
            }
            job.SourcePathData = path;

            // Instanzio uno stream locale per le operazioni WPF
            using (MemoryStream localStream = (s is MemoryStream) ? (MemoryStream)s : StreamExtensions.CopyToMemoryStream(s))
            {
                if (s != null && restoreStreamPosition && s.CanSeek)
                {
                    s.Seek(originalPosition, SeekOrigin.Begin);
                }

                if (disposeStream)
                {
                    s.Dispose();
                }

                /* ? ? ? */
                IEncoder managedEncoder       = c.Plugins.GetEncoder(job.Settings, job.SourcePathData);
                bool     supportsTransparency = managedEncoder.SupportsTransparency;

                // Recupero le dimensioni originali
                var frame = BitmapFrame.Create(StreamExtensions.CopyToMemoryStream(localStream));
                System.Windows.Size originalSize = new System.Windows.Size(frame.PixelWidth, frame.PixelHeight);

                // Resetto lo stream locale alla posizione iniziale, dopo aver letto i metadata
                localStream.Position = 0;



                // Uhm... sono costretto a referenziare le System.Drawing (GDI) per questo,
                // TODO: chiedere al tipo se si può prevedere un costruttore di ImageState che non preveda un System.Drawing.Size come parametro
                System.Drawing.Size orig = new System.Drawing.Size((int)originalSize.Width, (int)originalSize.Height);

                using (ImageState imageState = new ImageState(settings, orig, true))
                {
                    c.CurrentImageBuilder.Process(imageState);

                    Rectangle imageDest = PolygonMath.ToRectangle(PolygonMath.GetBoundingBox(imageState.layout["image"]));

                    BitmapSource finalImage;

                    BitmapImage bi = new BitmapImage();
                    bi.CacheOption = BitmapCacheOption.OnLoad;
                    bi.BeginInit();
                    bi.StreamSource = localStream;


                    WpfImageSettings wpfImageSettings = imageState.WpfDestinationImageSettings(settings);

                    bi.DecodePixelWidth  = Convert.ToInt32(wpfImageSettings.DestinationImageWidth);
                    bi.DecodePixelHeight = Convert.ToInt32(wpfImageSettings.DestinationImageHeight);
                    bi.EndInit();

                    // Creation of the encoder
                    WpfEncoderPlugin wpfEncoder = new WpfEncoderPlugin(settings, job.SourcePathData);


                    RenderTargetBitmap final = new RenderTargetBitmap(imageState.finalSize.Width, imageState.finalSize.Height, settings.Get <int>("dpi", 96), settings.Get <int>("dpi", 96), PixelFormats.Default);
                    DrawingVisual      dv    = new DrawingVisual();

                    using (DrawingContext dc = dv.RenderOpen())
                    {
                        string ARGBBackgroundColor = String.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", wpfEncoder.MimeType.Equals("image/jpeg") ? 255 : settings.BackgroundColor.A,
                                                                   settings.BackgroundColor.R,
                                                                   settings.BackgroundColor.G,
                                                                   settings.BackgroundColor.B);

                        System.Windows.Media.Brush BrushBackgroundColor = new System.Windows.Media.SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(ARGBBackgroundColor));

                        /* todo: verificare */
                        dc.DrawRectangle(BrushBackgroundColor, null, new Rect(0, 0, wpfImageSettings.DestinationImageWidth, wpfImageSettings.DestinationImageHeight));

                        Rect rect = new Rect(wpfImageSettings.OffsetX, wpfImageSettings.OffsetY, wpfImageSettings.DestinationImageWidth, wpfImageSettings.DestinationImageHeight);

                        //dc.PushTransform(new RotateTransform(settings.Rotate, (double)imageState.finalSize.Width / 2, (double)imageState.finalSize.Height / 2));

                        dc.DrawImage(bi, rect);
                    }

                    final.Render(dv);
                    finalImage = final;

                    // Write the image to the output stream
                    wpfEncoder.Write(finalImage, (Stream)job.Dest);
                }
            }

            return(RequestedAction.None);
        }
Exemple #11
0
        /// <summary>
        /// Decodes the image in byte[] data, performs the image proccessing, and encodes it to job.Dest
        /// </summary>
        /// <param name="data">The buffer containing the encoded image file</param>
        /// <param name="lData">The number of bytes to read</param>
        /// <param name="job"></param>
        /// <param name="supportsTransparency"></param>
        /// <returns></returns>
        protected virtual RequestedAction BuildJobWic(byte[] data, long lData, ImageJob job, bool supportsTransparency)
        {
            ResizeSettings settings = job.Settings; ResizeSettings q = settings;
            string         path = job.SourcePathData;

            //A list of COM objects to destroy
            List <object> com = new List <object>();

            try {
                //Create the factory
                IWICComponentFactory factory = (IWICComponentFactory) new WICImagingFactory();
                com.Add(factory);

                //Wrap the byte[] with a IWICStream instance
                var streamWrapper = factory.CreateStream();
                streamWrapper.InitializeFromMemory(data, (uint)lData);
                com.Add(streamWrapper);

                var decoder = factory.CreateDecoderFromStream(streamWrapper, null,
                                                              WICDecodeOptions.WICDecodeMetadataCacheOnLoad);
                com.Add(decoder);

                //Figure out which frame to work with
                int frameIndex = 0;
                if (!string.IsNullOrEmpty(q["page"]) && !int.TryParse(q["page"], NumberStyles.Number, NumberFormatInfo.InvariantInfo, out frameIndex))
                {
                    if (!string.IsNullOrEmpty(q["frame"]) && !int.TryParse(q["frame"], NumberStyles.Number, NumberFormatInfo.InvariantInfo, out frameIndex))
                    {
                        frameIndex = 0;
                    }
                }

                //So users can use 1-based numbers
                frameIndex--;

                if (frameIndex > 0)
                {
                    int frameCount = (int)decoder.GetFrameCount(); //Don't let the user go past the end.
                    if (frameIndex >= frameCount)
                    {
                        frameIndex = frameCount - 1;
                    }
                }

                IWICBitmapFrameDecode frame = decoder.GetFrame((uint)Math.Max(0, frameIndex));
                com.Add(frame);



                WICBitmapInterpolationMode interpolationMode = WICBitmapInterpolationMode.WICBitmapInterpolationModeFant;
                if ("nearest".Equals(settings["w.filter"], StringComparison.OrdinalIgnoreCase))
                {
                    interpolationMode = WICBitmapInterpolationMode.WICBitmapInterpolationModeNearestNeighbor;
                }
                if ("bicubic".Equals(settings["w.filter"], StringComparison.OrdinalIgnoreCase))
                {
                    interpolationMode = WICBitmapInterpolationMode.WICBitmapInterpolationModeCubic;
                }
                if ("linear".Equals(settings["w.filter"], StringComparison.OrdinalIgnoreCase))
                {
                    interpolationMode = WICBitmapInterpolationMode.WICBitmapInterpolationModeLinear;
                }
                if ("nearestneighbor".Equals(settings["w.filter"], StringComparison.OrdinalIgnoreCase))
                {
                    interpolationMode = WICBitmapInterpolationMode.WICBitmapInterpolationModeLinear;
                }

                //Find the original image size
                uint origWidth, origHeight;
                frame.GetSize(out origWidth, out origHeight);
                Size orig = new Size((int)origWidth, (int)origHeight);

                Guid pixelFormat;
                frame.GetPixelFormat(out pixelFormat);
                //Calculate the new size of the image and the canvas.
                ImageState state = new ImageState(settings, orig, true);
                c.CurrentImageBuilder.Process(state);


                Rectangle imageDest = PolygonMath.ToRectangle(PolygonMath.GetBoundingBox(state.layout["image"]));


                IWICBitmapSource imageData = frame;
                //Are we cropping? then daisy-chain a clipper
                if (state.copyRect.Left != 0 || state.copyRect.Top != 0 || state.copyRect.Width != state.originalSize.Width || state.copyRect.Height != state.originalSize.Height)
                {
                    //Cropping is absurdly slow... 4x slower than resizing!
                    //Cropping after resizing (unintuitively) is faster.
                    if (imageDest.Width != state.originalSize.Width || imageDest.Height != state.originalSize.Height)
                    {
                        double sx = (double)imageDest.Width / (double)state.copyRect.Width;
                        double sy = (double)imageDest.Height / (double)state.copyRect.Height;
                        uint   uncroppedDestWidth  = (uint)Math.Round(sx * state.originalSize.Width);
                        uint   uncroppedDestHeight = (uint)Math.Round(sy * state.originalSize.Height);

                        var scaler = factory.CreateBitmapScaler();
                        scaler.Initialize(imageData, uncroppedDestWidth, uncroppedDestHeight, interpolationMode);
                        com.Add(scaler);

                        //TODO: cropping is not consistent with GDI.
                        var clipper = factory.CreateBitmapClipper();
                        clipper.Initialize(scaler, new WICRect {
                            X      = (int)Math.Floor((double)state.copyRect.X * sx),
                            Y      = (int)Math.Floor((double)state.copyRect.Y * sy),
                            Width  = imageDest.Width,
                            Height = imageDest.Height
                        });
                        com.Add(clipper);
                        imageData = clipper;
                    }
                    else
                    {
                        var clipper = factory.CreateBitmapClipper();
                        clipper.Initialize(imageData, new WICRect {
                            X = (int)state.copyRect.X, Y = (int)state.copyRect.Y, Width = (int)state.copyRect.Width, Height = (int)state.copyRect.Height
                        });
                        com.Add(clipper);
                        imageData = clipper;
                    }
                    //If we're scaling but not cropping.
                }
                else if (imageDest.Width != state.originalSize.Width || imageDest.Height != state.originalSize.Height)
                {
                    var scaler = factory.CreateBitmapScaler();
                    scaler.Initialize(imageData, (uint)imageDest.Width, (uint)imageDest.Height, interpolationMode);
                    com.Add(scaler);
                    imageData = scaler;
                }



                //Are we padding? Then we have to do an intermediate write.
                if (state.destSize.Width != imageDest.Width || state.destSize.Height != imageDest.Height)
                {
                    byte[] bgcolor = ConversionUtils.ConvertColor(job.Settings.BackgroundColor, pixelFormat);

                    for (int i = 0; i < bgcolor.Length; i++)
                    {
                        bgcolor[i] = 255;                                      //White
                    }
                    var padder = new WicBitmapPadder(imageData, imageDest.X, imageDest.Y, state.destSize.Width - (imageDest.X + imageDest.Width), state.destSize.Height - (imageDest.Y + imageDest.Height), bgcolor, null);
                    imageData = padder;
                }

                //Now encode imageData and be done with it...
                return(Encode(factory, imageData, imageDest.Size, job));
            } finally {
                //Manually cleanup all the com reference counts, aggressively
                while (com.Count > 0)
                {
                    Marshal.ReleaseComObject(com[com.Count - 1]); //In reverse order, so no item is ever deleted out from under another.
                    com.RemoveAt(com.Count - 1);
                }
            }
        }
Exemple #12
0
        /// <summary>
        /// Returns a rectangle with canvas-relative coordinates. A callback is required to calculate the actual size of the content based on the specified bounds.
        /// The callback may be passed double.NaN for one or more paramters to indicate that they are not specified.
        ///
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public RectangleF CalculateLayerCoordinates(ImageState s, CalculateLayerContentSize actualSizeCalculator, bool forceInsideCanvas)
        {
            //Find container
            RectangleF cont;

            if (s.layout.ContainsRing(RelativeTo))
            {
                cont = PolygonMath.GetBoundingBox(s.layout[RelativeTo]);
            }
            else if ("canvas".Equals(RelativeTo, StringComparison.OrdinalIgnoreCase))
            {
                cont = new RectangleF(new PointF(), s.destSize);
            }
            else
            {
                cont = PolygonMath.GetBoundingBox(s.layout["image"]);
            }

            //Calculate layer coords
            RectangleF rect = new RectangleF();

            //Resolve all values to the same coordinate plane, null values will be transformed to NaN
            double left   = Resolve(Left, cont.X, cont.Width, false);
            double top    = Resolve(Top, cont.Y, cont.Height, false);
            double right  = Resolve(Right, cont.Right, cont.Width, true);
            double bottom = Resolve(Bottom, cont.Bottom, cont.Height, true);
            double width  = Resolve(Width, 0, cont.Width, false);
            double height = Resolve(Height, 0, cont.Height, false);

            //Force all values to be within the canvas area.
            if (forceInsideCanvas)
            {
                SizeF canvas = s.destSize;
                if (!double.IsNaN(left))
                {
                    left = Math.Min(Math.Max(0, left), canvas.Width);
                }
                if (!double.IsNaN(right))
                {
                    right = Math.Min(Math.Max(0, right), canvas.Width);
                }
                if (!double.IsNaN(width))
                {
                    width = Math.Min(Math.Max(0, width), canvas.Width);
                }
                if (!double.IsNaN(bottom))
                {
                    bottom = Math.Min(Math.Max(0, bottom), canvas.Height);
                }
                if (!double.IsNaN(top))
                {
                    top = Math.Min(Math.Max(0, top), canvas.Height);
                }
                if (!double.IsNaN(height))
                {
                    height = Math.Min(Math.Max(0, height), canvas.Height);
                }
            }

            //If right and left (or top and bottom) are inverted, avg them and set them equal.
            if (!double.IsNaN(left) && !double.IsNaN(right) && right < left)
            {
                left = right = ((left + right) / 2);
            }
            if (!double.IsNaN(top) && !double.IsNaN(bottom) && bottom < top)
            {
                bottom = top = ((bottom + top) / 2);
            }


            //Fill in width/height if enough stuff is specified
            if (!double.IsNaN(left) && !double.IsNaN(right) && double.IsNaN(width))
            {
                width = Math.Max(right - left, 0);
            }
            if (!double.IsNaN(top) && !double.IsNaN(bottom) && double.IsNaN(height))
            {
                height = Math.Max(bottom - top, 0);
            }


            //Execute the callback to get the actual size. Update the width and height values if the actual size is smaller.
            SizeF normalSize = actualSizeCalculator((double.IsNaN(width) && Fill) ? cont.Width : width, (double.IsNaN(height) && Fill) ? cont.Height : height);

            if (double.IsNaN(width) || width > normalSize.Width)
            {
                width = normalSize.Width;
            }
            if (double.IsNaN(height) || height > normalSize.Height)
            {
                height = normalSize.Height;
            }



            //If only width and height are specified, set the other values to match the container, and let alignment sort it out.
            if (double.IsNaN(left) && double.IsNaN(right))
            {
                left = cont.X; right = cont.Right;
            }                                                                                    //Handle situations where neither left nor right is specified, pretend left=0
            if (double.IsNaN(top) && double.IsNaN(bottom))
            {
                top = cont.X; bottom = cont.Bottom;
            }                                                                                      //Handle situations where neither top nor bottom is specified, pretend top=0


            //When all 3 values are specified in either direction, we must use the alignment setting to determine which direction to snap to.
            if (!double.IsNaN(left) && !double.IsNaN(right) && !double.IsNaN(width))
            {
                if (width > right - left)
                {
                    width = right - left;                       //Use the smaller value in this case, no need to align.
                }
                else
                {
                    if (Align == ContentAlignment.BottomLeft || Align == ContentAlignment.MiddleLeft || Align == ContentAlignment.TopLeft)
                    {
                        right = left + width;
                    }
                    if (Align == ContentAlignment.BottomCenter || Align == ContentAlignment.MiddleCenter || Align == ContentAlignment.TopCenter)
                    {
                        left += (right - left - width) / 2;
                        right = left + width;
                    }
                    if (Align == ContentAlignment.BottomRight || Align == ContentAlignment.MiddleRight || Align == ContentAlignment.TopRight)
                    {
                        left = right - width;
                    }
                }
            }

            //When all 3 values are specified in either direction, we must use the alignment setting to determine which direction to snap to.
            if (!double.IsNaN(top) && !double.IsNaN(bottom) && !double.IsNaN(height))
            {
                if (height > bottom - top)
                {
                    height = bottom - top;                        //Use the smaller value in this case, no need to align.
                }
                else
                {
                    if (Align == ContentAlignment.TopLeft || Align == ContentAlignment.TopCenter || Align == ContentAlignment.TopRight)
                    {
                        bottom = top + height;
                    }
                    if (Align == ContentAlignment.MiddleLeft || Align == ContentAlignment.MiddleCenter || Align == ContentAlignment.MiddleRight)
                    {
                        top   += (bottom - top - height) / 2;
                        bottom = top + height;
                    }
                    if (Align == ContentAlignment.BottomLeft || Align == ContentAlignment.BottomCenter || Align == ContentAlignment.BottomRight)
                    {
                        top = bottom - height;
                    }
                }
            }


            //Calculate values for top and left based off bottom and right
            if (double.IsNaN(left))
            {
                left = right - width;
            }
            if (double.IsNaN(top))
            {
                top = bottom - height;
            }

            //Calculate values for bottom and right based off top and left
            if (double.IsNaN(right))
            {
                right = left + width;
            }
            if (double.IsNaN(bottom))
            {
                bottom = top + height;
            }


            return(new RectangleF((float)left, (float)top, (float)width, (float)height));
        }
Exemple #13
0
        protected virtual void AssertLayout(string sourceSize, string resizeSettings, Action <RectangleF> assertImage, Action <SizeF> assertCanvas)
        {
            var sourceSizeSettings = new ResizeSettings(sourceSize);
            var result             = _imageLayoutBuilder.BuildLayout(new Size(sourceSizeSettings.Width, sourceSizeSettings.Height), new ResizeSettings(resizeSettings));

            if (assertCanvas != null)
            {
                assertCanvas(result.CanvasSize);
            }
            if (assertImage != null)
            {
                assertImage(result.Image);
            }

            var maxWidth  = (int)(Math.Max(result.Image.Width, result.CanvasSize.Width));
            var maxHeight = (int)(Math.Max(result.Image.Height, result.CanvasSize.Height));

            var padding = (int)Math.Max(Math.Abs(result.Image.Y), Math.Abs(result.Image.X)) + 20;

            if ((maxWidth + padding) < 400)
            {
                padding = (400 - maxWidth) / 2;
            }

            // create a bitmap for visualizing
            var bitmapSize = new RectangleF(0, 0, maxWidth + padding * 2, maxHeight + (padding * 2));

            using (var bmp = new Bitmap((int)bitmapSize.Width, (int)bitmapSize.Height))
            {
                using (var gfx = Graphics.FromImage(bmp))
                {
                    // set the background
                    gfx.FillRectangle(new SolidBrush(Color.White), 0, 0, bmp.Width, bmp.Height);

                    // output the results
                    gfx.DrawString("Source: " + sourceSize, new Font("Thaoma", 8), Brushes.Black, new RectangleF(0, 0, bmp.Width, bmp.Height), new StringFormat {
                        Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Near
                    });
                    gfx.DrawString("Destination: " + resizeSettings, new Font("Thaoma", 8), Brushes.Black, new RectangleF(0, 0, bmp.Width, bmp.Height), new StringFormat {
                        Alignment = StringAlignment.Far, LineAlignment = StringAlignment.Near
                    });
                    gfx.DrawString("Canvas: " + result.CanvasSize.Width + "x" + result.CanvasSize.Height, new Font("Thaoma", 8), Brushes.Green, new RectangleF(0, 0, bmp.Width, bmp.Height), new StringFormat {
                        Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Far
                    });
                    gfx.DrawString("Image: " + result.Image.Width + "x" + result.Image.Height, new Font("Thaoma", 8), Brushes.Red, new RectangleF(0, 0, bmp.Width, bmp.Height), new StringFormat {
                        Alignment = StringAlignment.Far, LineAlignment = StringAlignment.Far
                    });

                    //PolygonMath.AlignWith()
                    var canvas = new RectangleF(padding, padding, result.CanvasSize.Width, result.CanvasSize.Height);
                    var image  = new RectangleF(padding + result.Image.X, padding + result.Image.Y, result.Image.Width, result.Image.Height);
                    var points = new List <PointF>();
                    points.AddRange(PolygonMath.ToPoly(canvas));
                    points.AddRange(PolygonMath.ToPoly(image));
                    points = PolygonMath.AlignWith(points.ToArray(), PolygonMath.ToPoly(bitmapSize), ContentAlignment.MiddleCenter).ToList();
                    canvas = PolygonMath.GetBoundingBox(points.Take(4).ToArray());
                    image  = PolygonMath.GetBoundingBox(points.Skip(4).Take(4).ToArray());
                    gfx.FillRectangle(new SolidBrush(Color.Green), canvas);
                    gfx.DrawRectangle(new Pen(Color.Red, 2), image.X, image.Y, image.Width, image.Height);
                }
                var fileName = sourceSize + "--" + resizeSettings + ".bmp";
                var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
                bmp.Save(filePath, ImageFormat.Bmp);

                Trace.WriteLine("Source:        " + sourceSize);
                Trace.WriteLine("Destination:   " + resizeSettings);
                Trace.WriteLine("   Result:     " + filePath);
            }
        }
Exemple #14
0
        public override void RenderTo(Resizing.ImageState s)
        {
            if (string.IsNullOrEmpty(Text))
            {
                return;
            }

            string finalText = Text;

            if (finalText.IndexOf('#') > -1)
            {
                Regex r = new Regex("\\#\\{([^}]+)\\}");
                finalText = r.Replace(finalText, delegate(Match m){
                    string val = s.settings[m.Groups[1].Value];
                    if (val == null)
                    {
                        return("");
                    }
                    else
                    {
                        return(val);
                    }
                });
            }

            SizeF      naturalSize   = SizeF.Empty;
            SizeF      unrotatedSize = SizeF.Empty;
            RectangleF bounds        = this.CalculateLayerCoordinates(s, delegate(double maxwidth, double maxheight) {
                using (Font f = GetFont())
                    using (StringFormat sf = GetFormat()){
                        naturalSize = s.destGraphics.MeasureString(finalText, f, new PointF(), sf);
                        SizeF size  = naturalSize;

                        unrotatedSize = Fill ? PolygonMath.ScaleInside(size, new SizeF((float)maxwidth, (float)maxheight)) : size;

                        if (Angle != 0)
                        {
                            size = PolygonMath.GetBoundingBox(PolygonMath.RotatePoly(PolygonMath.ToPoly(new RectangleF(new PointF(0, 0), size)), Angle)).Size;
                        }
                        if (Fill)
                        {
                            size = PolygonMath.ScaleInside(size, new SizeF((float)maxwidth, (float)maxheight));
                        }
                        f.FontFamily.Dispose();
                        return(PolygonMath.RoundPoints(size));
                    }
            }, true);

            using (Font f = GetFont()) {
                s.destGraphics.SmoothingMode      = SmoothingMode.HighQuality;
                s.destGraphics.TextRenderingHint  = Rendering; // Utils.parseEnum<TextRenderingHint>(s.settings["watermark.rendering"], this.Rendering); ;
                s.destGraphics.PixelOffsetMode    = PixelOffsetMode.HighQuality;
                s.destGraphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                s.destGraphics.CompositingMode    = CompositingMode.SourceOver;
                s.destGraphics.CompositingQuality = CompositingQuality.HighQuality;

                s.destGraphics.ResetTransform();
                if (Angle != 0)
                {
                    s.destGraphics.RotateTransform((float)Angle);
                }
                s.destGraphics.ScaleTransform(unrotatedSize.Width / naturalSize.Width, unrotatedSize.Height / naturalSize.Height);
                s.destGraphics.TranslateTransform(bounds.X, bounds.Y, MatrixOrder.Append);
                using (StringFormat sf = GetFormat()) {
                    DrawString(s.destGraphics, finalText, f, new Point(0, 0), sf);
                }
                s.destGraphics.ResetTransform();

                f.FontFamily.Dispose();
            }
        }
Exemple #15
0
        /// <summary>
        /// Builds an FIBitmap from the stream and job.Settings
        /// </summary>
        /// <param name="original"></param>
        /// <param name="supportsTransparency"></param>
        /// <param name="mayUnloadOriginal"></param>
        /// <param name="job"></param>
        /// <returns></returns>
        protected FIBITMAP buildFiBitmap(ref FIBITMAP original, ImageJob job, bool supportsTransparency, bool mayUnloadOriginal)
        {
            ResizeSettings settings = job.Settings;

            if (original.IsNull)
            {
                return(FIBITMAP.Zero);
            }
            FIBITMAP final = FIBITMAP.Zero;

            //Find the image size
            Size orig = new Size((int)FreeImage.GetWidth(original), (int)FreeImage.GetHeight(original));

            //Calculate the new size of the image and the canvas.
            ImageState state = new ImageState(settings, orig, true);

            state.Job = job;
            c.CurrentImageBuilder.Process(state);
            RectangleF imageDest = PolygonMath.GetBoundingBox(state.layout["image"]);

            if (imageDest.Width != orig.Width || imageDest.Height != orig.Height)
            {
                //Rescale
                bool temp;
                final = FreeImage.Rescale(original, (int)imageDest.Width, (int)imageDest.Height, FreeImageScalingPlugin.ParseResizeAlgorithm(settings["fi.scale"], FREE_IMAGE_FILTER.FILTER_BOX, out temp));
                if (mayUnloadOriginal)
                {
                    FreeImage.UnloadEx(ref original);
                }
                if (final.IsNull)
                {
                    return(FIBITMAP.Zero);
                }
            }
            else
            {
                final = original;
            }

            RGBQUAD bgcolor = default(RGBQUAD);

            bgcolor.Color = settings.BackgroundColor;
            if (settings.BackgroundColor == Color.Transparent && !supportsTransparency)
            {
                bgcolor.Color = Color.White;
            }

            //If we need to leave padding, do so.
            BoxPadding outsideImage = new BoxPadding(imageDest.Left, imageDest.Top, state.destSize.Width - imageDest.Right, state.destSize.Height - imageDest.Bottom);

            if (outsideImage.All != 0)
            {
                var old = final;
                //Extend canvas
                final = FreeImage.EnlargeCanvas <RGBQUAD>(old,
                                                          (int)outsideImage.Left, (int)outsideImage.Top, (int)outsideImage.Right, (int)outsideImage.Bottom,
                                                          bgcolor.Color != Color.Transparent ? new Nullable <RGBQUAD>(bgcolor) : null,
                                                          FREE_IMAGE_COLOR_OPTIONS.FICO_RGBA);
                if (old == original)
                {
                    if (mayUnloadOriginal)
                    {
                        FreeImage.UnloadEx(ref original);
                        old = original;
                    }
                }
                else
                {
                    FreeImage.UnloadEx(ref old); //'old' has the original value of 'final', which we allocated.
                }
                if (final.IsNull)
                {
                    return(FIBITMAP.Zero);
                }
            }

            if (!final.IsNull)
            {
                job.ResultInfo["final.width"]  = (int)FreeImage.GetWidth(final);
                job.ResultInfo["final.height"] = (int)FreeImage.GetHeight(final);
            }

            return(final);
        }
Exemple #16
0
 RectangleF SalientBoundingBox()
 {
     return(PolygonMath.GetBoundingBox(Regions
                                       .SelectMany(r => new[] { r.Area.Location, new PointF(r.Area.Right, r.Area.Bottom) })
                                       .ToArray()));
 }