/// <summary> /// Draws the text onto the the image filled via the brush then outlined via the pen. /// </summary> /// <typeparam name="TPixel">The type of the color.</typeparam> /// <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> /// <param name="options">The options.</param> /// <returns> /// The <see cref="Image{TPixel}" />. /// </returns> public static Image <TPixel> DrawText <TPixel>(this Image <TPixel> source, string text, Font font, IBrush <TPixel> brush, IPen <TPixel> pen, Vector2 location, TextGraphicsOptions options) where TPixel : struct, IPixel <TPixel> { Vector2 dpi = DefaultTextDpi; if (options.UseImageResolution) { dpi = new Vector2((float)source.MetaData.HorizontalResolution, (float)source.MetaData.VerticalResolution); } var style = new FontSpan(font, dpi) { ApplyKerning = options.ApplyKerning, TabWidth = options.TabWidth, WrappingWidth = options.WrapTextWidth, HorizontalAlignment = options.HorizontalAlignment, VerticalAlignment = options.VerticalAlignment }; IPathCollection glyphs = TextBuilder.GenerateGlyphs(text, location, style); var pathOptions = (GraphicsOptions)options; if (brush != null) { source.Fill(brush, glyphs, pathOptions); } if (pen != null) { source.Draw(pen, glyphs, pathOptions); } return(source); }
/// <summary> /// Draws the text onto the the image filled via the brush then outlined via the pen. /// </summary> /// <typeparam name="TPixel">The type of the color.</typeparam> /// <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> /// <param name="options">The options.</param> /// <returns> /// The <see cref="Image{TPixel}" />. /// </returns> public static Image <TPixel> DrawText <TPixel>(this Image <TPixel> source, string text, Font font, IBrush <TPixel> brush, IPen <TPixel> pen, Vector2 location, TextGraphicsOptions options) where TPixel : struct, IPixel <TPixel> { GlyphBuilder glyphBuilder = new GlyphBuilder(location); TextRenderer renderer = new TextRenderer(glyphBuilder); Vector2 dpi = DefaultTextDpi; if (options.UseImageResolution) { dpi = new Vector2((float)source.MetaData.HorizontalResolution, (float)source.MetaData.VerticalResolution); } FontSpan style = new FontSpan(font, dpi) { ApplyKerning = options.ApplyKerning, TabWidth = options.TabWidth, WrappingWidth = options.WrapTextWidth, Alignment = options.TextAlignment }; renderer.RenderText(text, style); System.Collections.Generic.IEnumerable <SixLabors.Shapes.IPath> shapesToDraw = glyphBuilder.Paths; GraphicsOptions pathOptions = (GraphicsOptions)options; if (brush != null) { foreach (SixLabors.Shapes.IPath s in shapesToDraw) { source.Fill(brush, s, pathOptions); } } if (pen != null) { foreach (SixLabors.Shapes.IPath s in shapesToDraw) { source.Draw(pen, s, pathOptions); } } return(source); }
/// <summary> /// Draws the text onto the the image filled via the brush then outlined via the pen. /// </summary> /// <typeparam name="TColor">The type of the color.</typeparam> /// <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> /// <param name="options">The options.</param> /// <returns> /// The <see cref="Image{TColor}" />. /// </returns> public static Image <TColor> DrawTextAlongPath <TColor>(this Image <TColor> source, string text, Font font, IBrush <TColor> brush, IPath path, TextGraphicsOptions options) where TColor : struct, IPixel <TColor> { Vector2 dpi = DefaultTextDpi; if (options.UseImageResolution) { dpi = new Vector2((float)source.MetaData.HorizontalResolution, (float)source.MetaData.VerticalResolution); } FontSpan style = new FontSpan(font, dpi) { ApplyKerning = options.ApplyKerning, TabWidth = options.TabWidth, WrappingWidth = options.WrapTextWidth, Alignment = TextAlignment.Center }; var size = new TextMeasurer().MeasureText(text, style); WordArtGlyphBuilder glyphBuilder = new WordArtGlyphBuilder(path, size); TextRenderer renderer = new TextRenderer(glyphBuilder); renderer.RenderText(text, style); System.Collections.Generic.IEnumerable <SixLabors.Shapes.IPath> shapesToDraw = glyphBuilder.Paths; GraphicsOptions pathOptions = (GraphicsOptions)options; if (brush != null) { foreach (SixLabors.Shapes.IPath s in shapesToDraw) { source.Fill(brush, s, pathOptions); } } return(source); }
static void ApplyWaterMarkScaledWordWrap(Font font, string text, string inputPath, string outputPath) { using (Image img = Image.Load(inputPath)) { float padding = 20; // margin in 20px float targetWidth = img.Width - (padding * 2); float targetHeight = img.Height - (padding * 2); float targetMinHeight = img.Height - (padding * 3); // must be with in a margin width of the target height TextMeasurer measurer = new TextMeasurer(); // now we are working i 2 dimensions at once and cant just scale because it will cause the text to // reflow we need to just try multiple times var f = font; SixLabors.Fonts.Size s = new SixLabors.Fonts.Size(float.MaxValue, float.MaxValue); float scaleFactor = (f.Size / 2);// everytime we change direction we half this size int trapCount = (int)f.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; } f = new Font(f, f.Size - scaleFactor); isTooSmall = false; } if (s.Height < targetMinHeight) { if (!isTooSmall) { scaleFactor = scaleFactor / 2; } f = new Font(f, f.Size + scaleFactor); isTooSmall = true; } trapCount--; var style = new FontSpan(f, 72) { WrappingWidth = targetWidth }; s = measurer.MeasureText(text, style); } Color fill = new Color(128, 128, 128, 200); img.DrawText(text, f , fill, new Vector2(padding), new ImageSharp.Drawing.TextGraphicsOptions { WrapTextWidth = targetWidth }); img.Save(outputPath); } }