Browse Source

Save works

Frytek 5 years ago
parent
commit
c1b6ceb79a

+ 22 - 1
PixiEditor/Models/Controllers/BitmapOperationsUtility.cs

@@ -138,7 +138,7 @@ namespace PixiEditor.Models.Controllers
             }
         }
 
-        public void AddNewLayer(string name, int height, int width, bool setAsActive = true)
+        public void AddNewLayer(string name, int width, int height, bool setAsActive = true)
         {
             Layers.Add(new Layer(name, width, height));
             if (setAsActive)
@@ -154,6 +154,27 @@ namespace PixiEditor.Models.Controllers
             LayersChanged?.Invoke(this, new LayersChangedEventArgs(index, LayerAction.SetActive));
         }
 
+        public WriteableBitmap GetCombinedLayersBitmap()
+        {
+            WriteableBitmap finalBitmap = Layers[0].LayerBitmap.Clone();
+            finalBitmap.Lock();
+            for (int i = 1; i < Layers.Count; i++)
+            {
+                for (int y = 0; y < finalBitmap.Height; y++)
+                {
+                    for (int x = 0; x < finalBitmap.Width; x++)
+                    {
+                        Color color = Layers[i].LayerBitmap.GetPixel(x, y);
+                        if (color.A != 0 || color.R != 0 || color.B != 0 || color.G != 0)
+                        {
+                            finalBitmap.SetPixel(x, y, color);
+                        }
+                    }
+                }
+            }
+            finalBitmap.Unlock();
+            return finalBitmap;
+        }
     }
 }
 

+ 2 - 2
PixiEditor/Models/Controllers/PixelChangesController.cs

@@ -56,8 +56,8 @@ namespace PixiEditor.Models.Controllers
             var oldValuesTmp = new LayerChanges(new BitmapPixelChanges(oldValues), LastOldValues.LayerIndex);
             
             Tuple<LayerChanges, LayerChanges> outputChanges = new Tuple<LayerChanges, LayerChanges>(tmp, oldValuesTmp);
-            LastChanges.PixelChanges.ChangedPixels.Clear();
-            LastOldValues.PixelChanges.ChangedPixels.Clear();
+            LastChanges = null;
+            LastOldValues = null;
             return outputChanges;
         }
     }

+ 15 - 35
PixiEditor/Models/IO/Exporter.cs

@@ -24,15 +24,15 @@ namespace PixiEditor.Models.IO
         /// Creates ExportFileDialog to get width, height and path of file.
         /// </summary>
         /// <param name="type">Type of file to be saved in.</param>
-        /// <param name="imageToSave">Image to be saved as file.</param>
-        public static void Export(FileType type, Image imageToSave, Size fileDimensions)
+        /// <param name="bitmap">Bitmap to be saved as file.</param>
+        public static void Export(FileType type, WriteableBitmap bitmap, Size fileDimensions)
         {
             ExportFileDialog info = new ExportFileDialog(fileDimensions);
             //If OK on dialog has been clicked
             if (info.ShowDialog() == true)
             {
                 //If sizes are incorrect
-                if (info.FileWidth < imageToSave.Width || info.FileHeight < imageToSave.Height)
+                if (info.FileWidth < bitmap.Width || info.FileHeight < bitmap.Height)
                 {
                     MessageBox.Show("Incorrect height or width value", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                     return;
@@ -40,7 +40,7 @@ namespace PixiEditor.Models.IO
 
                 SavePath = info.FilePath;
                 FileDimensions = new Size(info.FileWidth, info.FileHeight);
-                SaveAsPng(info.FilePath, (int)imageToSave.Width, (int)imageToSave.Height, info.FileHeight, info.FileWidth, imageToSave);
+                SaveAsPng(info.FilePath, info.FileHeight, info.FileWidth, bitmap);
             }
         }
 
@@ -48,12 +48,12 @@ namespace PixiEditor.Models.IO
         /// Saves file with info that has been recieved from ExportFileDialog before, doesn't work without before Export() usage.
         /// </summary>
         /// <param name="type">Type of file</param>
-        /// <param name="imageToSave">Image to be saved as file.</param>
-        public static void ExportWithoutDialog(FileType type, Image imageToSave)
+        /// <param name="bitmap">Image to be saved as file.</param>
+        public static void ExportWithoutDialog(FileType type, WriteableBitmap bitmap)
         {
             try
             {
-                SaveAsPng(SavePath, (int)imageToSave.Width, (int)imageToSave.Height, (int)FileDimensions.Height, (int)FileDimensions.Width, imageToSave);
+                SaveAsPng(SavePath, (int)FileDimensions.Height, (int)FileDimensions.Width, bitmap);
             }
             catch (Exception ex)
             {
@@ -64,39 +64,19 @@ namespace PixiEditor.Models.IO
         /// Saves image to PNG file
         /// </summary>
         /// <param name="savePath">Save file path</param>
-        /// <param name="originalWidth">Original width of image</param>
-        /// <param name="originalHeight">Original height of image</param>
         /// <param name="exportWidth">File width</param>
         /// <param name="exportHeight">File height</param>
-        /// <param name="imageToExport">Image to be saved</param>
-        private static void SaveAsPng(string savePath, int originalWidth, int originalHeight, int exportWidth, int exportHeight, Image imageToExport)
+        private static void SaveAsPng(string savePath, int exportWidth, int exportHeight, WriteableBitmap bitmap)
         {
-            Rect bounds = VisualTreeHelper.GetDescendantBounds(imageToExport);
-            double dpi = 96d;
-
-
-            RenderTargetBitmap rtb = new RenderTargetBitmap(originalWidth * (exportWidth / originalWidth), originalHeight * (exportHeight / originalHeight), dpi, dpi, PixelFormats.Default);
-
-
-            DrawingVisual dv = new DrawingVisual();
-            using (DrawingContext dc = dv.RenderOpen())
-            {
-                VisualBrush vb = new VisualBrush(imageToExport);
-                dc.DrawRectangle(vb, null, new Rect(new Point(), new Size(originalWidth * (exportWidth / originalWidth), originalHeight * (exportHeight / originalHeight))));
-            }
-
-            rtb.Render(dv);
-            BitmapEncoder pngEncoder = new PngBitmapEncoder();
-            pngEncoder.Frames.Add(BitmapFrame.Create(rtb));
-
             try
             {
-                MemoryStream ms = new MemoryStream();
-
-                pngEncoder.Save(ms);
-                ms.Close();
-
-                File.WriteAllBytes(savePath, ms.ToArray());
+                bitmap = bitmap.Resize(exportWidth, exportHeight, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
+                using(FileStream stream = new FileStream(savePath, FileMode.Create))
+                {
+                    PngBitmapEncoder encoder = new PngBitmapEncoder();
+                    encoder.Frames.Add(BitmapFrame.Create(bitmap));
+                    encoder.Save(stream);
+                }
             }
             catch (Exception err)
             {

+ 9 - 9
PixiEditor/ViewModels/ViewModelMain.cs

@@ -291,15 +291,15 @@ namespace PixiEditor.ViewModels
         /// <param name="parameter"></param>
         private void SaveFile(object parameter)
         {
-            //TODO: Blend bitmaps and save file
-        //    if (Exporter.SavePath == null)
-        //    {
-        //        Exporter.Export(FileType.PNG, ActiveImage, new Size(BitmapUtility.ActiveLayer.Width, BitmapUtility.ActiveLayer.Height));
-        //    }
-        //    else
-        //    {
-        //        Exporter.ExportWithoutDialog(FileType.PNG, ActiveImage);
-        //    }
+            WriteableBitmap bitmap = BitmapUtility.GetCombinedLayersBitmap();
+            if (Exporter.SavePath == null)
+            {
+                Exporter.Export(FileType.PNG, bitmap, new Size(bitmap.PixelWidth, bitmap.PixelHeight));
+            }
+            else
+            {
+                Exporter.ExportWithoutDialog(FileType.PNG, bitmap);
+            }
         }
         /// <summary>
         /// Returns true if file save is possible.