// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) 2014 OxyPlot contributors
//
//
// Provides functionality to export plots to jpeg.
//
// --------------------------------------------------------------------------------------------------------------------
namespace OxyPlot.ImageSharp
{
using System.IO;
///
/// Provides functionality to export plots to jpeg.
///
public class JpegExporter : IExporter
{
///
/// Initializes a new instance of the class.
///
/// The width in pixels of the exported jpeg.
/// The height in pixels of the exported jpeg.
/// The resolution in dots per inch (DPI) of the exported jpeg.
/// The quality of the exported jpeg, a value between 0 and 100.
public JpegExporter(int width, int height, double resolution = 96, int quality = 75)
{
this.Width = width;
this.Height = height;
this.Resolution = resolution;
this.Quality = quality;
}
///
/// Gets or sets the width in pixels of the exported jpeg.
///
public int Width { get; set; }
///
/// Gets or sets the height in pixels of the exported jpeg.
///
public int Height { get; set; }
///
/// Gets or sets the resolution in dots per inch (DPI) of the exported jpeg.
///
public double Resolution { get; set; }
///
/// Gets or sets the quality of the exported jpeg, a value between 0 and 100.
///
public int Quality { 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 JpegExporter(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)
{
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);
}
}
}
}