Ejemplo n.º 1
0
        /// <summary>
        /// An extension method to draw the given <see cref="TemplateText"/> onto the
        /// <see cref="IImageProcessingContext"/> that we're working with.
        /// <para>Usage: <code>SixLabors.ImageSharp.SomeImage.Mutate(x => x.DrawKaguyaText(...))</code></para>
        /// </summary>
        /// <param name="ctx">The <see cref="IImageProcessingContext"/> we are working with.</param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static IImageProcessingContext DrawKaguyaText(this IImageProcessingContext ctx, TemplateText text)
        {
            if (text.Show)
            {
                var brush  = new SolidBrush(text.Color);
                var stroke = new Pen(text.StrokeColor, text.StrokeWidth);

                return(text.HasStroke
                    ? ctx.DrawText(text.Text, text.Font, brush, stroke, new PointF(text.Loc.X, text.Loc.Y))
                    : ctx.DrawText(text.Text, text.Font, text.Color, new PointF(text.Loc.X, text.Loc.Y)));
            }

            return(null);
        }
        private static IImageProcessingContext ApplyScalingWaterMarkSimple(IImageProcessingContext processingContext,
                                                                           Font font,
                                                                           string text,
                                                                           Color color,
                                                                           float padding)
        {
            Size imgSize = processingContext.GetCurrentSize();

            float targetWidth  = imgSize.Width - (padding * 2);
            float targetHeight = imgSize.Height - (padding * 2);

            // measure the text size
            FontRectangle size = TextMeasurer.Measure(text, new RendererOptions(font));

            //find out how much we need to scale the text to fill the space (up or down)
            float scalingFactor = Math.Min(imgSize.Width / size.Width, imgSize.Height / size.Height);

            //create a new font
            Font scaledFont = new Font(font, scalingFactor * font.Size);

            var center             = new PointF(imgSize.Width / 2, imgSize.Height / 2);
            var textGraphicOptions = new TextGraphicsOptions()
            {
                TextOptions =
                {
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment   = VerticalAlignment.Center
                }
            };

            return(processingContext.DrawText(textGraphicOptions, text, scaledFont, color, center));
        }
Ejemplo n.º 3
0
        public bool Render(IImageProcessingContext <Rgba32> context, Rectangle area, object input)
        {
            if (!(input is string text))
            {
                return(false);
            }

            var width  = area.Width;
            var height = area.Height;
            var x      = area.X;
            var y      = area.Y;

            var scaledFont         = GetAdjustedFont(width, height, text);
            var textGraphicOptions = new TextGraphicsOptions(true)
            {
                HorizontalAlignment = HorizontalAlignment,
                VerticalAlignment   = VerticalAlignment,
                WrapTextWidth       = width
            };

            var renderY = VerticalAlignment == VerticalAlignment.Center ? height / 2 + y : y;

            context.DrawText(textGraphicOptions, text, scaledFont, Brush, Pen, new PointF(x, renderY));

            return(true);
        }
 /// <summary>
 /// Draws the text onto the image filled via the brush.
 /// </summary>
 /// <param name="source">The image this method extends.</param>
 /// <param name="text">The text.</param>
 /// <param name="font">The font.</param>
 /// <param name="brush">The brush.</param>
 /// <param name="location">The location.</param>
 /// <returns>
 /// The <see cref="Image{TPixel}" />.
 /// </returns>
 public static IImageProcessingContext DrawText(
     this IImageProcessingContext source,
     string text,
     Font font,
     IBrush brush,
     PointF location) =>
 source.DrawText(source.GetTextGraphicsOptions(), text, font, brush, location);
        public static void DrawTextAnySize(this IImageProcessingContext context, TextGraphicsOptions op, string?text, FontFamily font, Color color, Rectangle bounds)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            int  fontSize    = 64;
            bool fits        = false;
            Font currentFont = font.CreateFont(fontSize);

            while (!fits)
            {
                currentFont = font.CreateFont(fontSize);
                SizeF size = TextMeasurer.Measure(text, new RendererOptions(currentFont));
                fits = size.Height <= bounds.Height && size.Width <= bounds.Width;

                if (!fits)
                {
                    fontSize -= 2;
                }

                if (fontSize <= 2)
                {
                    return;
                }
            }

            context.DrawText(op, text, currentFont, color, new Point(bounds.X, bounds.Y));
        }
Ejemplo n.º 6
0
        private void DrawControllerToggle(IImageProcessingContext context, PointF point)
        {
            var labelRectangle = MeasureString(ControllerToggleText, _labelsTextFont);

            // Use relative positions so we can center the the entire drawing later.

            float keyWidth  = _keyModeIcon.Width;
            float keyHeight = _keyModeIcon.Height;

            float labelPositionX = keyWidth + 8 - labelRectangle.X;
            float labelPositionY = -labelRectangle.Y - 1;

            float keyX = 0;
            float keyY = (int)((labelPositionY + labelRectangle.Height - keyHeight) / 2);

            float fullWidth  = labelPositionX + labelRectangle.Width;
            float fullHeight = Math.Max(labelPositionY + labelRectangle.Height, keyHeight);

            // Convert all relative positions into absolute.

            float originX = (int)(point.X - fullWidth / 2);
            float originY = (int)(point.Y - fullHeight / 2);

            keyX += originX;
            keyY += originY;

            var labelPosition   = new PointF(labelPositionX + originX, labelPositionY + originY);
            var overlayPosition = new Point((int)keyX, (int)keyY);

            context.DrawImage(_keyModeIcon, overlayPosition, 1);
            context.DrawText(ControllerToggleText, _labelsTextFont, _textNormalColor, labelPosition);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Draws the text onto the the image filled via the brush.
 /// </summary>
 /// <param name="source">The image this method extends.</param>
 /// <param name="text">The text.</param>
 /// <param name="font">The font.</param>
 /// <param name="color">The color.</param>
 /// <param name="location">The location.</param>
 /// <returns>
 /// The <see cref="Image{TPixel}" />.
 /// </returns>
 public static IImageProcessingContext DrawText(
     this IImageProcessingContext source,
     string text,
     Font font,
     Color color,
     PointF location) =>
 source.DrawText(TextGraphicsOptions.Default, text, font, color, location);
        public static void DrawText(this IImageProcessingContext context, TextGraphicsOptions op, string?text, Font font, Color color, Rectangle bounds)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            op.WrapTextWidth = bounds.Width;

            RendererOptions rOp = new RendererOptions(font);

            rOp.WrappingWidth = bounds.Width;

            bool fits = false;

            while (!fits)
            {
                SizeF size = TextMeasurer.Measure(text, rOp);
                fits = size.Height <= bounds.Height && size.Width <= bounds.Width;

                if (!fits)
                {
                    text = text.Truncate(text.Length - 5);
                }
            }

            context.DrawText(op, text, font, color, new Point(bounds.X, bounds.Y));
        }
 /// <summary>
 /// Draws the text onto the image outlined via the pen.
 /// </summary>
 /// <param name="source">The image processing context.</param>
 /// <param name="text">The text to draw.</param>
 /// <param name="font">The font.</param>
 /// <param name="pen">The pen used to outline the text.</param>
 /// <param name="location">The location.</param>
 /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
 public static IImageProcessingContext DrawText(
     this IImageProcessingContext source,
     string text,
     Font font,
     IPen pen,
     PointF location) =>
 source.DrawText(source.GetDrawingOptions(), text, font, pen, location);
Ejemplo n.º 10
0
        private static IImageProcessingContext DrawStats(this IImageProcessingContext source, Font header, Font body,
                                                         string rank, string level, string required, DateTime lastLevelUp)
        {
            const float spacing = XpBarLength / 4f;
            const float y       = 117.5f;

            source.DrawLines(Color.HotPink, 2.5f, new PointF(XpBarX + spacing, y), new PointF(XpBarX + spacing, y + 52))
            .DrawLines(Color.HotPink, 2.5f, new PointF(XpBarX + spacing * 2, y), new PointF(XpBarX + spacing * 2, y + 52))
            .DrawLines(Color.HotPink, 2.5f, new PointF(XpBarX + spacing * 3, y), new PointF(XpBarX + spacing * 3, y + 52));

            var headerOptions = new TextGraphicsOptions {
                TextOptions = { HorizontalAlignment = HorizontalAlignment.Center }
            };

            var bodyOptions = new TextGraphicsOptions {
                TextOptions = { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Bottom, }
            };

            // need to use en-US since en-CA adds a . to month abbreviation (en-US -> Jan, en-CA -> Jan.)
            string lastLevelUpString = lastLevelUp == DateTime.MinValue ? "Never" : lastLevelUp.ToString("MMM dd yyyy", new CultureInfo("en-US"));

            source.DrawText(headerOptions, "Rank", header, Color.HotPink, new PointF(301, y)) // (XpBarX + spacing) / 2
            .DrawText(bodyOptions, rank, body, Color.DarkSlateGray, new PointF(301, 170))
            .DrawText(headerOptions, "Level", header, Color.HotPink, new PointF(430, y))      // ((XpBarX + spacing) / 2 + (XpBarX + spacing * 2)) / 2
            .DrawText(bodyOptions, level, body, Color.DarkSlateGray, new PointF(430, 170))
            .DrawText(headerOptions, "Required XP", header, Color.HotPink, new PointF(558, y))
            .DrawText(bodyOptions, required, body, Color.DarkSlateGray, new PointF(558, 170))
            .DrawText(headerOptions, "Last Level Up", header, Color.HotPink, new PointF(695, y))
            .DrawText(bodyOptions, lastLevelUpString, body, Color.DarkSlateGray, new PointF(695, 170));

            return(source);
        }
 /// <summary>
 /// Draws the text using the given options onto the image filled via the brush then outlined via the pen.
 /// </summary>
 /// <param name="source">The image processing context.</param>
 /// <param name="textOptions">The text rendering options.</param>
 /// <param name="text">The text to draw.</param>
 /// <param name="brush">The brush used to fill the text.</param>
 /// <param name="pen">The pen used to outline the text.</param>
 /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
 public static IImageProcessingContext DrawText(
     this IImageProcessingContext source,
     TextOptions textOptions,
     string text,
     IBrush brush,
     IPen pen) =>
 source.DrawText(source.GetDrawingOptions(), textOptions, text, brush, pen);
 /// <summary>
 /// Draws the text onto the image filled via the brush.
 /// </summary>
 /// <param name="source">The image processing context.</param>
 /// <param name="drawingOptions">The drawing options.</param>
 /// <param name="text">The text to draw.</param>
 /// <param name="font">The font.</param>
 /// <param name="brush">The brush used to fill the text.</param>
 /// <param name="location">The location.</param>
 /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
 public static IImageProcessingContext DrawText(
     this IImageProcessingContext source,
     DrawingOptions drawingOptions,
     string text,
     Font font,
     IBrush brush,
     PointF location)
 => source.DrawText(drawingOptions, text, font, brush, null, location);
Ejemplo n.º 13
0
        private void drawText(IImageProcessingContext pc, string message, string foreColor, string backColor)
        {
            var fColor = Color.ParseHex(foreColor);
            var bColor = Color.ParseHex(backColor);

            pc.Fill(bColor);
            pc.DrawText(message, _font, fColor, new PointF(0, 0));
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Draws the text onto the the image filled via the brush then outlined via the pen.
 /// </summary>
 /// <param name="source">The image this method extends.</param>
 /// <param name="text">The text.</param>
 /// <param name="font">The font.</param>
 /// <param name="brush">The brush.</param>
 /// <param name="pen">The pen.</param>
 /// <param name="location">The location.</param>
 /// <returns>
 /// The <see cref="Image{TPixel}" />.
 /// </returns>
 public static IImageProcessingContext DrawText(
     this IImageProcessingContext source,
     string text,
     Font font,
     IBrush brush,
     IPen pen,
     PointF location) =>
 source.DrawText(TextGraphicsOptions.Default, text, font, brush, pen, location);
 /// <summary>
 /// Draws the text onto the image outlined via the pen.
 /// </summary>
 /// <param name="source">The image this method extends.</param>
 /// <param name="options">The options.</param>
 /// <param name="text">The text.</param>
 /// <param name="font">The font.</param>
 /// <param name="pen">The pen.</param>
 /// <param name="location">The location.</param>
 /// <returns>
 /// The <see cref="Image{TPixel}" />.
 /// </returns>
 public static IImageProcessingContext DrawText(
     this IImageProcessingContext source,
     TextGraphicsOptions options,
     string text,
     Font font,
     IPen pen,
     PointF location) =>
 source.DrawText(options, text, font, null, pen, location);
 /// <summary>
 /// Draws the text onto the image filled via the brush.
 /// </summary>
 /// <param name="source">The image this method extends.</param>
 /// <param name="options">The options.</param>
 /// <param name="text">The text.</param>
 /// <param name="font">The font.</param>
 /// <param name="color">The color.</param>
 /// <param name="location">The location.</param>
 /// <returns>
 /// The <see cref="Image{TPixel}" />.
 /// </returns>
 public static IImageProcessingContext DrawText(
     this IImageProcessingContext source,
     TextGraphicsOptions options,
     string text,
     Font font,
     Color color,
     PointF location) =>
 source.DrawText(options, text, font, Brushes.Solid(color), null, location);
Ejemplo n.º 17
0
        private void DrawPadButton(IImageProcessingContext context, PointF point, Image icon, string label, bool pressed, bool enabled)
        {
            // Use relative positions so we can center the the entire drawing later.

            float iconX      = 0;
            float iconY      = 0;
            float iconWidth  = icon.Width;
            float iconHeight = icon.Height;

            var labelRectangle = MeasureString(label, _labelsTextFont);

            float labelPositionX = iconWidth + 8 - labelRectangle.X;
            float labelPositionY = 3;

            float fullWidth  = labelPositionX + labelRectangle.Width + labelRectangle.X;
            float fullHeight = iconHeight;

            // Convert all relative positions into absolute.

            float originX = (int)(point.X - fullWidth / 2);
            float originY = (int)(point.Y - fullHeight / 2);

            iconX += originX;
            iconY += originY;

            var iconPosition  = new Point((int)iconX, (int)iconY);
            var labelPosition = new PointF(labelPositionX + originX, labelPositionY + originY);

            var selectedRectangle = new RectangleF(originX - 2 * _padPressedPenWidth, originY - 2 * _padPressedPenWidth,
                                                   fullWidth + 4 * _padPressedPenWidth, fullHeight + 4 * _padPressedPenWidth);

            var boundRectangle = new RectangleF(originX, originY, fullWidth, fullHeight);

            boundRectangle.Inflate(4 * _padPressedPenWidth, 4 * _padPressedPenWidth);

            context.Fill(_panelBrush, boundRectangle);
            context.DrawImage(icon, iconPosition, 1);
            context.DrawText(label, _labelsTextFont, _textNormalColor, labelPosition);

            if (enabled)
            {
                if (pressed)
                {
                    context.Draw(_padPressedPen, selectedRectangle);
                }
            }
            else
            {
                // Just draw a semi-transparent rectangle on top to fade the component with the background.
                // TODO (caian): This will not work if one decides to add make background semi-transparent as well.

                context.Fill(_disabledBrush, boundRectangle);
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        /// 绘制文字
        /// </summary>
        /// <param name="context"></param>
        /// <param name="value"></param>
        /// <param name="fontSize"></param>
        /// <param name="fontFamilyPaths"></param>
        /// <param name="colors"></param>
        /// <param name="point"></param>
        private void DrawText(IImageProcessingContext context, string value, int fontSize, IReadOnlyList <string> fontFamilyPaths, IReadOnlyList <Color> colors, PointF point)
        {
            var        random         = new Random();
            var        fonts          = new FontCollection();
            string     fontFamilyPath = fontFamilyPaths[random.Next(0, fontFamilyPaths.Count)];
            FontFamily fontFamily     = fonts.Install(fontFamilyPath); //字体的路径
            var        font           = new Font(fontFamily, fontSize);
            Color      color          = colors[random.Next(0, colors.Count)];

            context.DrawText(value, font, color, point);
        }
Ejemplo n.º 19
0
        private static void DrawProfileName(IImageProcessingContext context, string name, bool isRtl)
        {
            const int nameMaxWidth = 330;
            var       pointX       = 79;

            if (isRtl)
            {
                pointX = context.GetCurrentSize().Width - pointX - 330;
            }
            context.DrawText(name, 32, "#fff", new Size(nameMaxWidth, 40), new Point(pointX, 419));
        }
        /// <summary>
        /// Draws the text onto the image filled via the brush then outlined via the pen.
        /// </summary>
        /// <param name="source">The image processing context.</param>
        /// <param name="text">The text to draw.</param>
        /// <param name="font">The font.</param>
        /// <param name="brush">The brush used to fill the text.</param>
        /// <param name="pen">The pen used to outline the text.</param>
        /// <param name="location">The location.</param>
        /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
        public static IImageProcessingContext DrawText(
            this IImageProcessingContext source,
            string text,
            Font font,
            IBrush brush,
            IPen pen,
            PointF location)
        {
            TextOptions textOptions = new(font) { Origin = location };

            return(source.DrawText(textOptions, text, brush, pen));
        }
Ejemplo n.º 21
0
        private void DrawGraticules(IImageProcessingContext img, GraticuleLabels graticules, Point <double> corner, int zoom)
        {
            var font = SixLabors.Fonts.SystemFonts.CreateFont("Arial", 8);

            foreach (var meridian in graticules.VerticalLabels)
            {
                var loc   = meridian.worldLocation;
                var pt    = TileUtils.PositionToGlobalPixel(new LatLong(loc.Lat, loc.Long), zoom, TileSize);
                var xpos  = pt.X - corner.X;
                var start = new PointF((float)xpos, 0);
                var end   = new PointF((float)xpos, TileSize);
                img.DrawLines(Rgba32.Gray, 1f, new PointF[] { start, end });
                try
                {
                    if (xpos < TileSize - 10)
                    {
                        img.DrawText(Math.Round(loc.Long, 2).ToString(), font, Rgba32.Black, new PointF((float)xpos, 50));
                    }
                }
                catch (Exception)
                {
                }
            }
            foreach (var parallel in graticules.HorizontalLabels)
            {
                var loc   = parallel.worldLocation;
                var pt    = TileUtils.PositionToGlobalPixel(new LatLong(loc.Lat, loc.Long), zoom, TileSize);
                var ypos  = pt.Y - corner.Y;
                var start = new PointF(0, (float)ypos);
                var end   = new PointF(TileSize, (float)ypos);
                img.DrawLines(Rgba32.Gray, 1f, new PointF[] { start, end });
                try
                {
                    img.DrawText(Math.Round(loc.Lat, 4).ToString(), font, Rgba32.Black, new PointF(50, (float)ypos));
                }
                catch (Exception)
                {
                }
            }
        }
Ejemplo n.º 22
0
        private void DrawCustomGauge(bool top, string labelText, string value, float ratio, int img_width, float chevronSize, float width_margin, float chart_width, float min, float max, IImageProcessingContext ctx, bool hideHeader)
        {
            float.TryParse(value, out float floatValue);
            bool missingHeaderLabel         = (labelText?.Length ?? 0) == 0;
            bool writeValueHeaderAndChevron = !hideHeader || (floatValue >= min && floatValue <= max);

            if (writeValueHeaderAndChevron && !missingHeaderLabel)
            {
                var pen = new Pen(Color.White, chevronSize + 1);

                var arrowStartX = (ratio * img_width) + width_margin;
                var arrowStartY = (HALF_WIDTH - ((chart_width / 2) * (top ? 1 : -1)));
                var arrowAddY   = arrowStartY - ((chevronSize * 2) * (top ? 1 : -1));

                var startPoint = new PointF(arrowStartX, arrowStartY);
                var right      = new PointF(arrowStartX + chevronSize, arrowAddY);
                var left       = new PointF(arrowStartX - chevronSize, arrowAddY);

                PointF[] needle = { startPoint, right, left, startPoint };

                var valueText = value.ToString();
                var textColor = (floatValue > max || floatValue < min) ? Color.Red : Color.White;
                var font      = SystemFonts.CreateFont("Arial", chevronSize * 4, FontStyle.Regular);

                var   size    = TextMeasurer.Measure(valueText, new RendererOptions(font));
                float adjustY = top ? Math.Abs(-5 - size.Height) : 5;
                arrowAddY = top ? arrowAddY - adjustY : arrowAddY + adjustY;
                var valuePoint = new PointF(HALF_WIDTH - size.Width / 2, arrowAddY);
                ctx.DrawText(valueText, font, textColor, valuePoint);

                ctx.DrawPolygon(pen, needle);
                var text = labelText != string.Empty ? labelText[0].ToString() : string.Empty;
                size          = TextMeasurer.Measure(text, new RendererOptions(SystemFonts.CreateFont("Arial", chevronSize * 3, FontStyle.Regular)));
                startPoint.Y -= top ? size.Height : 0;
                startPoint.X -= size.Width / 2;
                ctx.DrawText(text, SystemFonts.CreateFont("Arial", chevronSize * 3, FontStyle.Regular), Color.Black, startPoint);
            }
        }
Ejemplo n.º 23
0
        public override void DrawPoint(IImageProcessingContext context, float scale, PointF point)
        {
            string     text          = new string(new[] { Character });
            float      fontPointSize = Size.Height * scale;
            Font       font          = new Font(FontFamily, fontPointSize, Style);
            RectangleF bounds        = MeasureText(text, font);
            float      x             = point.X - bounds.Width / 2;
            float      y             = point.Y - bounds.Height / 2;
            PointF     location      = new PointF(x, y);

            context.DrawText(text, font, Color, location);
            PointF[] points = bounds.ToPoints();
            DrawOutLine(context, scale, points.ToPath());
        }
Ejemplo n.º 24
0
 public static IImageProcessingContext DrawTextCentered(
     this IImageProcessingContext context,
     string text,
     Font font,
     Color color,
     PointF point) =>
 context.DrawText(
     new TextGraphicsOptions(new GraphicsOptions(), new TextOptions()
 {
     HorizontalAlignment = HorizontalAlignment.Center
 }),
     text,
     font,
     Brushes.Solid(color),
     null,
     point
     );
Ejemplo n.º 25
0
        /// <summary>
        /// Given a SixLabours ImageSharp image context, applies a watermark text overlay
        /// to the bottom right corner in the given font and colour.
        /// </summary>
        /// <param name="processingContext"></param>
        /// <param name="font"></param>
        /// <param name="text"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        private static IImageProcessingContext ApplyWaterMark(IImageProcessingContext processingContext,
                                                              Font font, string text, Color color)
        {
            Size imgSize = processingContext.GetCurrentSize();

            // measure the text size
            FontRectangle size = TextMeasurer.Measure(text, new RendererOptions(font));

            int ratio = 4; // Landscape, we make the text 25% of the width

            if (imgSize.Width >= imgSize.Height)
            {
                // Landscape - make it 1/6 of the width
                ratio = 6;
            }

            float quarter = imgSize.Width / ratio;

            // We want the text width to be 25% of the width of the image
            float scalingFactor = quarter / size.Width;

            // create a new font
            Font scaledFont = new Font(font, scalingFactor * font.Size);

            // 5% padding from the edge
            float fivePercent = quarter / 20;

            // 5% from the bottom right.
            var position = new PointF(imgSize.Width - fivePercent, imgSize.Height - fivePercent);

            var textGraphicOptions = new TextGraphicsOptions
            {
                TextOptions =
                {
                    HorizontalAlignment = HorizontalAlignment.Right,
                    VerticalAlignment   = VerticalAlignment.Bottom,
                }
            };

            return(processingContext.DrawText(textGraphicOptions, text, scaledFont, color, position));
        }
Ejemplo n.º 26
0
        private static IImageProcessingContext ApplyScalingWaterMarkSimple(this IImageProcessingContext processingContext,
                                                                           Font font,
                                                                           string text,
                                                                           Color color,
                                                                           float padding)
        {
            Size  imgsize      = processingContext.GetCurrentSize();
            float targetWidth  = imgsize.Width - (padding * 2);
            float targetHeight = imgsize.Height - (padding * 2);

            SizeF size          = TextMeasurer.Measure(text, new RendererOptions(font));
            float scalingFactor = Math.Min(imgsize.Width / size.Width, imgsize.Height / size.Height);

            Font scaledFont = new Font(font, scalingFactor * font.Size);

            var center             = new PointF(imgsize.Width / 2, imgsize.Height / 2);
            var textGraphicOptions = new TextGraphicsOptions(true)
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment   = VerticalAlignment.Center
            };

            return(processingContext.DrawText(textGraphicOptions, text, scaledFont, color, center));
        }
Ejemplo n.º 27
0
        public static IImageProcessingContext ApplyScalingWaterMark(this IImageProcessingContext processingContext, string text, Color color)
        {
            var(width, height) = processingContext.GetCurrentSize();
            var font               = SystemFonts.CreateFont("Arial", 10);
            var size               = TextMeasurer.Measure(text, new RendererOptions(font));
            var scalingFactor      = Math.Min(width / size.Width, height / size.Height);
            var scaledFont         = new Font(font, scalingFactor * font.Size);
            var center             = new PointF(width / 2, height / 2);
            var textGraphicOptions = new TextGraphicsOptions
            {
                TextOptions =
                {
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment   = VerticalAlignment.Center,
                },
                GraphicsOptions =
                {
                    BlendPercentage = (float)0.2,
                    Antialias       = true
                }
            };

            return(processingContext.DrawText(textGraphicOptions, text, scaledFont, color, center));
        }
Ejemplo n.º 28
0
        internal override Task <Matrix <float> > ApplyAsyncInternal(IImageProcessingContext <Rgba32> context)
        {
            if (_lastInput == null)
            {
                throw new InvalidOperationException("Input cannot be null.");
            }

            var scaledFont = CalculateScaleSize(_lastInput);

            var py = CenterHeight ? MaxHeight / 2 : 0;

            var hAlign             = CenterWidth ? HorizontalAlignment.Center : HorizontalAlignment.Left;
            var vAlign             = CenterHeight ? VerticalAlignment.Center : VerticalAlignment.Top;
            var textGraphicOptions = new TextGraphicsOptions(true)
            {
                HorizontalAlignment = hAlign,
                VerticalAlignment   = vAlign,
                WrapTextWidth       = MaxWidth
            };

            context.DrawText(textGraphicOptions, _lastInput, scaledFont, Brush, Pen, new PointF(0, py));

            return(Task.FromResult(GetProjectiveTransformationMatrix()));
        }
Ejemplo n.º 29
0
        private static IImageProcessingContext ApplyScalingWaterMarkWordWrap(
            this IImageProcessingContext processingContext,
            Font font,
            string text,
            Color color,
            float padding)
        {
            Size  imgSize      = processingContext.GetCurrentSize();
            float targetWidth  = imgSize.Width - (padding * 2);
            float targetHeight = imgSize.Height - (padding * 2);

            float targetMinHeight = imgSize.Height - (padding * 3);

            var   scaledFont = font;
            SizeF s          = new SizeF(float.MaxValue, float.MaxValue);

            float scaleFactor = (scaledFont.Size) / 2;
            int   trapCount   = (int)scaledFont.Size * 2;

            if (trapCount > 10)
            {
                trapCount = 10;
            }

            bool isTooSmall = false;

            while ((s.Height > targetHeight || s.Height < targetMinHeight) && trapCount > 0)
            {
                if (s.Height > targetHeight)
                {
                    if (isTooSmall)
                    {
                        scaleFactor = scaleFactor / 2;
                    }
                    scaledFont = new Font(scaledFont, scaledFont.Size - scaleFactor);
                    isTooSmall = false;
                }

                if (s.Height < targetHeight)
                {
                    if (!isTooSmall)
                    {
                        scaleFactor = scaleFactor / 2;
                    }
                    scaledFont = new Font(scaledFont, scaledFont.Size + scaleFactor);
                    isTooSmall = true;
                }

                trapCount--;

                s = TextMeasurer.Measure(text, new RendererOptions(scaledFont)
                {
                    WrappingWidth = targetWidth
                });
            }
            var center             = new PointF(padding, imgSize.Height / 2);
            var textGraphicOptions = new TextGraphicsOptions(true)
            {
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment   = VerticalAlignment.Center,
                WrapTextWidth       = targetWidth
            };

            return(processingContext.DrawText(textGraphicOptions, text, scaledFont, color, center));
        }
Ejemplo n.º 30
0
        public void DrawAxis(IImageProcessingContext ctx, float width, float height)
        {
            var font = SystemFonts.CreateFont("Arial", 20, SixLabors.Fonts.FontStyle.Regular);

            var blackPen      = Pens.Solid(Rgba32.ParseHex("#000000"), 1);
            var blackTransPen = Pens.Solid(Rgba32.ParseHex("#00005050"), 1);

            // Scale the cell size up until it's sane
            var cellSize = 1;

            while (cellSize * scale < 50)
            {
                cellSize++;
            }

            // Draw other thing
            for (int i = 1; i < width; i++)
            {
                var x = ScreenToX(i);

                // Draws every thing
                if (x % cellSize == 0)
                {
                    ctx.DrawLines(blackTransPen, new PointF[] { new PointF(i, 0), new PointF(i, height) });
                    ctx.DrawText(x.ToString(), font, new Rgba32(22, 33, 45, 66), new PointF(i, YToScreen(0)));
                }
            }

            // Draw other thing
            for (int i = 1; i < height; i++)
            {
                var y = ScreenToY(i);

                if (y % cellSize == 0)
                {
                    ctx.DrawLines(blackTransPen, new PointF[] { new PointF(0, i), new PointF(width, i) });

                    // skip drawing 0 twice
                    if (y != 0)
                    {
                        ctx.DrawText(y.ToString(), font, new Rgba32(22, 33, 45, 66), new PointF(XToScreen(0), i));
                    }
                }
            }

            // Draw x line
            for (int i = 1; i < width; i++)
            {
                var x = ScreenToX(i);

                if (x == 0)
                {
                    ctx.DrawLines(blackPen, new PointF[] { new PointF(i, 0), new PointF(i, height) });
                }
            }

            // Draw other thing
            for (int i = 1; i < height; i++)
            {
                var y = ScreenToY(i);

                if (y == 0)
                {
                    ctx.DrawLines(blackPen, new PointF[] { new PointF(0, i), new PointF(width, i) });
                }
            }
        }