PlotBase.Export.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="PlotBase.Export.cs" company="OxyPlot">
  3. // Copyright (c) 2014 OxyPlot contributors
  4. // </copyright>
  5. // <summary>
  6. // Represents a control that displays a <see cref="PlotModel" />.
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. namespace OxyPlot.Avalonia
  10. {
  11. using global::Avalonia.Media.Imaging;
  12. using System.IO;
  13. /// <summary>
  14. /// Represents a control that displays a <see cref="PlotModel" />.
  15. /// </summary>
  16. public partial class PlotBase
  17. {
  18. /// <summary>
  19. /// Saves the PlotView as a bitmap.
  20. /// </summary>
  21. /// <param name="stream">Stream to which to write the bitmap.</param>
  22. public void SaveBitmap(Stream stream)
  23. {
  24. SaveBitmap(stream, -1, -1, ActualModel.Background);
  25. }
  26. /// <summary>
  27. /// Saves the PlotView as a bitmap.
  28. /// </summary>
  29. /// <param name="stream">Stream to which to write the bitmap.</param>
  30. /// <param name="width">The width.</param>
  31. /// <param name="height">The height.</param>
  32. /// <param name="background">The background.</param>
  33. public void SaveBitmap(Stream stream, int width, int height, OxyColor background)
  34. {
  35. if (width <= 0)
  36. {
  37. width = (int)Bounds.Width;
  38. }
  39. if (height <= 0)
  40. {
  41. height = (int)Bounds.Height;
  42. }
  43. if (!background.IsVisible())
  44. {
  45. background = Background.ToOxyColor();
  46. }
  47. PngExporter.Export(ActualModel, stream, width, height, background);
  48. }
  49. /// <summary>
  50. /// Renders the PlotView to a bitmap.
  51. /// </summary>
  52. /// <returns>A bitmap.</returns>
  53. public Bitmap ToBitmap()
  54. {
  55. var background = ActualModel.Background.IsVisible() ? ActualModel.Background : Background.ToOxyColor();
  56. return PngExporter.ExportToBitmap(ActualModel, (int)Bounds.Width, (int)Bounds.Height, background);
  57. }
  58. }
  59. }