public Bitmap Render(IMesh mesh, Topology.DCEL.DcelMesh dcel, int width = 800) { Bitmap bitmap; // Check if the specified width is reasonable if (width < 2 * Math.Sqrt(mesh.Vertices.Count)) { return(new Bitmap(1, 1)); } var bounds = mesh.Bounds; // World margin on each side float margin = (float)bounds.Height * 0.05f; float scale = width / ((float)bounds.Width + 2 * margin); var target = new Rectangle(0, 0, width, (int)((bounds.Height + 2 * margin) * scale)); bitmap = new Bitmap(width, target.Height, PixelFormat.Format32bppPArgb); using (var g = Graphics.FromImage(bitmap)) { g.Clear(colors.Background); g.SmoothingMode = SmoothingMode.HighQuality; var context = new RenderContext(new Projection(target), colors); context.Add(mesh, true); context.Add(dcel.Vertices.ToArray(), dcel.Edges, false); if (EnableRegions) { context.Add(GetRegions(mesh)); } if (!EnablePoints) { context.Enable(3, false); } var renderer = new LayerRenderer(); renderer.Context = context; renderer.RenderTarget = g; renderer.Render(); } return(bitmap); }
/// <summary> /// Renders the polygon to a bitmap. /// </summary> /// <param name="poly">The polygon.</param> /// <param name="width">The desired width (pixel) of the image.</param> /// <returns>The bitmap.</returns> /// <remarks> /// The width has to be at least 2 * sqrt(n), n the number of vertices. /// Otherwise, an empty bitmap /// </remarks> public Bitmap Render(Geometry.IPolygon poly, int width = 800) { Bitmap bitmap; // Check if the specified width is reasonable if (width < 2 * Math.Sqrt(poly.Points.Count)) { return(new Bitmap(1, 1)); } var bounds = poly.Bounds(); // World margin on each side float margin = (float)bounds.Height * 0.05f; float scale = width / ((float)bounds.Width + 2 * margin); var target = new Rectangle(0, 0, width, (int)((bounds.Height + 2 * margin) * scale)); bitmap = new Bitmap(width, target.Height, PixelFormat.Format32bppPArgb); using (var g = Graphics.FromImage(bitmap)) { g.Clear(colors.Background); g.SmoothingMode = SmoothingMode.HighQuality; var context = new RenderContext(new Projection(target), colors); context.Add(poly); if (!EnablePoints) { context.Enable(3, false); } var renderer = new LayerRenderer(); renderer.Context = context; renderer.RenderTarget = g; renderer.Render(); } return(bitmap); }
/// <summary> /// Export the mesh to PNG format. /// </summary> /// <param name="mesh">The current mesh.</param> /// <param name="width">The desired width (pixel) of the image.</param> /// <returns>The bitmap.</returns> /// <remarks> /// The width has to be at least 2 * sqrt(n), n the number of vertices. /// Otherwise, an empty bitmap /// </remarks> public Bitmap Render(Mesh mesh, int width = 800) { Bitmap bitmap; // Check if the specified width is reasonable if (width < 2 * Math.Sqrt(mesh.Vertices.Count)) { return new Bitmap(1, 1); } var bounds = mesh.Bounds; // World margin on each side float margin = (float)bounds.Height * 0.05f; float scale = width / ((float)bounds.Width + 2 * margin); var target = new Rectangle(0, 0, width, (int)((bounds.Height + 2 * margin) * scale)); bitmap = new Bitmap(width, target.Height, PixelFormat.Format32bppPArgb); using (var g = Graphics.FromImage(bitmap)) { g.Clear(colors.Background); g.SmoothingMode = SmoothingMode.HighQuality; var context = new RenderContext(new Projection(target), colors); context.Add(mesh, true); if (EnableRegions) { context.Add(GetRegions(mesh)); } if (!EnablePoints) { context.Enable(3, false); } var renderer = new LayerRenderer(); renderer.Context = context; renderer.RenderTarget = g; renderer.Render(); } return bitmap; }