// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) 2014 OxyPlot contributors // // // Provides functionality to export plots to png. // // -------------------------------------------------------------------------------------------------------------------- namespace OxyPlot.Avalonia { using global::Avalonia; using global::Avalonia.Controls; using global::Avalonia.Media.Imaging; using global::Avalonia.Rendering; using System.IO; /// /// Provides functionality to export plots to png. /// public class PngExporter : IExporter { /// /// Initializes a new instance of the class. /// public PngExporter() { Width = 700; Height = 400; Resolution = 96; Background = OxyColors.White; } /// /// Gets or sets the width of the output image. /// public int Width { get; set; } /// /// Gets or sets the height of the output image. /// public int Height { get; set; } /// /// Gets or sets the resolution of the output image. /// /// The resolution in dots per inch (dpi). public int Resolution { get; set; } /// /// Gets or sets the background color. /// public OxyColor Background { get; set; } /// /// Exports the specified plot model to a stream. /// /// The model to export. /// The stream. /// The width of the output bitmap. /// The height of the output bitmap. /// The background color. The default value is null. /// The resolution (resolution). The default value is 96. 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); } /// /// Exports the specified plot model to a bitmap. /// /// The plot model. /// The width. /// The height. /// The background. /// The resolution (dpi). /// A bitmap. public static Bitmap ExportToBitmap( IPlotModel model, int width, int height, OxyColor background, int resolution = 96) { var exporter = new PngExporter { Width = width, Height = height, Background = background, Resolution = resolution }; return exporter.ExportToBitmap(model); } /// /// Exports the specified to the specified . /// /// The model. /// The output stream. public void Export(IPlotModel model, Stream stream) { var bmp = ExportToBitmap(model); bmp.Save(stream); } /// /// Exports the specified plot model to a bitmap. /// /// The model to export. /// A bitmap. public Bitmap 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, new OxyRect(0, 0, 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), new Vector(Resolution, Resolution)); bmp.Render(canvas); return bmp; } } }