// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) 2014 OxyPlot contributors
//
//
// Provides functionality to export plots to png.
//
// --------------------------------------------------------------------------------------------------------------------
namespace OxyPlot.ImageSharp
{
using System.IO;
///
/// Provides functionality to export plots to png.
///
public class PngExporter : IExporter
{
///
/// Initializes a new instance of the class.
///
/// The width in pixels of the exported png.
/// The height in pixels of the exported png.
/// The resolution in dots per inch (DPI) of the exported png.
public PngExporter(int width, int height, double resolution = 96)
{
this.Width = width;
this.Height = height;
this.Resolution = resolution;
}
///
/// Gets or sets the width in pixels of the exported png.
///
public int Width { get; set; }
///
/// Gets or sets the height in pixels of the exported png.
///
public int Height { get; set; }
///
/// Gets or sets the resolution in dots per inch (DPI) of the exported png.
///
public double Resolution { get; set; }
///
/// Exports the specified to the specified file.
///
/// The model.
/// The file name.
/// The width.
/// The height.
/// The resolution in dpi (defaults to 96dpi).
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);
}
}
///
/// Exports the specified to the specified .
///
/// The model.
/// The output stream.
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);
}
}
}
}