PngExporter.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="PngExporter.cs" company="OxyPlot">
  3. // Copyright (c) 2014 OxyPlot contributors
  4. // </copyright>
  5. // <summary>
  6. // Provides functionality to export plots to png.
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. namespace OxyPlot.Avalonia
  10. {
  11. using global::Avalonia;
  12. using global::Avalonia.Controls;
  13. using global::Avalonia.Media.Imaging;
  14. using global::Avalonia.Rendering;
  15. using System.IO;
  16. /// <summary>
  17. /// Provides functionality to export plots to png.
  18. /// </summary>
  19. public class PngExporter : IExporter
  20. {
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="PngExporter" /> class.
  23. /// </summary>
  24. public PngExporter()
  25. {
  26. Width = 700;
  27. Height = 400;
  28. Resolution = 96;
  29. Background = OxyColors.White;
  30. }
  31. /// <summary>
  32. /// Gets or sets the width of the output image.
  33. /// </summary>
  34. public int Width { get; set; }
  35. /// <summary>
  36. /// Gets or sets the height of the output image.
  37. /// </summary>
  38. public int Height { get; set; }
  39. /// <summary>
  40. /// Gets or sets the resolution of the output image.
  41. /// </summary>
  42. /// <value>The resolution in dots per inch (dpi).</value>
  43. public int Resolution { get; set; }
  44. /// <summary>
  45. /// Gets or sets the background color.
  46. /// </summary>
  47. public OxyColor Background { get; set; }
  48. /// <summary>
  49. /// Exports the specified plot model to a stream.
  50. /// </summary>
  51. /// <param name="model">The model to export.</param>
  52. /// <param name="stream">The stream.</param>
  53. /// <param name="width">The width of the output bitmap.</param>
  54. /// <param name="height">The height of the output bitmap.</param>
  55. /// <param name="background">The background color. The default value is <c>null</c>.</param>
  56. /// <param name="resolution">The resolution (resolution). The default value is 96.</param>
  57. public static void Export(IPlotModel model, Stream stream, int width, int height, OxyColor background, int resolution = 96)
  58. {
  59. var exporter = new PngExporter { Width = width, Height = height, Background = background, Resolution = resolution };
  60. exporter.Export(model, stream);
  61. }
  62. /// <summary>
  63. /// Exports the specified plot model to a bitmap.
  64. /// </summary>
  65. /// <param name="model">The plot model.</param>
  66. /// <param name="width">The width.</param>
  67. /// <param name="height">The height.</param>
  68. /// <param name="background">The background.</param>
  69. /// <param name="resolution">The resolution (dpi).</param>
  70. /// <returns>A bitmap.</returns>
  71. public static Bitmap ExportToBitmap(
  72. IPlotModel model,
  73. int width,
  74. int height,
  75. OxyColor background,
  76. int resolution = 96)
  77. {
  78. var exporter = new PngExporter { Width = width, Height = height, Background = background, Resolution = resolution };
  79. return exporter.ExportToBitmap(model);
  80. }
  81. /// <summary>
  82. /// Exports the specified <see cref="PlotModel"/> to the specified <see cref="Stream"/>.
  83. /// </summary>
  84. /// <param name="model">The model.</param>
  85. /// <param name="stream">The output stream.</param>
  86. public void Export(IPlotModel model, Stream stream)
  87. {
  88. var bmp = ExportToBitmap(model);
  89. bmp.Save(stream);
  90. }
  91. /// <summary>
  92. /// Exports the specified plot model to a bitmap.
  93. /// </summary>
  94. /// <param name="model">The model to export.</param>
  95. /// <returns>A bitmap.</returns>
  96. public Bitmap ExportToBitmap(IPlotModel model)
  97. {
  98. var scale = 96d / Resolution;
  99. var canvas = new Canvas { Width = Width * scale, Height = Height * scale, Background = Background.ToBrush() };
  100. canvas.Measure(new Size(canvas.Width, canvas.Height));
  101. canvas.Arrange(new Rect(0, 0, canvas.Width, canvas.Height));
  102. var rc = new CanvasRenderContext(canvas) { RendersToScreen = false };
  103. model.Update(true);
  104. model.Render(rc, new OxyRect(0, 0, canvas.Width, canvas.Height));
  105. canvas.Measure(new Size(canvas.Width, canvas.Height));
  106. canvas.Arrange(new Rect(0, 0, canvas.Width, canvas.Height));
  107. var bmp = new RenderTargetBitmap(new PixelSize(Width, Height), new Vector(Resolution, Resolution));
  108. bmp.Render(canvas);
  109. return bmp;
  110. }
  111. }
  112. }