@@ -18,40 +18,54 @@ namespace OxyPlot.Core.Drawing
1818 public class PngExporter : IExporter
1919 {
2020 /// <summary>
21- /// The export.
21+ /// Gets or sets the width in pixels of the exported png.
22+ /// </summary>
23+ public int Width { get ; set ; }
24+
25+ /// <summary>
26+ /// Gets or sets the height in pixels of the exported png.
2227 /// </summary>
23- /// <param name="model">
24- /// The model.
25- /// </param>
26- /// <param name="fileName">
27- /// The file name.
28- /// </param>
29- /// <param name="width">
30- /// The width.
31- /// </param>
32- /// <param name="height">
33- /// The height.
34- /// </param>
35- /// <param name="background">
36- /// The background.
37- /// </param>
38- public static void Export ( IPlotModel model , string fileName , int width , int height , Brush background = null )
28+ public int Height { get ; set ; }
29+
30+ /// <summary>
31+ /// Gets or sets the background color of the exported png.
32+ /// </summary>
33+ public OxyColor Background { get ; set ; }
34+
35+ /// <summary>
36+ /// Gets or sets the resolution in dpi of the exported png.
37+ /// </summary>
38+ public double Resolution { get ; set ; }
39+
40+ /// <summary>
41+ /// Exports the specified <see cref="PlotModel" /> to the specified file.
42+ /// </summary>
43+ /// <param name="model">The model.</param>
44+ /// <param name="fileName">The file name.</param>
45+ /// <param name="width">The width.</param>
46+ /// <param name="height">The height.</param>
47+ /// <param name="background">The background color.</param>
48+ /// <param name="resolution">The resolution in dpi (defaults to 96dpi).</param>
49+ public static void Export ( IPlotModel model , string fileName , int width , int height , OxyColor background , double resolution = 96 )
3950 {
40- using ( var bm = new Bitmap ( width , height ) )
41- {
42- using ( var g = Graphics . FromImage ( bm ) )
43- {
44- if ( background != null )
45- {
46- g . FillRectangle ( background , 0 , 0 , width , height ) ;
47- }
51+ Brush brush = background . IsInvisible ( ) ? null : background . ToBrush ( ) ;
52+ Export ( model , fileName , width , height , brush , resolution ) ;
53+ }
4854
49- var rc = new GraphicsRenderContext ( g ) { RendersToScreen = false } ;
50- rc . SetGraphicsTarget ( g ) ;
51- model . Update ( true ) ;
52- model . Render ( rc , width , height ) ;
53- bm . Save ( fileName , System . Drawing . Imaging . ImageFormat . Png ) ;
54- }
55+ /// <summary>
56+ /// Exports the specified <see cref="PlotModel" /> to the specified file.
57+ /// </summary>
58+ /// <param name="model">The model.</param>
59+ /// <param name="fileName">The file name.</param>
60+ /// <param name="width">The width.</param>
61+ /// <param name="height">The height.</param>
62+ /// <param name="background">The background color (defaults to null).</param>
63+ /// <param name="resolution">The resolution in dpi (defaults to 96dpi).</param>
64+ public static void Export ( IPlotModel model , string fileName , int width , int height , Brush background = null , double resolution = 96 )
65+ {
66+ using ( var bm = ExportToBitmap ( model , width , height , background , resolution ) )
67+ {
68+ bm . Save ( fileName , System . Drawing . Imaging . ImageFormat . Png ) ;
5569 }
5670 }
5771
@@ -73,13 +87,14 @@ public static void Export(IPlotModel model, Stream stream, int width, int height
7387 }
7488
7589 /// <summary>
76- /// Exports the specified <see cref="PlotModel" /> to the specified <see cref="Stream " />.
90+ /// Exports the specified <see cref="PlotModel" /> to a <see cref="MemoryStream " />.
7791 /// </summary>
7892 /// <param name="model">The model.</param>
7993 /// <param name="width">The width.</param>
8094 /// <param name="height">The height.</param>
8195 /// <param name="background">The background color.</param>
8296 /// <param name="resolution">The resolution in dpi (defaults to 96dpi).</param>
97+ /// <returns>A <see cref="Stream"/>.</returns>
8398 public static Stream ExportToStream ( IPlotModel model , int width , int height , OxyColor background , double resolution = 96 )
8499 {
85100 var stream = new MemoryStream ( ) ;
@@ -95,22 +110,34 @@ public static Stream ExportToStream(IPlotModel model, int width, int height, Oxy
95110 /// Exports the specified <see cref="PlotModel" /> to a <see cref="Bitmap" />.
96111 /// </summary>
97112 /// <param name="model">The model to export.</param>
98- /// <param name="width"></param>
99- /// <param name="height"></param>
100- /// <param name="background"></param>
101- /// <param name="resolution"></param>
102- /// <returns>A bitmap .</returns>
113+ /// <param name="width">The width. </param>
114+ /// <param name="height">The height. </param>
115+ /// <param name="background">The background color. </param>
116+ /// <param name="resolution">The resolution in dpi (defaults to 96dpi). </param>
117+ /// <returns>A <see cref="Bitmap"/> .</returns>
103118 public static Bitmap ExportToBitmap ( IPlotModel model , int width , int height , OxyColor background , double resolution = 96 )
119+ {
120+ Brush brush = background . IsInvisible ( ) ? null : background . ToBrush ( ) ;
121+ return ExportToBitmap ( model , width , height , brush , resolution ) ;
122+ }
123+
124+ /// <summary>
125+ /// Exports the specified <see cref="PlotModel" /> to a <see cref="Bitmap" />.
126+ /// </summary>
127+ /// <param name="model">The model to export.</param>
128+ /// <param name="width">The width.</param>
129+ /// <param name="height">The height.</param>
130+ /// <param name="background">The background color.</param>
131+ /// <param name="resolution">The resolution in dpi (defaults to 96dpi).</param>
132+ /// <returns>A <see cref="Bitmap"/>.</returns>
133+ public static Bitmap ExportToBitmap ( IPlotModel model , int width , int height , Brush background , double resolution = 96 )
104134 {
105135 var bm = new Bitmap ( width , height ) ;
106136 using ( var g = Graphics . FromImage ( bm ) )
107137 {
108- if ( background . IsVisible ( ) )
138+ if ( background != null )
109139 {
110- using ( var brush = background . ToBrush ( ) )
111- {
112- g . FillRectangle ( brush , 0 , 0 , width , height ) ;
113- }
140+ g . FillRectangle ( background , 0 , 0 , width , height ) ;
114141 }
115142
116143 using ( var rc = new GraphicsRenderContext ( g ) { RendersToScreen = false } )
@@ -126,26 +153,17 @@ public static Bitmap ExportToBitmap(IPlotModel model, int width, int height, Oxy
126153 }
127154
128155 /// <summary>
129- /// Gets or sets the width in pixels of the exported png .
156+ /// Exports the specified <see cref="PlotModel"/> to the specified <see cref="Stream"/> .
130157 /// </summary>
131- public int Width { get ; set ; }
132-
133- /// <summary>
134- /// Gets or sets the height in pixels of the exported png.
135- /// </summary>
136- public int Height { get ; set ; }
158+ /// <param name="model">The model.</param>
159+ /// <param name="stream">The output stream.</param>
160+ public void Export ( IPlotModel model , Stream stream ) => Export ( model , stream , this . Width , this . Height , this . Background , this . Resolution ) ;
137161
138162 /// <summary>
139- /// Gets or sets the background color of the exported png .
163+ /// Exports the specified <see cref="PlotModel"/> to the specified file .
140164 /// </summary>
141- public OxyColor Background { get ; set ; }
142-
143- /// <summary>
144- /// Gets or sets the resolution in dpi of the exported png.
145- /// </summary>
146- public double Resolution { get ; set ; }
147-
148- public void Export ( IPlotModel model , Stream stream ) => Export ( model , stream , this . Width , this . Height , OxyColors . White , this . Resolution ) ;
149- public void ExportToFile ( IPlotModel model , string filename ) => Export ( model , filename , this . Width , this . Height , this . Background . ToBrush ( ) ) ;
165+ /// <param name="model">The model.</param>
166+ /// <param name="filename">The file name.</param>
167+ public void ExportToFile ( IPlotModel model , string filename ) => Export ( model , filename , this . Width , this . Height , this . Background , this . Resolution ) ;
150168 }
151169}
0 commit comments