Skip to content

Commit 9d83ad0

Browse files
committed
Fix OxyPlot.Core.Drawing PngExporter Export to Stream background (#1382)
1 parent d791be7 commit 9d83ad0

File tree

4 files changed

+107
-71
lines changed

4 files changed

+107
-71
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ All notable changes to this project will be documented in this file.
7575
- In WPF, make sure the axes are initalized when the Model is set before the PlotView has been loaded (#1303)
7676
- Fixed MinimumSegmentLength not working for LineSeries (#1044)
7777
- Fixed rendering issues with MagnitudeAxisFullPlotArea (#1364)
78+
- OxyPlot.Core.Drawing PngExporter Export to Stream background (#1382)
7879

7980
## [1.0.0] - 2016-09-11
8081
### Added

Source/Examples/Core.Drawing/Example1/Example1.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
<TargetFramework>netcoreapp2.1</TargetFramework>
66
</PropertyGroup>
77

8-
<ItemGroup>
9-
<PackageReference Include="System.Drawing.Common" Version="4.6.0-preview.19073.11" />
10-
</ItemGroup>
11-
128
<ItemGroup>
139
<ProjectReference Include="..\..\..\OxyPlot.Core.Drawing\OxyPlot.Core.Drawing.csproj" />
1410
</ItemGroup>

Source/Examples/Core.Drawing/Example1/Program.cs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,57 @@ static void Main(string[] args)
1515
{
1616
var outputUsingMemStream = "test-oxyplot-memstream.png";
1717
var outputToFile = "test-oxyplot-file.png";
18+
var outputToFileBrush = "test-oxyplot-file-brush.png";
1819
var outputExportFileStream = "test-oxyplot-stream-export.png";
20+
var outputExportBitmap = "test-oxyplot-exportobitmap.png";
21+
var outputExportBitmapBrush = "test-oxyplot-exportobitmap-brush.png";
22+
var outputExportStreamOOP = "test-oxyplot-ExportToStream.png";
1923
var outputExportFileOOP = "test-oxyplot-ExportToFile.png";
2024

2125
var width = 1024;
2226
var height = 768;
2327

2428
var model = BuildPlotModel();
2529

30+
// export to file using static methods
31+
PngExporter.Export(model, outputToFile, width, height, OxyColors.LightGray);
2632

27-
PngExporter.Export(model, outputToFile, width, height, Brushes.White);
33+
PngExporter.Export(model, outputToFileBrush, width, height, Brushes.LightGray);
2834

35+
// export to stream using static methods
2936
using (var stream = new MemoryStream())
3037
{
31-
PngExporter.Export(model, stream, width, height, OxyColors.White, 96);
38+
PngExporter.Export(model, stream, width, height, OxyColors.LightGray, 96);
3239
System.IO.File.WriteAllBytes(outputUsingMemStream, stream.ToArray());
3340
}
3441

35-
using (var pngStream = PngExporter.ExportToStream(model, width, height, OxyColors.White))
42+
using (var pngStream = PngExporter.ExportToStream(model, width, height, OxyColors.LightGray))
3643
{
3744
var fileStream = new System.IO.FileStream(outputExportFileStream, FileMode.Create);
3845
pngStream.CopyTo(fileStream);
3946
fileStream.Flush();
4047
}
4148

42-
var stream2 = new MemoryStream();
43-
var pngExporter = new PngExporter { Width = width, Height = height, Background = OxyColors.White };
44-
pngExporter.Export(model, stream2);
49+
// export to bitmap using static methods
50+
using (var bm = PngExporter.ExportToBitmap(model, width, height, OxyColors.LightGray))
51+
{
52+
bm.Save(outputExportBitmap);
53+
}
54+
55+
using (var bm = PngExporter.ExportToBitmap(model, width, height, Brushes.LightGray))
56+
{
57+
bm.Save(outputExportBitmapBrush);
58+
}
59+
60+
// export using the instance methods
61+
using (var stream = new MemoryStream())
62+
{
63+
var pngExporter = new PngExporter { Width = width, Height = height, Background = OxyColors.LightGray };
64+
pngExporter.Export(model, stream);
65+
System.IO.File.WriteAllBytes(outputExportStreamOOP, stream.ToArray());
66+
}
4567

46-
// Write to a file, OOP
47-
var pngExporter2 = new PngExporter { Width = width, Height = height, Background = OxyColors.White };
68+
var pngExporter2 = new PngExporter { Width = width, Height = height, Background = OxyColors.LightGray };
4869
pngExporter2.ExportToFile(model, outputExportFileOOP);
4970
}
5071

Source/OxyPlot.Core.Drawing/PngExporter.cs

Lines changed: 77 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)