ImageSavingHelper.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.IO;
  5. using SkiaSharp;
  6. namespace Avalonia.Skia.Helpers
  7. {
  8. /// <summary>
  9. /// Helps with saving images to stream/file.
  10. /// </summary>
  11. public static class ImageSavingHelper
  12. {
  13. /// <summary>
  14. /// Save Skia image to a file.
  15. /// </summary>
  16. /// <param name="image">Image to save</param>
  17. /// <param name="fileName">Target file.</param>
  18. public static void SaveImage(SKImage image, string fileName)
  19. {
  20. if (image == null) throw new ArgumentNullException(nameof(image));
  21. if (fileName == null) throw new ArgumentNullException(nameof(fileName));
  22. using (var stream = File.Create(fileName))
  23. {
  24. SaveImage(image, stream);
  25. }
  26. }
  27. /// <summary>
  28. /// Save Skia image to a stream.
  29. /// </summary>
  30. /// <param name="image">Image to save</param>
  31. /// <param name="stream">Target stream.</param>
  32. public static void SaveImage(SKImage image, Stream stream)
  33. {
  34. if (image == null) throw new ArgumentNullException(nameof(image));
  35. if (stream == null) throw new ArgumentNullException(nameof(stream));
  36. using (var data = image.Encode())
  37. {
  38. data.SaveTo(stream);
  39. }
  40. }
  41. }
  42. }