Ejemplo n.º 1
0
        //---------------------------------------------------------------------
        private static void Mask()
        {
            Action <Surface> draw = surface =>
            {
                using (var ctx = new Context(surface))
                {
                    ctx.Scale(500, 500);

                    Gradient linpat = new LinearGradient(0, 0, 1, 1);
                    linpat.AddColorStop(0, new Color(0, 0.3, 0.8));
                    linpat.AddColorStop(1, new Color(1, 0.8, 0.3));

                    Gradient radpat = new RadialGradient(0.5, 0.5, 0.25, 0.5, 0.5, 0.6);
                    radpat.AddColorStop(0, new Color(0, 0, 0, 1));
                    radpat.AddColorStop(1, new Color(0, 0, 0, 0));

                    ctx.Source = linpat;
                    //ctx.Paint();
                    ctx.Mask(radpat);
                }
            };

            using (Surface surface = new ImageSurface(Format.Argb32, 500, 500))
            {
                draw(surface);
                surface.WriteToPng("mask.png");
            }

            using (Surface surface = new PdfSurface("mask.pdf", 500, 500))
                draw(surface);

            using (Surface surface = new SvgSurface("mask.svg", 500, 500))
                draw(surface);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Exports the specified <see cref="PlotModel" /> to a png file.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="fileName">Name of the output file.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="background">The background color.</param>
        public static void Export(IPlotModel model, string fileName, int width, int height, Pattern background = null)
        {
            using (var bm = new ImageSurface(Format.ARGB32, width, height))
            {
                using (var g = new Context(bm))
                {
                    if (background != null)
                    {
                        g.Save();
                        g.SetSource(background);
                        g.Rectangle(0, 0, width, height);
                        g.Fill();
                        g.Restore();
                    }

                    var rc = new GraphicsRenderContext {
                        RendersToScreen = false
                    };
                    rc.SetGraphicsTarget(g);
                    model.Update(true);
                    model.Render(rc, width, height);
                    bm.WriteToPng(fileName);
                }
            }
        }
Ejemplo n.º 3
0
        public Image Copy()
        {
            Gdk.Colormap colormap = Gdk.Colormap.System;

            /* FIXME: copying a surface to a pixbuf through a pixmap does not require writting / reading to a file,
             * but it does not handle transparencies correctly and draws transparent pixels in a black color, so
             * for now we force the first method */
            colormap = null;

            /* In unit tests running without a display, the default Screen is null and we can't get a valid colormap.
             * In this scenario we use a fallback that writes the surface to a temporary file */
            if (colormap == null)
            {
                string tempFile = System.IO.Path.GetTempFileName();
                surface.WriteToPng(tempFile);
                Gdk.Pixbuf pixbuf = new Gdk.Pixbuf(tempFile);
                return(new Image(pixbuf));
            }
            else
            {
                Gdk.Pixmap pixmap = new Gdk.Pixmap(null, Width, Height, 24);
                using (Context cr = Gdk.CairoHelper.Create(pixmap)) {
                    cr.Operator = Operator.Source;
                    cr.SetSource(surface);
                    cr.Paint();
                }
                return(new Image(Gdk.Pixbuf.FromDrawable(pixmap, Gdk.Colormap.System, 0, 0, 0, 0, Width, Height)));
            }
        }
Ejemplo n.º 4
0
        static void Main()
        {
            // The using statement ensures that potentially heavy objects
            // are disposed immediately.
            using (ImageSurface draw = new ImageSurface(Format.Argb32, 70, 150))
            {
                using (Context gr = new Context(draw))
                {
                    gr.Antialias = Antialias.Subpixel;        // sets the anti-aliasing method
                    gr.LineWidth = 9;                         // sets the line width
                    gr.SetSourceColor(new Color(0, 0, 0, 1)); // red, green, blue, alpha
                    gr.MoveTo(10, 10);                        // sets the Context's start point.
                    gr.LineTo(40, 60);                        // draws a "virtual" line from 5,5 to 20,30
                    gr.Stroke();                              //stroke the line to the image surface

                    gr.Antialias = Antialias.Gray;
                    gr.LineWidth = 8;
                    gr.SetSourceColor(new Color(1, 0, 0, 1));
                    gr.LineCap = LineCap.Round;
                    gr.MoveTo(10, 50);
                    gr.LineTo(40, 100);
                    gr.Stroke();

                    gr.Antialias = Antialias.None;    //fastest method but low quality
                    gr.LineWidth = 7;
                    gr.MoveTo(10, 90);
                    gr.LineTo(40, 140);
                    gr.Stroke();

                    draw.WriteToPng("antialias.png");  //save the image as a png image.
                }
            }
        }
Ejemplo n.º 5
0
    Knockout()
    {
        Surface s   = new ImageSurface(Format.ARGB32, 400, 400);
        Context ctx = new Context(s);

        Draw(ctx, 400, 400);
        s.WriteToPng("knockout.png");
    }
Ejemplo n.º 6
0
        public Image Copy()
        {
            string tempFile = System.IO.Path.GetTempFileName();

            surface.WriteToPng(tempFile);
            Gdk.Pixbuf pixbuf = new Gdk.Pixbuf(tempFile);
            return(new Image(pixbuf));
        }
Ejemplo n.º 7
0
	static void Main ()
	{		
		Surface s = new ImageSurface (Format.ARGB32, 500, 500);
		Cairo.Context g = new Cairo.Context (s);

		draw (g, 500, 500);
		
		s.WriteToPng ("image.png");
	}
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            int width  = 400;
            int height = 400;

            using (Surface surface = new ImageSurface(Cairo.Format.Rgb24, width, height))
            {
                surface.WriteToPng("plplot1.png");
            }
        }
Ejemplo n.º 9
0
    public void CreatePng()
    {
        const int Width     = 480;
        const int Height    = 160;
        const int CheckSize = 10;
        const int Spacing   = 2;

        // Create an Image-based surface with data stored in ARGB32 format and a context,
        // "using" is used here to ensure that they are Disposed once we are done
        using (ImageSurface surface = new ImageSurface(Format.ARGB32, Width, Height))
            using (Context cr = new Cairo.Context(surface))
            {
                // Start drawing a checkerboard
                int i, j, xcount, ycount;
                xcount = 0;
                i      = Spacing;
                while (i < Width)
                {
                    j      = Spacing;
                    ycount = xcount % 2;             // start with even/odd depending on row
                    while (j < Height)
                    {
                        if (ycount % 2 != 0)
                        {
                            cr.SetSourceRGB(1, 0, 0);
                        }
                        else
                        {
                            cr.SetSourceRGB(1, 1, 1);
                        }
                        // If we're outside the clip, this will do nothing.
                        cr.Rectangle(i, j, CheckSize, CheckSize);
                        cr.Fill();

                        j += CheckSize + Spacing;
                        ++ycount;
                    }
                    i += CheckSize + Spacing;
                    ++xcount;
                }

                // Select a font to draw with
                cr.SelectFontFace("serif", FontSlant.Normal, FontWeight.Bold);
                cr.SetFontSize(64.0);

                // Select a color (blue)
                cr.SetSourceRGB(0, 0, 1);

                // Draw
                cr.MoveTo(20, 100);
                cr.ShowText("Hello, World");

                surface.WriteToPng("test.png");
            }
    }
Ejemplo n.º 10
0
        static BitmapContent CreateBitmapContent(
            double bitmapWidth,
            double bitmapHeight,
            string fontName,
            FontSlant fontSlant,
            FontWeight fontWeight,
            double fontSize,
            List <CharacterData> cds,
            ParsedPath pngFile)
        {
            using (ImageSurface surface = new ImageSurface(Format.Argb32, (int)bitmapWidth, (int)bitmapHeight))
            {
                using (Context g = new Context(surface))
                {
                    SetupContext(g, fontName, fontSlant, fontWeight, fontSize);
                    double x = 0;

                    for (int i = 0; i < cds.Count; i++)
                    {
                        CharacterData cd = cds[i];

                        if (cd.Location.Width == 0)
                        {
                            continue;
                        }

                        g.MoveTo(x + cd.Bearing.X, cd.Bearing.Y);
                        g.ShowText(cd.Character);
#if DEBUG
                        g.Save();
                        g.Color     = new Color(1.0, 0, 0, 0.5);
                        g.Antialias = Antialias.None;
                        g.LineWidth = 1;
                        g.MoveTo(x + 0.5, 0.5);
                        g.LineTo(x + cd.Location.Width - 0.5, 0);
                        g.LineTo(x + cd.Location.Width - 0.5, cd.Location.Height - 0.5);
                        g.LineTo(x + 0.5, cd.Location.Height - 0.5);
                        g.LineTo(x + 0.5, 0.5);
                        g.Stroke();
                        g.Restore();
#endif
                        x += cd.Location.Width;
                    }

                    g.Restore();
                }

                if (pngFile != null)
                {
                    surface.WriteToPng(pngFile);
                }

                return(new BitmapContent(SurfaceFormat.Color, surface.Width, surface.Height, surface.Data));
            }
        }
Ejemplo n.º 11
0
        //---------------------------------------------------------------------
        private static void AntiAlias()
        {
            Action <Surface> draw = surface =>
            {
                using (var ctx = new Context(surface))
                {
                    // Sets the anti-aliasing method:
                    ctx.Antialias = Antialias.Subpixel;

                    // Sets the line width:
                    ctx.LineWidth = 9;

                    // red, green, blue, alpha:
                    ctx.Color = new Color(0, 0, 0, 1);

                    // Sets the Context's start point:
                    ctx.MoveTo(10, 10);

                    // Draws a "virtual" line:
                    ctx.LineTo(40, 60);

                    // Stroke the line to the image surface:
                    ctx.Stroke();

                    ctx.Antialias = Antialias.Gray;
                    ctx.LineWidth = 8;
                    ctx.Color     = new Color(1, 0, 0, 1);
                    ctx.LineCap   = LineCap.Round;
                    ctx.MoveTo(10, 50);
                    ctx.LineTo(40, 100);
                    ctx.Stroke();

                    // Fastest method but low quality:
                    ctx.Antialias = Antialias.None;
                    ctx.LineWidth = 7;
                    ctx.MoveTo(10, 90);
                    ctx.LineTo(40, 140);
                    ctx.Stroke();
                }
            };

            using (Surface surface = new ImageSurface(Format.Argb32, 70, 150))
            {
                draw(surface);

                // Save the image as a png image:
                surface.WriteToPng("antialias.png");
            }

            using (Surface surface = new PdfSurface("antialias.pdf", 70, 150))
                draw(surface);

            using (Surface surface = new SvgSurface("antialias.svg", 70, 150))
                draw(surface);
        }
Ejemplo n.º 12
0
        //---------------------------------------------------------------------
        private static void Shapes()
        {
            Action <Surface> draw = surface =>
            {
                using (var c = new Context(surface))
                {
                    c.Antialias = Antialias.Subpixel;

                    // Hexagon:
                    Shape shape = new Hexagon(50);
                    shape.Draw(c, 50, 50);
                    shape.Fill(c, 50, 50, new Color(0.5, 0.5, 0.5));

                    // Square:
                    shape = new Square(50);
                    shape.Draw(c, 150, 50);
                    shape.Fill(c, 150, 50, new Color(0.5, 0.5, 0.5));

                    // Circle:
                    shape = new Circle(50);
                    shape.Draw(c, 100, 150);
                    shape.Fill(c, 100, 150, new Color(0.5, 0.5, 0.5));

                    // Bounding box:
                    var boundingBox = new Square(50);
                    c.LineWidth = 1;
                    var red = new Color(1, 0, 0);
                    boundingBox.Draw(c, 50, 50, red);
                    boundingBox.Draw(c, 150, 50, red);
                    boundingBox.Draw(c, 100, 150, red);
                }
            };

            using (Surface surface = new ImageSurface(Format.Argb32, 180, 180))
            {
                draw(surface);
                surface.WriteToPng("shapes.png");
            }

            using (Surface surface = new PdfSurface("shapes.pdf", 180, 180))
                draw(surface);

            using (Surface surface = new PSSurface("shapes.eps", 300, 300))
                draw(surface);

            using (Surface surface = new SvgSurface("shapes.svg", 180, 180))
                draw(surface);
        }
Ejemplo n.º 13
0
        public static void CombinePngs(List <ImagePlacement> placements, ParsedPath pngPath)
        {
            try
            {
                int w = 0;
                int h = 0;

                foreach (var placement in placements)
                {
                    int wt = (int)placement.TargetRectangle.Width;
                    int ht = (int)placement.TargetRectangle.Height;

                    if (wt > w)
                    {
                        w = wt;
                    }

                    if (ht > h)
                    {
                        h = ht;
                    }
                }

                using (ImageSurface combinedImage = new ImageSurface(Format.Argb32, w, h))
                {
                    using (Cairo.Context g = new Cairo.Context(combinedImage))
                    {
                        foreach (var placement in placements)
                        {
                            using (ImageSurface image = new ImageSurface(placement.ImageFile))
                            {
                                int x = (int)placement.TargetRectangle.X;
                                int y = (int)placement.TargetRectangle.Y;

                                g.SetSourceSurface(image, x, y);
                                g.Paint();
                            }
                        }
                    }

                    combinedImage.WriteToPng(pngPath);
                }
            }
            catch (Exception e)
            {
                throw new ArgumentException("Unable to combine images into file '{0}'".CultureFormat(pngPath), e);
            }
        }
Ejemplo n.º 14
0
        public void ExportToPng(string path)
        {
            //ReadGepmetry
            int fX, fY, fWidth, fHeight, fDepth;

            GdkWindow.GetGeometry(out fX, out fY, out fWidth, out fHeight, out fDepth);
            using (ImageSurface draw = new ImageSurface(Format.Argb32, fWidth, fHeight)) {
                using (Context gr = new Context(draw)) {
                    DrawPrimitives(_pro.Get(), gr);
                    DrawLines(_pro.Lines(), gr);
                    DrawConnectors(_pro.Connectors(), gr);

                    draw.WriteToPng(path);
                }
            }
        }
Ejemplo n.º 15
0
        //---------------------------------------------------------------------
        private static void Demo02()
        {
            Action <Surface> draw = surface =>
            {
                using (var c = new Context(surface))
                {
                    c.Scale(500, 500);

                    Gradient radpat = new RadialGradient(0.25, 0.25, 0.1, 0.5, 0.5, 0.5);
                    radpat.AddColorStop(0, new Color(1.0, 0.8, 0.8));
                    radpat.AddColorStop(1, new Color(0.9, 0.0, 0.0));

                    for (int i = 1; i < 10; i++)
                    {
                        for (int j = 1; j < 10; j++)
                        {
                            c.Rectangle(i / 10d - 0.04, j / 10d - 0.04, 0.08, 0.08);
                        }
                    }
                    c.Source = radpat;
                    c.Fill();

                    Gradient linpat = new LinearGradient(0.25, 0.35, 0.75, 0.65);
                    linpat.AddColorStop(0.00, new Color(1, 1, 1, 0));
                    linpat.AddColorStop(0.25, new Color(0, 1, 0, 0.5));
                    linpat.AddColorStop(0.50, new Color(1, 1, 1, 0));
                    linpat.AddColorStop(0.75, new Color(0, 0, 1, 0.5));
                    linpat.AddColorStop(1.00, new Color(1, 1, 1, 0));

                    c.Rectangle(0, 0, 1, 1);
                    c.Source = linpat;
                    c.Fill();
                }
            };

            using (Surface surface = new ImageSurface(Format.Argb32, 500, 500))
            {
                draw(surface);
                surface.WriteToPng("demo02.png");
            }

            using (Surface surface = new PdfSurface("demo02.pdf", 500, 500))
                draw(surface);

            using (Surface surface = new SvgSurface("demo02.svg", 500, 500))
                draw(surface);
        }
Ejemplo n.º 16
0
        internal static void DrawNode(Node node, [CallerMemberName] string memberName = "")
        {
            using (ImageSurface surface = new ImageSurface(Format.Argb32, (int)node.Rect.Width, (int)node.Rect.Height))
                using (Context context = new Context(surface))
                {
                    Draw(context, node);

                    if (!Directory.Exists(OutputPath))
                    {
                        Directory.CreateDirectory(OutputPath);
                    }

                    string filePath = OutputPath + "\\" + DateTime.UtcNow.ToString("yyyy-MM-dd_HH-mm-ss-fff_") + surface.GetHashCode() + memberName + ".png";
                    surface.WriteToPng(filePath);
                    Util.OpenImage(filePath);
                }
        }
Ejemplo n.º 17
0
        public static void Main(string[] args)
        {
            // call the snippets
            Snippets snip = new Snippets();

            foreach (string snippet in Snippets.snippets)
            {
                string  filename = "./" + snippet + ".png";
                Surface surface  = new ImageSurface(Format.ARGB32, IMAGE_WIDTH, IMAGE_WIDTH);
                Context cr       = new Context(surface);

                cr.Save();
                Snippets.InvokeSnippet(snip, snippet, cr, IMAGE_WIDTH, IMAGE_HEIGHT);
                surface.WriteToPng(filename);
                cr.Restore();
            }
        }
Ejemplo n.º 18
0
        private void Save(string filename, bool bSource, bool bDrawings)
        {
            Surface pngSurface = new ImageSurface(Format.ARGB32, sourceWidth, sourceHeight);

            using (Context c = new Context(pngSurface)) {
                if (bSource)
                {
                    c.SetSourceSurface(source, 0, 0);
                    c.Paint();
                }
                if (bDrawings)
                {
                    c.SetSourceSurface(drawings, 0, 0);
                    c.PaintWithAlpha(transparency);
                }
            }
            pngSurface.WriteToPng(filename);
        }
Ejemplo n.º 19
0
        //---------------------------------------------------------------------
        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");
            }
        }
Ejemplo n.º 20
0
        //---------------------------------------------------------------------
        private static void Demo01()
        {
            Action <Surface> draw = surface =>
            {
                using (var c = new Context(surface))
                {
                    c.Scale(500, 500);

                    c.Color = new Color(0, 0, 0);
                    c.MoveTo(0, 0);         // absetzen und neu beginnen
                    c.LineTo(1, 1);
                    c.MoveTo(1, 0);
                    c.LineTo(0, 1);
                    c.LineWidth = 0.2;
                    c.Stroke();             // Lininen zeichnen

                    c.Rectangle(0, 0, 0.5, 0.5);
                    c.Color = new Color(1, 0, 0, 0.8);
                    c.Fill();

                    c.Rectangle(0, 0.5, 0.5, 0.5);
                    c.Color = new Color(0, 1, 0, 0.6);
                    c.Fill();

                    c.Rectangle(0.5, 0, 0.5, 0.5);
                    c.Color = new Color(0, 0, 0, 0.4);
                    c.Fill();
                }
            };

            using (Surface surface = new ImageSurface(Format.Argb32, 500, 500))
            {
                draw(surface);
                surface.WriteToPng("demo01.png");
            }

            using (Surface surface = new PdfSurface("demo01.pdf", 500, 500))
                draw(surface);

            using (Surface surface = new SvgSurface("demo01.svg", 500, 500))
                draw(surface);
        }
Ejemplo n.º 21
0
        public static void RenderPng(SimulationCoordinateSpace coordinateSpace,
                                     INetworkDatabase ndb,
                                     IUnitDatabase units,
                                     IDictionary <int, TrainControlStateSummary>?controllerMap,
                                     string filename,
                                     int w, int h,
                                     double scale, int fontSize)
        {
            var center = new PointD(w / 2, h / 2);

            var     surf = new ImageSurface(Format.Argb32, w, h);
            Context cr   = new Context(surf);

            RenderToContext(coordinateSpace, ndb, units, controllerMap, cr, center, scale, fontSize);

            cr.Dispose();
            surf.WriteToPng(filename);
            surf.Finish();
            surf.Dispose();
        }
Ejemplo n.º 22
0
        public static void RotatePng(ParsedPath pngPath, ImageRotation rotation)
        {
            if (rotation == ImageRotation.None)
            {
                return;
            }

            using (ImageSurface originalImage = new ImageSurface(pngPath))
            {
                int w;
                int h;

                if (rotation == ImageRotation.Left || rotation == ImageRotation.Right)
                {
                    w = originalImage.Height;
                    h = originalImage.Width;
                }
                else
                {
                    w = originalImage.Width;
                    h = originalImage.Height;
                }

                double[] rotationRadians = { 0, -Math.PI / 2, Math.PI / 2, Math.PI };

                using (ImageSurface rotatedImage = new ImageSurface(Format.Argb32, w, h))
                {
                    using (Cairo.Context g = new Cairo.Context(rotatedImage))
                    {
                        g.Translate(rotatedImage.Width / 2.0, rotatedImage.Height / 2.0);
                        g.Rotate(rotationRadians[(int)rotation]);
                        g.Translate(-originalImage.Width / 2.0, -originalImage.Height / 2.0);

                        g.SetSourceSurface(originalImage, 0, 0);
                        g.Paint();
                    }

                    rotatedImage.WriteToPng(pngPath);
                }
            }
        }
Ejemplo n.º 23
0
        internal static void DrawPathPrimitive(PathPrimitive primitive, [CallerMemberName]
                                               string memberName = "")
        {
            var size = GetPrimitiveSize(primitive, out Point minPoint);

            using (ImageSurface surface = new ImageSurface(Format.Argb32, (int)size.Width, (int)size.Height))
                using (Context context = new Context(surface))
                {
                    context.Translate(-minPoint.x, -minPoint.y);
                    Draw(context, primitive);

                    if (!Directory.Exists(OutputPath))
                    {
                        Directory.CreateDirectory(OutputPath);
                    }

                    string filePath = OutputPath + "\\" + DateTime.UtcNow.ToString("yyyy-MM-dd_HH-mm-ss-fff_") + surface.GetHashCode() + memberName + ".png";
                    surface.WriteToPng(filePath);
                    Util.OpenImage(filePath);
                }
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Exports the specified <see cref="PlotModel" /> to the specified <see cref="Stream" />.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="stream">The output stream.</param>
        public void Export(IPlotModel model, Stream stream)
        {
            using (var bm = new ImageSurface(Format.ARGB32, this.Width, this.Height))
            {
                using (var g = new Context(bm))
                {
                    if (this.Background.IsVisible())
                    {
                        g.Save();
                        using (var pattern = new SolidPattern(this.Background.R, this.Background.G, this.Background.B, this.Background.A))
                        {
                            g.SetSource(pattern);
                            g.Rectangle(0, 0, this.Width, this.Height);
                            g.Fill();
                        }

                        g.Restore();
                    }

                    var rc = new GraphicsRenderContext {
                        RendersToScreen = false
                    };
                    rc.SetGraphicsTarget(g);
                    model.Update(true);
                    model.Render(rc, this.Width, this.Height);

                    // write to a temporary file
                    var tmp = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid() + ".png");
                    bm.WriteToPng(tmp);
                    var bytes = File.ReadAllBytes(tmp);

                    // write to the stream
                    stream.Write(bytes, 0, bytes.Length);

                    // delete the temporary file
                    File.Delete(tmp);
                }
            }
        }
Ejemplo n.º 25
0
        //---------------------------------------------------------------------
        private static void Arrow()
        {
            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);

                    c.Color = new Color(0, 0, 1);
                    c.MoveTo(0.1, 0.10);
                    c.LineTo(0.9, 0.45);
                    c.Stroke();

                    c.Arrow(0.1, 0.50, 0.9, 0.95, 0.05, 10);
                    c.Stroke();
                }
            };

            using (Surface surface = new ImageSurface(Format.Argb32, 500, 500))
            {
                draw(surface);
                surface.WriteToPng("arrow.png");
            }

            using (Surface surface = new PdfSurface("arrow.pdf", 500, 500))
                draw(surface);

            using (Surface surface = new SvgSurface("arrow.svg", 500, 500))
                draw(surface);
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Save the current Render data as a PNG image.
        /// </summary>
        public void SaveImage()
        {
            if (Running)
            {
                Logger.Say("Please stop the simulation first.");
                return;
            }
            if (ImageData == null)
            {
                Logger.Say("Image output is empty.");
                return;
            }

            var rand  = new Random();
            var title = "render-" + Instance.GetTitle().Replace(" ", "_") + "-" +
                        Math.Truncate(rand.NextDouble() * 100000000);

            var surface = new ImageSurface(ImageData, Format.ARGB32,
                                           RenderWidth, RenderHeight, 4 * RenderWidth);

            surface.WriteToPng(Path.Combine(_dataLogger.FileDir, title + ".png"));
            surface.Dispose();
        }
Ejemplo n.º 27
0
        public static void GenerateGraph(List <ResultDbEntry> resultList, string filename)
        {
            // FIXME Exception if more than 50 results...

            ImageSurface surface = new ImageSurface(Format.Rgb24, 103, 52);
            Context      context = new Context(surface);

            // Fill with grad
            LinearGradient grad = new LinearGradient(0.0, 0.0, 0.0, 52.0);

            grad.AddColorStopRgb(0.0, new Color(1.0, 1.0, 1.0));
            grad.AddColorStopRgb(1.0, new Color(0.8, 0.8, 0.9));
            context.Pattern = grad;
            context.Paint();

            // Frame
            context.SetSourceRGB(0, 0, 0);
            context.LineWidth = 1.0;
            context.Rectangle(0.5, 0.5, 102.0, 51.0);
            context.Stroke();

            long denominator = (long)(FindBiggestResult(resultList) * 1.2);

            context.LineWidth = 1.5;

            // FIXME Reverse to copy
            resultList.Reverse();

            double x             = 100.5 - ((resultList.Count - 1) * 2.0);
            bool   hasPrevResult = false;
            long   prevResult    = 0;

            foreach (ResultDbEntry entry in resultList)
            {
                if (entry.Failure)
                {
                    x += 2.0;
                    continue;
                }

                double sz = ((double)entry.Time / denominator) * 50.0;

                if (hasPrevResult && UtilFu.GetValueDifference(prevResult, entry.Time) > 0.1)
                {
                    context.SetSourceRGB(1.0, 0.0, 0.0);
                }
                else if (hasPrevResult && UtilFu.GetValueDifference(prevResult, entry.Time) < -0.1)
                {
                    context.SetSourceRGB(0.0, 1.0, 0.0);
                }
                else
                {
                    context.SetSourceRGB(0.4, 0.4, 0.4);
                }

                context.MoveTo(x, 51);
                context.LineTo(x, 51 - sz);
                context.Stroke();

                x += 2.0;

                hasPrevResult = true;
                prevResult    = entry.Time;
            }

            surface.WriteToPng(filename);

            resultList.Reverse();
            ((IDisposable)context).Dispose();
            ((IDisposable)surface).Dispose();
        }
Ejemplo n.º 28
0
 protected virtual void Save()
 {
     s.WriteToPng(saveName + DateTime.Now.ToString("yyyy-MM-dd_HHmmss") + ".png");
 }
Ejemplo n.º 29
0
        //---------------------------------------------------------------------
        private static void Primitives()
        {
            Action <Surface> draw = surface =>
            {
                using (var c = new Context(surface))
                {
                    c.Scale(4, 4);

                    // Stroke:
                    c.LineWidth = 0.1;
                    c.Color     = new Color(0, 0, 0);
                    c.Rectangle(10, 10, 10, 10);
                    c.Stroke();

                    c.Save();
                    {
                        c.Color = new Color(0, 0, 0);
                        c.Translate(20, 5);
                        c.MoveTo(0, 0);
                        c.LineTo(10, 5);
                        c.Stroke();
                    }
                    c.Restore();

                    // Fill:
                    c.Color = new Color(0, 0, 0);
                    c.SetSourceRGB(0, 0, 0);
                    c.Rectangle(10, 30, 10, 10);
                    c.Fill();

                    // Text:
                    c.Color = new Color(0, 0, 0);
                    c.SelectFontFace("Georgia", FontSlant.Normal, FontWeight.Bold);
                    c.SetFontSize(10);
                    TextExtents te = c.TextExtents("a");
                    c.MoveTo(
                        0.5 - te.Width / 2 - te.XBearing + 10,
                        0.5 - te.Height / 2 - te.YBearing + 50);
                    c.ShowText("a");

                    c.Color = new Color(0, 0, 0);
                    c.SelectFontFace("Arial", FontSlant.Normal, FontWeight.Bold);
                    c.SetFontSize(10);
                    te = c.TextExtents("a");
                    c.MoveTo(
                        0.5 - te.Width / 2 - te.XBearing + 10,
                        0.5 - te.Height / 2 - te.YBearing + 60);
                    c.ShowText("a");
                }
            };

            using (Surface surface = new ImageSurface(Format.Argb32, 200, 3200))
            {
                draw(surface);
                surface.WriteToPng("primitives.png");
            }

            using (Surface surface = new PdfSurface("primitives.pdf", 200, 3200))
                draw(surface);

            using (Surface surface = new SvgSurface("primitives.svg", 200, 3200))
                draw(surface);
        }
Ejemplo n.º 30
0
        //---------------------------------------------------------------------
        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");
            }
        }