Exporter.cs 4.2 KB

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