Exemple #1
0
        public override void RenderTo(Resizing.ImageState s)
        {
            if (string.IsNullOrEmpty(Path))
            {
                return;
            }

            s.destGraphics.CompositingMode    = System.Drawing.Drawing2D.CompositingMode.SourceOver;
            s.destGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            s.destGraphics.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            Bitmap img = null;

            if (this.ShouldLoadAsOriginalSize())
            {
                img = GetMemCachedBitmap(Path, ImageQuery);
            }

            //Calculate the location for the bitmap
            RectangleF imgBounds = this.CalculateLayerCoordinates(s, delegate(double maxwidth, double maxheight) {
                ResizeSettings opts = new ResizeSettings(ImageQuery);
                if (Fill && string.IsNullOrEmpty(opts["scale"]))
                {
                    opts.Scale = ScaleMode.Both;
                }
                if (!double.IsNaN(maxwidth))
                {
                    opts.MaxWidth = (int)Math.Floor(maxwidth);
                }
                if (!double.IsNaN(maxheight))
                {
                    opts.MaxHeight = (int)Math.Floor(maxheight);
                }

                if (img == null)
                {
                    img = GetMemCachedBitmap(Path, opts);              //Delayed creation allows the maxwidth/maxheight to be used in gradient plugin
                }
                lock (img) {
                    return(ImageBuilder.Current.GetFinalSize(img.Size, opts));
                }
            }, true);

            lock (img) { //Only one reader from the cached bitmap at a time.
                //Skip rendering unless we have room to work with.
                if (imgBounds.Width < 2 || imgBounds.Height < 2)
                {
                    return;
                }


                if (ImageQuery.Keys.Count > 0 || Fill)
                {
                    ResizeSettings settings = new ResizeSettings(ImageQuery);
                    if (Fill && string.IsNullOrEmpty(settings["scale"]))
                    {
                        settings.Scale = ScaleMode.Both;
                    }

                    settings.MaxWidth  = (int)Math.Floor(imgBounds.Width);
                    settings.MaxHeight = (int)Math.Floor(imgBounds.Height);

                    using (Bitmap final = ImageBuilder.Current.Build(img, settings, false)) {
                        s.destGraphics.DrawImage(final, PolygonMath.ToRectangle(PolygonMath.CenterInside(PolygonMath.DownScaleInside(final.Size, imgBounds.Size), imgBounds)));
                    }
                }
                else
                {
                    s.destGraphics.DrawImage(img, PolygonMath.ToRectangle(PolygonMath.CenterInside(PolygonMath.DownScaleInside(img.Size, imgBounds.Size), imgBounds)));
                }
            }
        }
Exemple #2
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();
            }
        }