public static void SaveImage(this IPathCollection shape, params string[] path) { shape = shape.Translate(-shape.Bounds.Location) // touch top left .Translate(new Vector2(10)); // move in from top left var fullPath = System.IO.Path.GetFullPath(System.IO.Path.Combine("Output", System.IO.Path.Combine(path))); // pad even amount around shape int width = (int)(shape.Bounds.Left + shape.Bounds.Right); int height = (int)(shape.Bounds.Top + shape.Bounds.Bottom); using (var img = new Image <Rgba32>(width, height)) { img.Fill(Rgba32.DarkBlue); foreach (var s in shape) { // In ImageSharp.Drawing.Paths there is an extension method that takes in an IShape directly. img.Fill(Rgba32.HotPink, s, new ImageSharp.GraphicsOptions(true)); } // img.Draw(Color.LawnGreen, 1, new ShapePath(shape)); // Ensure directory exists Directory.CreateDirectory(System.IO.Path.GetDirectoryName(fullPath)); img.Save(fullPath); } }
public void DrawGlyphToImage(int charCode, System.Drawing.Point location, Image <Rgba32> image) { int charIndex = charCode - FirstChar; IPathCollection paths = glyphPaths[charIndex]; paths = paths.Translate(location.X - renderOffsets[charIndex].X, location.Y - renderOffsets[charIndex].Y); DrawColoredPaths(image, paths, pathColors?[charIndex]); }
private static IPathCollection GetGlyphs(string text, Size targetSize, FontFamily fontFamily) { Font font = new Font(fontFamily, 100); // size doesn't matter too much as we will be scaling shortly anyway RendererOptions style = new RendererOptions(font, 72); // again dpi doesn't overlay matter as this code genreates a vector style.Origin = new System.Numerics.Vector2(targetSize.Width / 2, 0); // this is the important line, where we render the glyphs to a vector instead of directly to the image // this allows further vector manipulation (scaling, translating) etc without the expensive pixel operations. IPathCollection glyphs = TextBuilder.GenerateGlyphs(text, style); var widthScale = (targetSize.Width / glyphs.Bounds.Width); var heightScale = (targetSize.Height / glyphs.Bounds.Height); var minScale = Math.Min(widthScale, heightScale); // scale so that it will fit exactly in image shape once rendered glyphs = glyphs.Scale(minScale); // move the vectorised glyph so that it touchs top and left edges // could be tweeked to center horizontaly & vertically here glyphs = glyphs.Translate(-glyphs.Bounds.Location); //glyphs = glyphs.Translate(0, targetSize.Height * 3); return(glyphs); }
/// <summary> /// Creates a path translated by the supplied postion /// </summary> /// <param name="path">The path to translate.</param> /// <param name="x">The amount to translate along the X axis.</param> /// <param name="y">The amount to translate along the Y axis.</param> /// <returns>A <see cref="IPath"/> with a translate transform applied.</returns> public static IPathCollection Translate(this IPathCollection path, float x, float y) { return(path.Translate(new PointF(x, y))); }