-
Notifications
You must be signed in to change notification settings - Fork 1
/
PngExporter.cs
108 lines (97 loc) · 3.67 KB
/
PngExporter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PngExporter.cs" company="OxyPlot">
// Copyright (c) 2014 OxyPlot contributors
// </copyright>
// <summary>
// Provides functionality to export plots to png.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace OxyPlot.EtoForms
{
using Eto.Drawing;
//using System.Drawing.Imaging;
using System.IO;
/// <summary>
/// Provides functionality to export plots to png.
/// </summary>
public class PngExporter : IExporter
{
/// <summary>
/// Initializes a new instance of the <see cref="PngExporter" /> class.
/// </summary>
public PngExporter()
{
this.Width = 700;
this.Height = 400;
this.Resolution = 96;
this.Background = OxyColors.White;
}
/// <summary>
/// Gets or sets the width of the output image.
/// </summary>
public int Width { get; set; }
/// <summary>
/// Gets or sets the height of the output image.
/// </summary>
public int Height { get; set; }
/// <summary>
/// Gets or sets the resolution (dpi) of the output image.
/// </summary>
public double Resolution { get; set; }
/// <summary>
/// Gets or sets the background color.
/// </summary>
public OxyColor Background { get; set; }
/// <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>
/// <param name="resolution">The resolution.</param>
public static void Export(IPlotModel model, string fileName, int width, int height, OxyColor background, double resolution = 96)
{
var exporter = new PngExporter { Width = width, Height = height, Background = background, Resolution = resolution };
using (var stream = File.Create(fileName))
{
exporter.Export(model, 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 bm = this.ExportToBitmap(model))
{
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.Format32bppRgba);
using (var g = new Graphics(bm))
{
if (this.Background.IsVisible())
{
g.FillRectangle(this.Background.ToEto(), 0, 0, this.Width, this.Height);
}
using (var rc = new GraphicsRenderContext(g) { RendersToScreen = false })
{
model.Update(true);
model.Render(rc, this.Width, this.Height);
}
return bm;
}
}
}
}