JpegExporter.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="JpegExporter.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 jpeg using the SkiaSharp renderer.
  12. /// </summary>
  13. public class JpegExporter : 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 the export quality (0-100).
  29. /// </summary>
  30. public int Quality { get; set; } = 90;
  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="quality">The export quality (0-100).</param>
  39. /// <param name="dpi">The DPI (dots per inch).</param>
  40. public static void Export(IPlotModel model, string path, int width, int height, int quality, float dpi = 96)
  41. {
  42. using var stream = File.OpenWrite(path);
  43. Export(model, stream, width, height, quality, dpi);
  44. }
  45. /// <summary>
  46. /// Exports the specified model to a stream.
  47. /// </summary>
  48. /// <param name="model">The model.</param>
  49. /// <param name="stream">The output stream.</param>
  50. /// <param name="width">The width (points).</param>
  51. /// <param name="height">The height (points).</param>
  52. /// <param name="quality">The export quality (0-100).</param>
  53. /// <param name="dpi">The DPI (dots per inch).</param>
  54. public static void Export(IPlotModel model, Stream stream, int width, int height, int quality, float dpi = 96)
  55. {
  56. var exporter = new JpegExporter { Width = width, Height = height, Quality = quality, Dpi = dpi };
  57. exporter.Export(model, stream);
  58. }
  59. /// <inheritdoc/>
  60. public void Export(IPlotModel model, Stream stream)
  61. {
  62. using var bitmap = new SKBitmap(this.Width, this.Height);
  63. using (var canvas = new SKCanvas(bitmap))
  64. using (var context = new SkiaRenderContext { RenderTarget = RenderTarget.PixelGraphic, SkCanvas = canvas })
  65. {
  66. canvas.Clear(SKColors.White);
  67. var dpiScale = this.Dpi / 96;
  68. context.DpiScale = dpiScale;
  69. model.Update(true);
  70. var backgroundColor = model.Background;
  71. // jpg doesn't support transparency
  72. if (!backgroundColor.IsVisible())
  73. {
  74. backgroundColor = OxyColors.White;
  75. }
  76. canvas.Clear(backgroundColor.ToSKColor());
  77. model.Render(context, new OxyRect(0, 0, this.Width / dpiScale, this.Height / dpiScale));
  78. }
  79. using var skStream = new SKManagedWStream(stream);
  80. bitmap.Encode(skStream, SKEncodedImageFormat.Jpeg, this.Quality);
  81. }
  82. }
  83. }