/// <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); } } }
/// <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); }
/// <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); } } }
/// <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); } }
/// <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); } } }
/// <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); } }
/// <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(); }
/// <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); } } }
/// <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); }
/// <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); } } }
/// <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); }
/// <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); } }
/// <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); }
/// <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); }
/// <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); }
/// <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); }
/// <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); } }
/// <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); } }
/// <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); } }
/// <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) { using (SvgRenderContext1 svgRenderContext = new SvgRenderContext1(stream, width, height, true, textMeasurer, model.Background)) { model.Update(true); model.Render(svgRenderContext, width, height); svgRenderContext.Complete(); svgRenderContext.Flush(); } }
/// <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 ImageRenderContext(this.Width, this.Height, model.Background, this.Resolution)) { var dpiScale = this.Resolution / 96; model.Update(true); model.Render(rc, new OxyRect(0, 0, this.Width / dpiScale, this.Height / dpiScale)); rc.SaveAsPng(stream); } }
/// <summary> /// Update the graph data sources; this causes the axes minima and maxima to be calculated /// </summary> public void UpdateView() { foreach (PlotView plotView in plots) { IPlotModel theModel = plotView.Model as IPlotModel; if (theModel != null) { theModel.Update(true); } } }
/// <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 background = model.Background.IsInvisible() ? OxyColors.White : model.Background; using (var rc = new ImageRenderContext(this.Width, this.Height, background, this.Resolution)) { var dpiScale = this.Resolution / 96; model.Update(true); model.Render(rc, new OxyRect(0, 0, this.Width / dpiScale, this.Height / dpiScale)); rc.SaveAsJpeg(stream, this.Quality); } }
/// <inheritdoc/> public void Export(IPlotModel model, Stream stream) { using var document = SKDocument.CreatePdf(stream); using var pdfCanvas = document.BeginPage(this.Width, this.Height); using var context = new SkiaRenderContext { RenderTarget = RenderTarget.VectorGraphic, SkCanvas = pdfCanvas, UseTextShaping = this.UseTextShaping }; const float dpiScale = 72f / 96; context.DpiScale = dpiScale; model.Update(true); pdfCanvas.Clear(model.Background.ToSKColor()); model.Render(context, new OxyRect(0, 0, this.Width / dpiScale, this.Height / dpiScale)); }
/// <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(); } }
/// <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> /// <param name="useVerticalTextAlignmentWorkaround">Whether to use the workaround for vertical text alignment</param> public static void Export(IPlotModel model, Stream stream, double width, double height, bool isDocument, IRenderContext textMeasurer = null, bool useVerticalTextAlignmentWorkaround = false) { if (textMeasurer == null) { textMeasurer = new PdfRenderContext(width, height, model.Background); } using (var rc = new SvgRenderContext(stream, width, height, isDocument, textMeasurer, model.Background, useVerticalTextAlignmentWorkaround)) { model.Update(true); model.Render(rc, new OxyRect(0, 0, width, height)); rc.Complete(); rc.Flush(); } }
/// <inheritdoc/> public void Export(IPlotModel model, Stream stream) { using var skStream = new SKManagedWStream(stream); using var writer = new SKXmlStreamWriter(skStream); using var canvas = SKSvgCanvas.Create(new SKRect(0, 0, this.Width, this.Height), writer); if (!model.Background.IsInvisible()) { canvas.Clear(model.Background.ToSKColor()); } // SVG export does not work with UseTextShaping=true. However SVG does text shaping by itself anyway, so we can just disable it using var context = new SkiaRenderContext { RenderTarget = RenderTarget.VectorGraphic, SkCanvas = canvas, UseTextShaping = false }; model.Update(true); model.Render(context, new OxyRect(0, 0, this.Width, this.Height)); }
/// <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, UseTextShaping = this.UseTextShaping }) { var dpiScale = this.Dpi / 96; context.DpiScale = dpiScale; model.Update(true); canvas.Clear(model.Background.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.Png, 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); }
/// <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); }
/// <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); }
/// <inheritdoc/> public void Export(IPlotModel model, Stream stream) { using var skStream = new SKManagedWStream(stream); using var canvas = SKSvgCanvas.Create(new SKRect(0, 0, Width, Height), skStream); if (!model.Background.IsInvisible()) { canvas.Clear(model.Background.ToSKColor()); } // SVG export does not work with UseTextShaping=true. However SVG does text shaping by itself anyway, so we can just disable it using var context = new SkiaRenderContext { RenderTarget = RenderTarget.VectorGraphic, SkCanvas = canvas, UseTextShaping = false, DpiScale = Dpi / 96 }; //use fake dpi to scale, looks much better! model.Update(true); model.Render(context, new OxyRect(0, 0, Width / context.DpiScale, Height / context.DpiScale)); }
/// <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()); }
/// <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); } } }
/// <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) { TextFormattingMode = this.TextFormattingMode }; model.Update(true); model.Render(rc, width, height); canvas.UpdateLayout(); xpsDocumentWriter.Write(canvas); } }
/// <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); } } }
/// <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); } } }
/// <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; } }
/// <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); }
/// <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); } }