-
Notifications
You must be signed in to change notification settings - Fork 980
Expand file tree
/
Copy pathJpegExporter.cs
More file actions
90 lines (81 loc) · 3.56 KB
/
JpegExporter.cs
File metadata and controls
90 lines (81 loc) · 3.56 KB
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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="JpegExporter.cs" company="OxyPlot">
// Copyright (c) 2014 OxyPlot contributors
// </copyright>
// <summary>
// Provides functionality to export plots to jpeg.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
#nullable enable
namespace OxyPlot.ImageSharp
{
using System.IO;
/// <summary>
/// Provides functionality to export plots to jpeg.
/// </summary>
public class JpegExporter : IExporter
{
/// <summary>
/// Initializes a new instance of the <see cref="JpegExporter"/> class.
/// </summary>
/// <param name="width">The width in pixels of the exported jpeg.</param>
/// <param name="height">The height in pixels of the exported jpeg.</param>
/// <param name="resolution">The resolution in dots per inch (DPI) of the exported jpeg.</param>
/// <param name="quality">The quality of the exported jpeg, a value between 0 and 100.</param>
public JpegExporter(int width, int height, double resolution = 96, int quality = 75)
{
this.Width = width;
this.Height = height;
this.Resolution = resolution;
this.Quality = quality;
}
/// <summary>
/// Gets or sets the width in pixels of the exported jpeg.
/// </summary>
public int Width { get; set; }
/// <summary>
/// Gets or sets the height in pixels of the exported jpeg.
/// </summary>
public int Height { get; set; }
/// <summary>
/// Gets or sets the resolution in dots per inch (DPI) of the exported jpeg.
/// </summary>
public double Resolution { get; set; }
/// <summary>
/// Gets or sets the quality of the exported jpeg, a value between 0 and 100.
/// </summary>
public int Quality { get; set; }
/// <summary>
/// Exports the specified <see cref="PlotModel" /> to the specified 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="resolution">The resolution in dpi (defaults to 96dpi).</param>
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);
}
}
/// <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);
}
}
}
}