Browse Source

Export supports: ImageFormat.Png, ImageFormat.Jpeg

tomaszkot 3 năm trước cách đây
mục cha
commit
afd6703e6a

+ 17 - 0
PixiEditor/Models/Dialogs/ExportFileDialog.cs

@@ -1,10 +1,13 @@
 using PixiEditor.Views;
+using System.Drawing.Imaging;
 using System.Windows;
 
 namespace PixiEditor.Models.Dialogs
 {
     public class ExportFileDialog : CustomDialog
     {
+        ImageFormat _chosenFormat;
+
         private int fileHeight;
 
         private string filePath;
@@ -56,6 +59,19 @@ namespace PixiEditor.Models.Dialogs
             }
         }
 
+        public ImageFormat ChosenFormat
+        {
+            get => _chosenFormat;
+            set
+            {
+                if (_chosenFormat != value)
+                {
+                    _chosenFormat = value;
+                    RaisePropertyChanged(nameof(ChosenFormat));
+                }
+            }
+        }
+
         public override bool ShowDialog()
         {
             ExportFilePopup popup = new ExportFilePopup
@@ -69,6 +85,7 @@ namespace PixiEditor.Models.Dialogs
                 FileWidth = popup.SaveWidth;
                 FileHeight = popup.SaveHeight;
                 FilePath = popup.SavePath;
+                ChosenFormat = popup.SaveFormat;
             }
 
             return (bool)popup.DialogResult;

+ 10 - 5
PixiEditor/Models/IO/Exporter.cs

@@ -4,6 +4,7 @@ using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Dialogs;
 using SkiaSharp;
 using System;
+using System.Drawing.Imaging;
 using System.IO;
 using System.IO.Compression;
 using System.Runtime.InteropServices;
@@ -60,10 +61,13 @@ namespace PixiEditor.Models.IO
             // If OK on dialog has been clicked
             if (info.ShowDialog())
             {
-                SaveAsPng(info.FilePath, info.FileWidth, info.FileHeight, bitmap);
+                if(info.ChosenFormat == ImageFormat.Png)
+                    SaveAs(new PngBitmapEncoder(), info.FilePath, info.FileWidth, info.FileHeight, bitmap);
+                else if (info.ChosenFormat == ImageFormat.Jpeg)
+                    SaveAs(new JpegBitmapEncoder(), info.FilePath, info.FileWidth, info.FileHeight, bitmap);
             }
         }
-
+                
         public static void SaveAsGZippedBytes(string path, Surface surface)
         {
             SaveAsGZippedBytes(path, surface, SKRectI.Create(0, 0, surface.Width, surface.Height));
@@ -94,18 +98,19 @@ namespace PixiEditor.Models.IO
         /// <summary>
         ///     Saves image to PNG file.
         /// </summary>
+        /// <param name="encoder">encoder to do the job.</param>
         /// <param name="savePath">Save file path.</param>
         /// <param name="exportWidth">File width.</param>
         /// <param name="exportHeight">File height.</param>
         /// <param name="bitmap">Bitmap to save.</param>
-        public static void SaveAsPng(string savePath, int exportWidth, int exportHeight, WriteableBitmap bitmap)
+        private static void SaveAs(BitmapEncoder encoder, string savePath, int exportWidth, int exportHeight, WriteableBitmap bitmap)
         {
             try
             {
                 bitmap = bitmap.Resize(exportWidth, exportHeight, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
-                using (FileStream stream = new FileStream(savePath, FileMode.Create))
+                using (var stream = new FileStream(savePath, FileMode.Create))
                 {
-                    PngBitmapEncoder encoder = new PngBitmapEncoder();
+                    encoder = new JpegBitmapEncoder();
                     encoder.Frames.Add(BitmapFrame.Create(bitmap));
                     encoder.Save(stream);
                 }

+ 45 - 3
PixiEditor/ViewModels/SaveFilePopupViewModel.cs

@@ -1,12 +1,19 @@
 using Microsoft.Win32;
 using PixiEditor.Helpers;
+using System;
+using System.Drawing.Imaging;
+using System.IO;
+using System.Linq;
+using System.Reflection;
 using System.Windows;
 
 namespace PixiEditor.ViewModels
 {
     internal class SaveFilePopupViewModel : ViewModelBase
     {
+        ImageFormat[] _formats = new[] { ImageFormat.Png, ImageFormat.Jpeg };
         private string _filePath;
+        private ImageFormat _chosenFormat;
 
         public SaveFilePopupViewModel()
         {
@@ -27,11 +34,44 @@ namespace PixiEditor.ViewModels
                 if (_filePath != value)
                 {
                     _filePath = value;
-                    RaisePropertyChanged("FilePath");
+                    RaisePropertyChanged(nameof(FilePath));
                 }
             }
         }
 
+        public ImageFormat ChosenFormat 
+        { 
+            get => _chosenFormat;
+            set
+            {
+                if (_chosenFormat != value)
+                {
+                    _chosenFormat = value;
+                    RaisePropertyChanged(nameof(ChosenFormat));
+                }
+            }
+        }
+
+        string GetFormattedString(ImageFormat imageFormat)
+        {
+            var formatLower = imageFormat.ToString().ToLower();
+            return $"{imageFormat} Image (.{formatLower}) | *.{formatLower}";
+        }
+
+        string BuildFilter()
+        {
+            var filter = string.Join("|", _formats.Select(i => GetFormattedString(i)));
+            return filter;
+        }
+
+        ImageFormat ParseImageFormat(string fileExtension)
+        {
+            fileExtension = fileExtension.Replace(".", "");
+            return (ImageFormat)typeof(ImageFormat)
+                    .GetProperty(fileExtension, BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase)
+                    .GetValue(null);
+        }
+
         /// <summary>
         ///     Command that handles Path choosing to save file
         /// </summary>
@@ -41,13 +81,14 @@ namespace PixiEditor.ViewModels
             {
                 Title = "Export path",
                 CheckPathExists = true,
-                DefaultExt = "PNG Image (.png) | *.png",
-                Filter = "PNG Image (.png) | *.png"
+                DefaultExt = "." + _formats.First().ToString().ToLower(),
+                Filter = BuildFilter()
             };
             if (path.ShowDialog() == true)
             {
                 if (string.IsNullOrEmpty(path.FileName) == false)
                 {
+                    ChosenFormat = ParseImageFormat(Path.GetExtension(path.SafeFileName));
                     return path.FileName;
                 }
             }
@@ -71,6 +112,7 @@ namespace PixiEditor.ViewModels
             if (path == null)
                 return;
             FilePath = path;
+            
             ((Window)parameter).DialogResult = true;
             CloseButton(parameter);
         }

+ 7 - 0
PixiEditor/Views/Dialogs/ExportFilePopup.xaml.cs

@@ -1,4 +1,5 @@
 using PixiEditor.ViewModels;
+using System.Drawing.Imaging;
 using System.Windows;
 using System.Windows.Input;
 
@@ -52,5 +53,11 @@ namespace PixiEditor.Views
             get => dataContext.FilePath;
             set => dataContext.FilePath = value;
         }
+
+        public ImageFormat SaveFormat 
+        {
+            get => dataContext.ChosenFormat;
+            set => dataContext.ChosenFormat = value;
+        }
     }
 }