Exporter.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using Microsoft.Win32;
  13. using PixiEditorDotNetCore3.Models.Dialogs;
  14. using PixiEditorDotNetCore3.Models.Enums;
  15. namespace PixiEditorDotNetCore3.Models.IO
  16. {
  17. public class Exporter
  18. {
  19. public static string SavePath = null;
  20. public static Size FileDimensions;
  21. /// <summary>
  22. /// Creates ExportFileDialog to get width, height and path of file.
  23. /// </summary>
  24. /// <param name="type">Type of file to be saved in.</param>
  25. /// <param name="imageToSave">Image to be saved as file.</param>
  26. public static void Export(FileType type, Image imageToSave, Size fileDimensions)
  27. {
  28. ExportFileDialog info = new ExportFileDialog(fileDimensions);
  29. //If OK on dialog has been clicked
  30. if (info.ShowDialog() == true)
  31. {
  32. //If sizes are incorrect
  33. if (info.FileWidth < imageToSave.Width || info.FileHeight < imageToSave.Height)
  34. {
  35. MessageBox.Show("Incorrect height or width value", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  36. return;
  37. }
  38. SavePath = info.FilePath;
  39. FileDimensions = new Size(info.FileWidth, info.FileHeight);
  40. SaveAsPng(info.FilePath, (int)imageToSave.Width, (int)imageToSave.Height, info.FileHeight, info.FileWidth, imageToSave);
  41. }
  42. }
  43. /// <summary>
  44. /// Saves file with info that has been recieved from ExportFileDialog before, doesn't work without before Export() usage.
  45. /// </summary>
  46. /// <param name="type">Type of file</param>
  47. /// <param name="imageToSave">Image to be saved as file.</param>
  48. public static void ExportWithoutDialog(FileType type, Image imageToSave)
  49. {
  50. try
  51. {
  52. SaveAsPng(SavePath, (int)imageToSave.Width, (int)imageToSave.Height, (int)FileDimensions.Height, (int)FileDimensions.Width, imageToSave);
  53. }
  54. catch (Exception ex)
  55. {
  56. MessageBox.Show(ex.Message);
  57. }
  58. }
  59. /// <summary>
  60. /// Saves image to PNG file
  61. /// </summary>
  62. /// <param name="savePath">Save file path</param>
  63. /// <param name="originalWidth">Original width of image</param>
  64. /// <param name="originalHeight">Original height of image</param>
  65. /// <param name="exportWidth">File width</param>
  66. /// <param name="exportHeight">File height</param>
  67. /// <param name="imageToExport">Image to be saved</param>
  68. private static void SaveAsPng(string savePath, int originalWidth, int originalHeight, int exportWidth, int exportHeight, Image imageToExport)
  69. {
  70. Rect bounds = VisualTreeHelper.GetDescendantBounds(imageToExport);
  71. double dpi = 96d;
  72. RenderTargetBitmap rtb = new RenderTargetBitmap(originalWidth * (exportWidth / originalWidth), originalHeight * (exportHeight / originalHeight), dpi, dpi, PixelFormats.Default);
  73. DrawingVisual dv = new DrawingVisual();
  74. using (DrawingContext dc = dv.RenderOpen())
  75. {
  76. VisualBrush vb = new VisualBrush(imageToExport);
  77. dc.DrawRectangle(vb, null, new Rect(new Point(), new Size(originalWidth * (exportWidth / originalWidth), originalHeight * (exportHeight / originalHeight))));
  78. }
  79. rtb.Render(dv);
  80. BitmapEncoder pngEncoder = new PngBitmapEncoder();
  81. pngEncoder.Frames.Add(BitmapFrame.Create(rtb));
  82. try
  83. {
  84. MemoryStream ms = new MemoryStream();
  85. pngEncoder.Save(ms);
  86. ms.Close();
  87. File.WriteAllBytes(savePath, ms.ToArray());
  88. }
  89. catch (Exception err)
  90. {
  91. MessageBox.Show(err.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  92. }
  93. }
  94. }
  95. }