//--------------------------------------------------------------------- private static void Arrows() { Action <Surface> draw = surface => { using (var c = new Context(surface)) { c.Scale(300, 300); // Is only relevant to PNG c.Antialias = Antialias.Subpixel; // adjust line width due scaling double ux = 1, uy = 1; c.DeviceToUserDistance(ref ux, ref uy); c.LineWidth = Math.Max(ux, uy); c.MoveTo(0, 0.1); c.LineTo(1, 0.1); c.MoveTo(0, 0.9); c.LineTo(1, 0.9); c.Stroke(); var arrow = new Arrow(); c.Color = KnownColors.Blue; arrow.DrawArrow(c, 0.1, 0.1, 0.2, 0.9); arrow.DrawVector(c, 0.2, 0.1, 0.3, 0.9); arrow = new OpenArrow(); c.Color = new Color(0, 1, 0); arrow.DrawArrow(c, 0.3, 0.1, 0.4, 0.9); arrow.DrawVector(c, 0.4, 0.1, 0.5, 0.9); arrow = new CircleArrow(0.01); c.Color = new Color(1, 0, 0); arrow.DrawArrow(c, 0.5, 0.1, 0.6, 0.9); arrow.DrawVector(c, 0.6, 0.1, 0.7, 0.9); } }; using (Surface surface = new PdfSurface("arrows.pdf", 300, 300)) { draw(surface); // PNG can also be created this way surface.WriteToPng("arrows.png"); } using (Surface surface = new PSSurface("arrows.eps", 300, 300)) draw(surface); using (Surface surface = new SvgSurface("arrows.svg", 300, 300)) draw(surface); }
//--------------------------------------------------------------------- private static void Gradient() { Action <Surface> draw = surface => { using (var c = new Context(surface)) { Gradient pat = new LinearGradient(0.0, 0.0, 0.0, 256.0); pat.AddColorStopRgba(1, 0, 0, 0, 1); pat.AddColorStopRgba(0, 1, 1, 1, 1); c.Rectangle(0, 0, 256, 256); c.SetSource(pat); c.Fill(); pat.Dispose(); pat = new RadialGradient(115.2, 102.4, 25.6, 102.4, 102.4, 128.0); pat.AddColorStopRgba(0, 1, 1, 1, 1); pat.AddColorStopRgba(1, 0, 0, 0, 1); c.SetSource(pat); c.Arc(128.0, 128.0, 76.8, 0, 2 * Math.PI); c.Fill(); pat.Dispose(); } }; using (Surface surface = new ImageSurface(Format.Argb32, 500, 500)) { draw(surface); surface.WriteToPng("gradient.png"); } using (Surface surface = new PdfSurface("gradient.pdf", 500, 500)) { draw(surface); surface.WriteToPng("gradient1.png"); } using (Surface surface = new SvgSurface("gradient.svg", 500, 500)) { draw(surface); surface.WriteToPng("gradient2.png"); } }
//--------------------------------------------------------------------- private static void Hexagon() { Func <double, PointD[]> getHexagonPoints = cellSize => { double ri = cellSize / 2; double r = 2 * ri / Math.Sqrt(3); var p1 = new PointD(0, r); var p2 = new PointD(ri, r / 2); var p3 = new PointD(ri, -r / 2); var p4 = new PointD(0, -r); var p5 = new PointD(-ri, -r / 2); var p6 = new PointD(-ri, r / 2); PointD[] hexagon = { p1, p2, p3, p4, p5, p6 }; return(hexagon); }; Action <Surface> draw = surface => { using (var c = new Context(surface)) { c.Scale(500, 500); // Hat nur für PNG Relevanz: c.Antialias = Antialias.Subpixel; // Linienweite, wegen Maßstab so: double ux = 1, uy = 1; c.InverseTransformDistance(ref ux, ref uy); c.LineWidth = Math.Max(ux, uy); PointD[] hexagon = getHexagonPoints(0.5); c.Save(); { c.Translate(0.5, 0.5); c.MoveTo(hexagon[0]); c.LineTo(hexagon[1]); c.LineTo(hexagon[2]); c.LineTo(hexagon[3]); c.LineTo(hexagon[4]); c.LineTo(hexagon[5]); c.ClosePath(); c.Stroke(); } c.Restore(); c.Color = new Color(0, 0, 1); ux = 0.1; uy = 0.1; c.InverseTransformDistance(ref ux, ref uy); c.LineWidth = Math.Max(ux, uy); c.MoveTo(0.5, 0); c.LineTo(0.5, 1); c.MoveTo(0, 0.5); c.LineTo(1, 0.5); c.Stroke(); } }; using (Surface surface = new ImageSurface(Format.Argb32, 500, 500)) { draw(surface); surface.WriteToPng("hexagon.png"); } using (Surface surface = new PdfSurface("hexagon.pdf", 500, 500)) { draw(surface); surface.WriteToPng("hexagon1.png"); } using (Surface surface = new SvgSurface("hexagon.svg", 500, 500)) { draw(surface); surface.WriteToPng("hexagon2.png"); } }
//--------------------------------------------------------------------- private static void PaintAfter() { Pattern pattern = null; using (Surface surface = new SvgSurface("test.svg", 300, 300)) using (var c = new Context(surface)) { // Draw to group c.PushGroup(); { var hex = new Hexagon(150); hex.Fill(c, 150, 150, new Color(0.8, 0.8, 0.8)); } // Get group pattern = c.PopGroup(); c.Source = pattern; // Draw (without mask, hence everything that's in the source) c.Paint(); } using (Surface pdfSurface = new PdfSurface("test1.pdf", 300, 300)) using (Surface svgSurface = new SvgSurface("test1.svg", 300, 300)) using (var c = new Context(svgSurface)) { c.PushGroup(); { c.Source = pattern; c.Paint(); //pattern.Destroy(); c.Color = new Color(0, 0, 1); c.LineWidth = 0.1; c.MoveTo(0, 150); c.LineTo(300, 150); c.MoveTo(150, 0); c.LineTo(150, 300); c.Stroke(); c.Color = new Color(0, 0, 0); //c.SelectFontFace("Georgia", FontSlant.Normal, FontWeight.Bold); //c.SelectFontFace("Rockwell", FontSlant.Normal, FontWeight.Normal); c.SelectFontFace("Arial", FontSlant.Normal, FontWeight.Normal); //c.SelectFontFace("Times New Roman", FontSlant.Normal, FontWeight.Normal); c.SetFontSize(16); string text = "Hexagon with coordinate-axis"; //string text = "III"; // Determine the width of the text double textWidth = c.GetTextWidth(text); c.Save(); c.Translate((300 - textWidth) / 2, 16); { c.ShowText(text); } c.Restore(); c.MoveTo(0, 0); c.LineTo(300, 300); c.MoveTo(0, 300); c.LineTo(300, 0); c.Stroke(); c.Save(); c.Translate(16, 300); c.Rotate(-Math.PI / 2); c.Translate((300 - textWidth) / 2, 0); { c.ShowText(text); } c.Restore(); // Draw scala c.Save(); c.Translate(270, 20); { // Farbverlauf: Gradient linpat = new LinearGradient(0, 0, 0, 260); linpat.AddColorStop(0, new Color(0, 1, 0)); linpat.AddColorStop(0.5, new Color(0, 0, 1)); linpat.AddColorStop(1, new Color(1, 0, 0)); c.Rectangle(0, 0, 20, 260); c.Source = linpat; //c.Fill(); c.FillPreserve(); c.Color = new Color(0, 0, 0); c.LineWidth = 1; //c.Rectangle(0, 0, 20, 260); c.Stroke(); } c.Restore(); } pattern = c.PopGroup(); c.Source = pattern; c.Paint(); using (var c1 = new Context(pdfSurface)) { c1.Color = new Color(1, 1, 1, 0); c1.Rectangle(0, 0, 300, 300); c1.Fill(); c1.Source = pattern; c1.Paint(); pdfSurface.WriteToPng("test1.png"); } } }