Пример #1
0
        /// <summary>
        /// Exports the specified <see cref="PlotModel" /> to a <see cref="Bitmap" />.
        /// </summary>
        /// <param name="model">The model to export.</param>
        /// <returns>A bitmap.</returns>
        public Bitmap ExportToBitmap(IPlotModel model)
        {
            var bm = new Bitmap(this.Width, this.Height, PixelFormat.Format24bppRgb);

            bm.SetResolution(this.Resolution, this.Resolution);
            using (var g = Graphics.FromImage(bm))
            {
                if (this.Background.IsVisible())
                {
                    using (var brush = this.Background.ToBrush())
                    {
                        g.FillRectangle(brush, 0, 0, this.Width, this.Height);
                    }
                }

                using (var rc = new GroundTruthGraphicsRenderContext(g)
                {
                    RendersToScreen = false
                })
                {
                    model.Update(true);
                    model.Render(rc, this.Width, this.Height);
                    this.GroundTruth     = rc.GroundTruth;
                    this.GroundTruthText = rc.GroundTruthText;
                }
                return(bm);
            }
        }
Пример #2
0
 /// <summary>
 /// Exports the specified <see cref="PlotModel" /> to a file.
 /// </summary>
 /// <param name="exporter">The exporter.</param>
 /// <param name="model">The model to export.</param>
 /// <param name="path">The path to the file.</param>
 public static void ExportToFile(this IExporter exporter, IPlotModel model, string path)
 {
     using (var stream = File.OpenWrite(path))
     {
         exporter.Export(model, stream);
     }
 }
Пример #3
0
        /// <summary>
        /// Export the specified plot model to an xaml string.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="background">The background.</param>
        /// <returns>A xaml string.</returns>
        public static string ExportToString(IPlotModel model, double width, double height, OxyColor background)
        {
            var g = new Grid();
            if (background.IsVisible())
            {
                g.Background = background.ToBrush();
            }

            var c = new Canvas();
            g.Children.Add(c);

            var size = new Size(width, height);
            g.Measure(size);
            g.Arrange(new Rect(0, 0, width, height));
            g.UpdateLayout();

            var rc = new ShapesRenderContext(c) { UseStreamGeometry = false };
            model.Update(true);
            model.Render(rc, width, height);

            var sb = new StringBuilder();
            using (var sw = new StringWriter(sb))
            {
                var xw = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true });
                XamlWriter.Save(c, xw);
            }

            return sb.ToString();
        }
Пример #4
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 stream.</param>
 public void Export(IPlotModel model, Stream stream)
 {
     var rc = new PdfRenderContext(this.Width, this.Height, this.Background);
     model.Update(true);
     model.Render(rc, this.Width, this.Height);
     rc.Save(stream);
 }
Пример #5
0
 /// <summary>
 /// Exports the specified model to a file.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="path">The path.</param>
 /// <param name="width">The width (points).</param>
 /// <param name="height">The height (points).</param>
 public static void Export(IPlotModel model, string path, double width, double height)
 {
     using (var s = File.OpenWrite(path))
     {
         Export(model, s, width, height);
     }
 }
Пример #6
0
        /// <summary>
        /// Exports the specified plot model to an xps file.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="fileName">The file name.</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, double width, double height, OxyColor background)
        {
            using (var xpsPackage = Package.Open(fileName, FileMode.Create, FileAccess.ReadWrite))
            {
                using (var doc = new XpsDocument(xpsPackage))
                {
                    var canvas = new Canvas { Width = width, Height = height, Background = background.ToBrush() };
                    canvas.Measure(new Size(width, height));
                    canvas.Arrange(new Rect(0, 0, width, height));

                    var rc = new ShapesRenderContext(canvas);
#if !NET35
                    rc.TextFormattingMode = TextFormattingMode.Ideal;
#endif

                    model.Update(true);
                    model.Render(rc, width, height);

                    canvas.UpdateLayout();

                    var xpsdw = XpsDocument.CreateXpsDocumentWriter(doc);
                    xpsdw.Write(canvas);
                }
            }
        }
Пример #7
0
        /// <summary>
        /// Exports the specified plot model to a bitmap.
        /// </summary>
        /// <param name="model">The model to export.</param>
        /// <returns>A bitmap.</returns>
        public BitmapSource ExportToBitmap(IPlotModel model)
        {
            var scale  = 96d / this.Resolution;
            var canvas = new Canvas {
                Width = this.Width * scale, Height = this.Height * scale, Background = this.Background.ToBrush()
            };

            canvas.Measure(new Size(canvas.Width, canvas.Height));
            canvas.Arrange(new Rect(0, 0, canvas.Width, canvas.Height));

            var rc = new ShapesRenderContext(canvas)
            {
                RendersToScreen = false, TextFormattingMode = TextFormattingMode.Ideal
            };

            model.Update(true);
            model.Render(rc, canvas.Width, canvas.Height);

            canvas.UpdateLayout();

            var bmp = new RenderTargetBitmap(this.Width, this.Height, this.Resolution, this.Resolution, PixelFormats.Pbgra32);

            bmp.Render(canvas);
            return(bmp);

            // alternative implementation:
            // http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx
            // var dv = new DrawingVisual();
            // using (var ctx = dv.RenderOpen())
            // {
            //    var vb = new VisualBrush(canvas);
            //    ctx.DrawRectangle(vb, null, new Rect(new Point(), new Size(width, height)));
            // }
            // bmp.Render(dv);
        }
Пример #8
0
 /// <summary>
 /// Exports the specified plot model to a file.
 /// </summary>
 /// <param name="model">The model to export.</param>
 /// <param name="fileName">The file name.</param>
 /// <param name="width">The width of the output bitmap.</param>
 /// <param name="height">The height of the output bitmap.</param>
 /// <param name="background">The background color. The default value is <c>null</c>.</param>
 /// <param name="resolution">The resolution (resolution). The default value is 96.</param>
 public static void Export(IPlotModel model, string fileName, int width, int height, OxyColor background, int resolution = 96)
 {
     using (var s = File.Create(fileName))
     {
         Export(model, s, width, height, background, resolution);
     }
 }
Пример #9
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 = this.ExportToBitmap(model))
     {
         bm.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
     }
 }
Пример #10
0
        /// <summary>
        /// Exports the specified <see cref="PlotModel" /> to a <see cref="Bitmap" />.
        /// </summary>
        /// <param name="model">The model to export.</param>
        /// <returns>A <see cref="Bitmap"/>.</returns>
        public Bitmap ExportToBitmap(IPlotModel model)
        {
            var bm = new Bitmap(this.Width, this.Height);

            using (var g = Graphics.FromImage(bm))
            {
                if (!this.Background.IsInvisible())
                {
                    using (var brush = this.Background.ToBrush())
                    {
                        g.FillRectangle(brush, 0, 0, this.Width, this.Height);
                    }
                }

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

                // this throws an exception
                // bm.SetResolution(resolution, resolution);
                // https://github.com/dotnet/corefx/blob/master/src/System.Drawing.Common/src/System/Drawing/Bitmap.cs#L301

                return(bm);
            }
        }
Пример #11
0
        /// <summary>
        /// Exports the specified plot model to a xml writer.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="writer">The xml writer.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        private static void Export(IPlotModel model, XmlWriter writer, double width, double height)
        {
            var c = new Canvas();

            if (model.Background.IsVisible())
            {
                c.Background = model.Background.ToBrush();
            }

            c.Measure(new Size(width, height));
            c.Arrange(new Rect(0, 0, width, height));

            var rc = new CanvasRenderContext(c)
            {
                UseStreamGeometry = false
            };

            rc.TextFormattingMode = TextFormattingMode.Ideal;

            model.Update(true);
            model.Render(rc, new OxyRect(0, 0, width, height));

            c.UpdateLayout();

            XamlWriter.Save(c, writer);
        }
Пример #12
0
        /// <summary>
        /// Updates the specified plot model and renders to null.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="width">The width of the output surface.</param>
        /// <param name="height">The height the output surface.</param>
        /// <remarks>This method is useful to simulate rendering in the unit tests.</remarks>
        public static void UpdateAndRenderToNull(this IPlotModel model, double width, double height)
        {
            var rc = new NullRenderContext();

            model.Update(true);
            model.Render(rc, width, height);
        }
Пример #13
0
        /// <summary>
        /// Exports the specified <see cref="PlotModel" /> to a <see cref="Bitmap" />.
        /// </summary>
        /// <param name="model">The model to export.</param>
        /// <returns>A bitmap.</returns>
        public Bitmap ExportToBitmap(IPlotModel model)
        {
            var bm = new Bitmap(this.Width, this.Height);

            using (var g = Graphics.FromImage(bm))
            {
                if (model.Background.IsVisible())
                {
                    using (var brush = model.Background.ToBrush())
                    {
                        g.FillRectangle(brush, 0, 0, this.Width, this.Height);
                    }
                }

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

                bm.SetResolution((float)this.Resolution, (float)this.Resolution);
                return(bm);
            }
        }
Пример #14
0
 /// <summary>
 /// Exports the specified <see cref="PlotModel" /> to a file.
 /// </summary>
 /// <param name="exporter">The exporter.</param>
 /// <param name="model">The model to export.</param>
 /// <param name="path">The path to the file.</param>
 public static void ExportToFile(this IExporter exporter, IPlotModel model, string path)
 {
     using (var stream = File.OpenWrite(path))
     {
         exporter.Export(model, stream);
     }
 }
Пример #15
0
        /// <summary>
        /// Exports the specified plot model to a bitmap.
        /// </summary>
        /// <param name="model">The model to export.</param>
        /// <returns>A bitmap.</returns>
        public IBitmap ExportToBitmap(IPlotModel model)
        {
            var scale  = 96d / Resolution;
            var canvas = new Canvas {
                Width = Width * scale, Height = Height * scale, Background = Background.ToBrush()
            };

            canvas.Measure(new Size(canvas.Width, canvas.Height));
            canvas.Arrange(new Rect(0, 0, canvas.Width, canvas.Height));

            var rc = new CanvasRenderContext(canvas)
            {
                RendersToScreen = false
            };

            model.Update(true);
            model.Render(rc, canvas.Width, canvas.Height);

            canvas.Measure(new Size(canvas.Width, canvas.Height));
            canvas.Arrange(new Rect(0, 0, canvas.Width, canvas.Height));

            var bmp = new RenderTargetBitmap(new PixelSize(Width, Height));

            bmp.Render(canvas);
            return(bmp);
        }
Пример #16
0
 /// <summary>
 /// Exports the specified model to a file.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="path">The path.</param>
 /// <param name="width">The width (points).</param>
 /// <param name="height">The height (points).</param>
 public static void Export(IPlotModel model, string path, double width, double height)
 {
     using (var s = File.Create(path))
     {
         Export(model, s, width, height);
     }
 }
Пример #17
0
        /// <summary>
        /// Exports the specified plot model to an xps file.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="fileName">The file name.</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, double width, double height, OxyColor background)
        {
            using (var xpsPackage = Package.Open(fileName, FileMode.Create, FileAccess.ReadWrite))
            {
                using (var doc = new XpsDocument(xpsPackage))
                {
                    var canvas = new Canvas {
                        Width = width, Height = height, Background = background.ToBrush()
                    };
                    canvas.Measure(new Size(width, height));
                    canvas.Arrange(new Rect(0, 0, width, height));

                    var rc = new ShapesRenderContext(canvas)
                    {
                        TextFormattingMode = TextFormattingMode.Ideal
                    };
                    model.Update(true);
                    model.Render(rc, width, height);

                    canvas.UpdateLayout();

                    var xpsdw = XpsDocument.CreateXpsDocumentWriter(doc);
                    xpsdw.Write(canvas);
                }
            }
        }
Пример #18
0
        /// <inheritdoc/>
        public void Export(IPlotModel model, Stream stream)
        {
            using var bitmap = new SKBitmap(this.Width, this.Height);

            using (var canvas = new SKCanvas(bitmap))
                using (var context = new SkiaRenderContext {
                    RenderTarget = RenderTarget.PixelGraphic, SkCanvas = canvas
                })
                {
                    canvas.Clear(SKColors.White);
                    var dpiScale = this.Dpi / 96;
                    context.DpiScale = dpiScale;
                    model.Update(true);
                    var backgroundColor = model.Background;

                    // jpg doesn't support transparency
                    if (!backgroundColor.IsVisible())
                    {
                        backgroundColor = OxyColors.White;
                    }

                    canvas.Clear(backgroundColor.ToSKColor());
                    model.Render(context, new OxyRect(0, 0, this.Width / dpiScale, this.Height / dpiScale));
                }

            using var skStream = new SKManagedWStream(stream);
            SKPixmap.Encode(skStream, bitmap, SKEncodedImageFormat.Jpeg, this.Quality);
        }
Пример #19
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 stream.</param>
        public void Export(IPlotModel model, Stream stream)
        {
            using (var xpsPackage = Package.Open(stream))
            {
                using (var doc = new XpsDocument(xpsPackage))
                {
                    var canvas = new Canvas {
                        Width = this.Width, Height = this.Height, Background = this.Background.ToBrush()
                    };
                    canvas.Measure(new Size(this.Width, this.Height));
                    canvas.Arrange(new Rect(0, 0, this.Width, this.Height));

                    var rc = new ShapesRenderContext(canvas)
                    {
                        TextFormattingMode = this.TextFormattingMode
                    };
                    model.Update(true);
                    model.Render(rc, this.Width, this.Height);

                    canvas.UpdateLayout();

                    var xpsdw = XpsDocument.CreateXpsDocumentWriter(doc);
                    xpsdw.Write(canvas);
                }
            }
        }
Пример #20
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 Bitmap(this.Width, this.Height))
            {
                using (var g = Graphics.FromImage(bm))
                {
                    if (this.Background.IsVisible())
                    {
                        using (var brush = this.Background.ToBrush())
                        {
                            g.FillRectangle(brush, 0, 0, this.Width, this.Height);
                        }
                    }

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

                    bm.Save(stream, ImageFormat.Png);
                }
            }
        }
Пример #21
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);
                }
            }
        }
Пример #22
0
        /// <summary>
        /// Exports the specified <see cref="PlotModel" /> to the specified <see cref="Stream" />.
        /// </summary>
        /// <param name="plotModel">The model.</param>
        /// <param name="stream">The output stream.</param>
        public void Export(IPlotModel plotModel, Stream stream)
        {
            var bitmapSource = ExportToBitmap(plotModel);
            var encoder      = new TiffBitmapEncoder();

            encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
            encoder.Save(stream);
        }
Пример #23
0
 /// <summary>
 /// Exports the specified model.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="fileName">The file name.</param>
 /// <param name="width">The width.</param>
 /// <param name="height">The height.</param>
 /// <param name="background">The background.</param>
 public static void Export(IPlotModel model, string fileName, int width, int height, Brush background = null)
 {
     var exporter = new PngExporter { Width = width, Height = height, Background = background.ToOxyColor() };
     using (var stream = File.Create(fileName))
     {
         exporter.Export(model, stream);
     }
 }
Пример #24
0
        /// <summary>
        /// Exports the specified model to a stream.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="stream">The output stream.</param>
        /// <param name="width">The width (points).</param>
        /// <param name="height">The height (points).</param>
        /// <param name="quality">The export quality (0-100).</param>
        /// <param name="dpi">The DPI (dots per inch).</param>
        public static void Export(IPlotModel model, Stream stream, int width, int height, int quality, float dpi = 96)
        {
            var exporter = new JpegExporter {
                Width = width, Height = height, Quality = quality, Dpi = dpi
            };

            exporter.Export(model, stream);
        }
Пример #25
0
    public static FileInfo ToPNG(this IPlotModel Model, FileInfo File, int Width = 800, int Height = 600, double Resolution = 96)
    {
        var exporter = new PngExporter(Width, Height, Resolution);

        using var stream = File.Create();
        exporter.Export(Model, stream);
        return(File);
    }
Пример #26
0
        /// <summary>
        /// Exports the specified model to a stream.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="stream">The output stream.</param>
        /// <param name="width">The width (points).</param>
        /// <param name="height">The height (points).</param>
        public static void Export(IPlotModel model, Stream stream, double width, double height)
        {
            var exporter = new PdfExporter {
                Width = width, Height = height
            };

            exporter.Export(model, stream);
        }
Пример #27
0
        /// <summary>
        /// Exports the specified plot model to a stream.
        /// </summary>
        /// <param name="model">The model to export.</param>
        /// <param name="stream">The stream.</param>
        /// <param name="width">The width of the output bitmap.</param>
        /// <param name="height">The height of the output bitmap.</param>
        /// <param name="background">The background color. The default value is <c>null</c>.</param>
        /// <param name="resolution">The resolution (resolution). The default value is 96.</param>
        public static void Export(IPlotModel model, Stream stream, int width, int height, OxyColor background, int resolution = 96)
        {
            var exporter = new PngExporter {
                Width = width, Height = height, Background = background, Resolution = resolution
            };

            exporter.Export(model, stream);
        }
Пример #28
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)
        {
            var bmp     = this.ExportToBitmap(model);
            var encoder = new PngBitmapEncoder();

            encoder.Frames.Add(BitmapFrame.Create(bmp));
            encoder.Save(stream);
        }
Пример #29
0
 /// <summary>
 /// Exports the specified model.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="fileName">The file name.</param>
 /// <param name="width">The width.</param>
 /// <param name="height">The height.</param>
 /// <param name="background">The background.</param>
 public static void Export(IPlotModel model, string fileName, int width, int height, Brush background = null)
 {
     var exporter = new PngExporter { Width = width, Height = height, Background = background.ToOxyColor() };
     using (var stream = File.Create(fileName))
     {
         exporter.Export(model, stream);
     }
 }
Пример #30
0
        /// <summary>
        /// Prints the specified plot model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="width">The width (using the actual media width if set to NaN).</param>
        /// <param name="height">The height (using the actual media height if set to NaN).</param>
        public static void Print(IPlotModel model, double width, double height)
        {
            var exporter = new XpsExporter {
                Width = width, Height = height, Background = model.Background
            };

            exporter.Print(model);
        }
Пример #31
0
 /// <summary>
 /// Exports the specified plot model to an xps file.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="fileName">The file name.</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, double width, double height, OxyColor background)
 {
     using (var stream = File.Open(fileName, FileMode.Create, FileAccess.ReadWrite))
     {
         var exporter = new XpsExporter { Width = width, Height = height, Background = background };
         exporter.Export(model, stream);
     }
 }
Пример #32
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 stream.</param>
        public void Export(IPlotModel model, Stream stream)
        {
            var rc = new PdfRenderContext(this.Width, this.Height, model.Background);

            model.Update(true);
            model.Render(rc, new OxyRect(0, 0, this.Width, this.Height));
            rc.Save(stream);
        }
Пример #33
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 stream.</param>
 public void Export(IPlotModel model, Stream stream)
 {
     using (var rc = new PdfRenderContext(this.Width, this.Height, this.Background))
     {
         model.Update(true);
         model.Render(rc, this.Width, this.Height);
         rc.Save(stream);
     }
 }
Пример #34
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 rc = new PngRenderingContext(this.Width, this.Height, model.Background, this.Resolution))
     {
         model.Update(true);
         model.Render(rc, new OxyRect(0, 0, this.Width, this.Height));
         rc.Save(stream);
     }
 }
Пример #35
0
 /// <summary>
 /// Export a plot model to an image.
 /// </summary>
 /// <param name="plot">Plot model to be exported.</param>
 /// <param name="width">Desired width of the image (in px).</param>
 /// <param name="height">Desired height of the image (in px).</param>
 public Image Export(IPlotModel plot, double width, double height)
 {
     using (Stream stream = new MemoryStream())
     {
         PngExporter.Export(plot, stream, (int)width, (int)height);
         stream.Seek(0, SeekOrigin.Begin);
         return(Image.FromStream(stream));
     }
 }
Пример #36
0
        /// <summary>
        /// Update the graph data sources; this causes the axes minima and maxima to be calculated
        /// </summary>
        public void UpdateView()
        {
            IPlotModel theModel = this.plot1.Model as IPlotModel;

            if (theModel != null)
            {
                theModel.Update(true);
            }
        }
Пример #37
0
        /// <summary>
        /// Exports the specified <see cref="PlotModel" /> to the specified file.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="fileName">The file name.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="resolution">The resolution in dpi (defaults to 96dpi).</param>
        public static void Export(IPlotModel model, string fileName, int width, int height, double resolution = 96)
        {
            var exporter = new PngExporter(width, height, resolution);

            using (var stream = File.Create(fileName))
            {
                exporter.Export(model, stream);
            }
        }
Пример #38
0
        /// <summary>
        /// Export the specified plot model to an xaml string.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="background">The background.</param>
        /// <returns>A xaml string.</returns>
        public static string ExportToString(IPlotModel model, double width, double height, OxyColor background)
        {
            var sb = new StringBuilder();
            using (var sw = new StringWriter(sb))
            {
                var xw = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true });
                Export(model, xw, width, height, background);
            }

            return sb.ToString();
        }
Пример #39
0
        /// <summary>
        /// Exports the specified model to a stream.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="stream">The output stream.</param>
        /// <param name="width">The width (points).</param>
        /// <param name="height">The height (points).</param>
        /// <param name="isDocument">if set to <c>true</c>, the xml headers will be included (?xml and !DOCTYPE).</param>
        /// <param name="textMeasurer">The text measurer.</param>
        public static void Export(IPlotModel model, Stream stream, double width, double height, bool isDocument, IRenderContext textMeasurer = null)
        {
            if (textMeasurer == null)
            {
                textMeasurer = new PdfRenderContext(width, height, model.Background);
            }

            using (var rc = new SvgRenderContext(stream, width, height, true, textMeasurer, model.Background))
            {
                model.Update(true);
                model.Render(rc, width, height);
                rc.Complete();
                rc.Flush();
            }
        }
Пример #40
0
        /// <summary>
        /// Exports the specified plot model to a stream.
        /// </summary>
        /// <param name="model">The plot model.</param>
        /// <param name="stream">The stream to write to.</param>
        /// <param name="width">The width of the export image.</param>
        /// <param name="height">The height of the exported image.</param>
        /// <param name="background">The background.</param>
        public static void Export(IPlotModel model, Stream stream, double width, double height, OxyColor background)
        {
            var canvas = new Canvas { Width = width, Height = height };
            if (background.IsVisible())
            {
                canvas.Background = background.ToBrush();
            }

            canvas.Measure(new Size(width, height));
            canvas.Arrange(new Rect(0, 0, width, height));

            var rc = new SilverlightRenderContext(canvas);
            model.Update(true);
            model.Render(rc, width, height);

            canvas.UpdateLayout();
            var image = canvas.ToImage();
            image.WriteToStream(stream);
        }
Пример #41
0
        /// <summary>
        /// Exports the specified plot model to a xml writer.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="writer">The xml writer.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="background">The background.</param>
        private static void Export(IPlotModel model, XmlWriter writer, double width, double height, OxyColor background)
        {
            var c = new Canvas();
            if (background.IsVisible())
            {
                c.Background = background.ToBrush();
            }

            c.Measure(new Size(width, height));
            c.Arrange(new Rect(0, 0, width, height));

            var rc = new CanvasRenderContext(c) { UseStreamGeometry = false };

            rc.TextFormattingMode = TextFormattingMode.Ideal;

            model.Update(true);
            model.Render(rc, width, height);

            c.UpdateLayout();

            XamlWriter.Save(c, writer);
        }
Пример #42
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);
                }
            }
        }
Пример #43
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 = 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);
                }
            }
        }
Пример #44
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 Bitmap(this.Width, this.Height))
            {
                using (var g = Graphics.FromImage(bm))
                {
                    if (this.Background.IsVisible())
                    {
                        using (var brush = this.Background.ToBrush())
                        {
                            g.FillRectangle(brush, 0, 0, this.Width, this.Height);
                        }
                    }

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

                    bm.Save(stream, ImageFormat.Png);
                }
            }
        }
Пример #45
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 stream.</param>
 /// <param name="width">The width.</param>
 /// <param name="height">The height.</param>
 public static void Export(IPlotModel model, Stream stream, double width, double height)
 {
     var exporter = new XpsExporter { Width = width, Height = height };
     exporter.Export(model, stream);
 }
Пример #46
0
 /// <summary>
 /// Prints the specified plot model.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="width">The width (using the actual media width if set to NaN).</param>
 /// <param name="height">The height (using the actual media height if set to NaN).</param>
 public static void Print(IPlotModel model, double width, double height)
 {
     var exporter = new XpsExporter { Width = width, Height = height, Background = model.Background };
     exporter.Print(model);
 }
Пример #47
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 = this.ExportToBitmap(model))
     {
         bm.Save(stream, ImageFormat.Png);
     }
 }
Пример #48
0
        /// <summary>
        /// Exports the specified <see cref="PlotModel" /> to a <see cref="Bitmap" />.
        /// </summary>
        /// <param name="model">The model to export.</param>
        /// <returns>A bitmap.</returns>
        public Bitmap ExportToBitmap(IPlotModel model)
        {
            var bm = new Bitmap(this.Width, this.Height);
            using (var g = Graphics.FromImage(bm))
            {
                if (this.Background.IsVisible())
                {
                    using (var brush = this.Background.ToBrush())
                    {
                        g.FillRectangle(brush, 0, 0, this.Width, this.Height);
                    }
                }

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

                bm.SetResolution(this.Resolution, this.Resolution);
                return bm;
            }
        }
Пример #49
0
 /// <summary>
 /// Exports the specified model to a stream.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="stream">The output stream.</param>
 /// <param name="width">The width (points).</param>
 /// <param name="height">The height (points).</param>
 public static void Export(IPlotModel model, Stream stream, double width, double height)
 {
     var exporter = new PdfExporter { Width = width, Height = height, Background = model.Background };
     exporter.Export(model, stream);
 }
Пример #50
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 stream.</param>
        public void Export(IPlotModel model, Stream stream)
        {
            using (var xpsPackage = Package.Open(stream))
            {
                using (var doc = new XpsDocument(xpsPackage))
                {
                    var canvas = new Canvas { Width = this.Width, Height = this.Height, Background = this.Background.ToBrush() };
                    canvas.Measure(new Size(this.Width, this.Height));
                    canvas.Arrange(new Rect(0, 0, this.Width, this.Height));

                    var rc = new ShapesRenderContext(canvas);
#if !NET35
                    rc.TextFormattingMode = this.TextFormattingMode;
#endif
                    model.Update(true);
                    model.Render(rc, this.Width, this.Height);

                    canvas.UpdateLayout();

                    var xpsdw = XpsDocument.CreateXpsDocumentWriter(doc);
                    xpsdw.Write(canvas);
                }
            }
        }
Пример #51
0
        private async void SaveDocument(IPlotModel model, String newDocument)
        {
            if (model == null)
            {
                var msg = new MessageDialog("График не создан, рассчитайте распределение", "Ошибка сохранения");
                await msg.ShowAsync();
                return;
            }

            var savePicker = new Windows.Storage.Pickers.FileSavePicker
            {
                SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary
            };
            savePicker.FileTypeChoices.Add("PDF Document", new List<string>() { ".pdf" });
            savePicker.SuggestedFileName = newDocument;
            StorageFile file = await savePicker.PickSaveFileAsync();
            if (file != null)
            {
                CachedFileManager.DeferUpdates(file);
                var stream = await file.OpenAsync(FileAccessMode.ReadWrite);

                using (var outputStream = stream.GetOutputStreamAt(0))
                {
                    using (var dataWriter = new Windows.Storage.Streams.DataWriter(outputStream))
                    {
                        using (var memoryStream = new MemoryStream())
                        {
                            var pdf = new PdfExporter();
                            PdfExporter.Export(model, memoryStream, 1000, 400);
                            var bt = memoryStream.ToArray();
                            dataWriter.WriteBytes(bt);
                            await dataWriter.StoreAsync();
                            await outputStream.FlushAsync();
                        }
                    }
                }
                var status = await CachedFileManager.CompleteUpdatesAsync(file);
                if (status == FileUpdateStatus.Complete)
                {
                    var msg = new MessageDialog("По пути " + file.Path, "Файл сохранен");
                    await msg.ShowAsync();
                }
                else
                {
                    var msg = new MessageDialog("Произошла ошибка сохранения");
                    await msg.ShowAsync();
                }
            }
        }
Пример #52
0
        /// <summary>
        /// Exports to string.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="width">The width (points).</param>
        /// <param name="height">The height (points).</param>
        /// <param name="isDocument">if set to <c>true</c>, the xml headers will be included (?xml and !DOCTYPE).</param>
        /// <param name="textMeasurer">The text measurer.</param>
        /// <returns>The plot as an <c>SVG</c> string.</returns>
        public static string ExportToString(IPlotModel model, double width, double height, bool isDocument, IRenderContext textMeasurer = null)
        {
            string svg;
            using (var ms = new MemoryStream())
            {
                Export(model, ms, width, height, isDocument, textMeasurer);
                ms.Flush();
                ms.Position = 0;
                var sr = new StreamReader(ms);
                svg = sr.ReadToEnd();
            }

            return svg;
        }
Пример #53
0
        /// <summary>
        /// Prints the specified plot model.
        /// </summary>
        /// <param name="model">The model.</param>
        public void Print(IPlotModel model)
        {
            PrintDocumentImageableArea area = null;
            var xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(ref area);
            if (xpsDocumentWriter != null)
            {
                var width = this.Width;
                var height = this.Height;
                if (double.IsNaN(width))
                {
                    width = area.MediaSizeWidth;
                }

                if (double.IsNaN(height))
                {
                    height = area.MediaSizeHeight;
                }

                var canvas = new Canvas { Width = width, Height = height, Background = this.Background.ToBrush() };
                canvas.Measure(new Size(width, height));
                canvas.Arrange(new Rect(0, 0, width, height));

                var rc = new ShapesRenderContext(canvas);
#if !NET35
                rc.TextFormattingMode = this.TextFormattingMode;
#endif
                model.Update(true);
                model.Render(rc, width, height);

                canvas.UpdateLayout();

                xpsDocumentWriter.Write(canvas);
            }
        }
Пример #54
0
 /// <summary>
 /// Exports the specified <see cref="PlotModel" /> to a <see cref="Stream" />.
 /// </summary>
 /// <param name="model">The model to export.</param>
 /// <param name="stream">The target stream.</param>
 public void Export(IPlotModel model, Stream stream)
 {
     Export(model, stream, this.Width, this.Height, this.IsDocument, this.TextMeasurer);
 }
Пример #55
0
 /// <summary>
 /// Exports the specified <see cref="PlotModel" /> to a string.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>the SVG content as a string.</returns>
 public string ExportToString(IPlotModel model)
 {
     return ExportToString(model, this.Width, this.Height, this.IsDocument, this.TextMeasurer);
 }
Пример #56
0
        /// <summary>
        /// Prints the specified plot model.
        /// </summary>
        /// <param name="model">The model.</param>
        public void Print(IPlotModel model)
        {
            PrintDocumentImageableArea area = null;
            var xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(ref area);
            if (xpsDocumentWriter != null)
            {
                if (double.IsNaN(this.Width))
                {
                    this.Width = area.ExtentWidth;
                }

                if (double.IsNaN(this.Height))
                {
                    this.Height = area.ExtentHeight;
                }

                this.Write(model, xpsDocumentWriter);
            }
        }
Пример #57
0
        /// <summary>
        /// Exports the specified plot model to a bitmap.
        /// </summary>
        /// <param name="model">The model to export.</param>
        /// <returns>A bitmap.</returns>
        public BitmapSource ExportToBitmap(IPlotModel model)
        {
            var scale = 96d / this.Resolution;
            var canvas = new Canvas { Width = this.Width * scale, Height = this.Height * scale, Background = this.Background.ToBrush() };
            canvas.Measure(new Size(canvas.Width, canvas.Height));
            canvas.Arrange(new Rect(0, 0, canvas.Width, canvas.Height));

            var rc = new CanvasRenderContext(canvas) { RendersToScreen = false };

            rc.TextFormattingMode = TextFormattingMode.Ideal;

            model.Update(true);
            model.Render(rc, canvas.Width, canvas.Height);

            canvas.UpdateLayout();

            var bmp = new RenderTargetBitmap(this.Width, this.Height, this.Resolution, this.Resolution, PixelFormats.Pbgra32);
            bmp.Render(canvas);
            return bmp;

            // alternative implementation:
            // http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx
            // var dv = new DrawingVisual();
            // using (var ctx = dv.RenderOpen())
            // {
            //    var vb = new VisualBrush(canvas);
            //    ctx.DrawRectangle(vb, null, new Rect(new Point(), new Size(width, height)));
            // }
            // bmp.Render(dv);
        }
Пример #58
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 stream.</param>
 public void Export(IPlotModel model, Stream stream)
 {
     using (var xpsPackage = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite))
     {
         using (var doc = new XpsDocument(xpsPackage))
         {
             var xpsdw = XpsDocument.CreateXpsDocumentWriter(doc);
             this.Write(model, xpsdw);
         }
     }
 }
Пример #59
0
        /// <summary>
        /// Write the specified <see cref="IPlotModel" /> to the specified <see cref="XpsDocumentWriter" />.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="writer">The document writer.</param>
        private void Write(IPlotModel model, XpsDocumentWriter writer)
        {
            var canvas = new Canvas { Width = this.Width, Height = this.Height, Background = this.Background.ToBrush() };
            canvas.Measure(new Size(this.Width, this.Height));
            canvas.Arrange(new Rect(0, 0, this.Width, this.Height));

            var rc = new CanvasRenderContext(canvas);
            rc.TextFormattingMode = this.TextFormattingMode;

            model.Update(true);
            model.Render(rc, this.Width, this.Height);

            canvas.UpdateLayout();

            writer.Write(canvas);
        }