PngExporter.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="PngExporter.cs" company="OxyPlot">
  3. // Copyright (c) 2020 OxyPlot contributors
  4. // </copyright>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. namespace OxyPlot.SkiaSharp
  7. {
  8. using global::SkiaSharp;
  9. using System.IO;
  10. /// <summary>
  11. /// Provides functionality to export plots to png using the SkiaSharp renderer.
  12. /// </summary>
  13. public class PngExporter : IExporter
  14. {
  15. /// <summary>
  16. /// Gets or sets the DPI.
  17. /// </summary>
  18. public float Dpi { get; set; } = 96;
  19. /// <summary>
  20. /// Gets or sets the export height (in pixels).
  21. /// </summary>
  22. public int Height { get; set; }
  23. /// <summary>
  24. /// Gets or sets the export width (in pixels).
  25. /// </summary>
  26. public int Width { get; set; }
  27. /// <summary>
  28. /// Gets or sets a value indicating whether text shaping should be used when rendering text.
  29. /// </summary>
  30. public bool UseTextShaping { get; set; } = true;
  31. /// <summary>
  32. /// Exports the specified model to a file.
  33. /// </summary>
  34. /// <param name="model">The model.</param>
  35. /// <param name="path">The path.</param>
  36. /// <param name="width">The width (points).</param>
  37. /// <param name="height">The height (points).</param>
  38. /// <param name="dpi">The DPI (dots per inch).</param>
  39. public static void Export(IPlotModel model, string path, int width, int height, float dpi = 96)
  40. {
  41. using var stream = File.OpenWrite(path);
  42. Export(model, stream, width, height, dpi);
  43. }
  44. /// <summary>
  45. /// Exports the specified model to a stream.
  46. /// </summary>
  47. /// <param name="model">The model.</param>
  48. /// <param name="stream">The output stream.</param>
  49. /// <param name="width">The width (points).</param>
  50. /// <param name="height">The height (points).</param>
  51. /// <param name="dpi">The DPI (dots per inch).</param>
  52. public static void Export(IPlotModel model, Stream stream, int width, int height, float dpi = 96)
  53. {
  54. var exporter = new PngExporter { Width = width, Height = height, Dpi = dpi };
  55. exporter.Export(model, stream);
  56. }
  57. /// <inheritdoc/>
  58. public void Export(IPlotModel model, Stream stream)
  59. {
  60. using var bitmap = new SKBitmap(this.Width, this.Height);
  61. using (var canvas = new SKCanvas(bitmap))
  62. using (var context = new SkiaRenderContext { RenderTarget = RenderTarget.PixelGraphic, SkCanvas = canvas, UseTextShaping = this.UseTextShaping })
  63. {
  64. var dpiScale = this.Dpi / 96;
  65. context.DpiScale = dpiScale;
  66. model.Update(true);
  67. canvas.Clear(model.Background.ToSKColor());
  68. model.Render(context, new OxyRect(0, 0, this.Width / dpiScale, this.Height / dpiScale));
  69. }
  70. using var skStream = new SKManagedWStream(stream);
  71. bitmap.Encode(skStream, SKEncodedImageFormat.Png, 0);
  72. }
  73. }
  74. }