Browse Source

Again some small bitmap filling improvenments

flabbet 5 years ago
parent
commit
7fd9d19647

+ 24 - 8
PixiEditorDotNetCore3/Models/Layers/Layer.cs

@@ -1,14 +1,9 @@
-using PixiEditor.Helpers;
-using PixiEditorDotNetCore3.Models.Tools;
+using PixiEditorDotNetCore3.Models.Tools;
 using System;
-using System.Collections.Generic;
 using System.ComponentModel;
-using System.Diagnostics;
-using System.Linq;
-using System.Text;
+using System.Runtime.InteropServices;
 using System.Threading.Tasks;
 using System.Windows;
-using System.Windows.Controls;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
 
@@ -47,11 +42,32 @@ namespace PixiEditorDotNetCore3.Models.Layers
         public void ApplyPixels(BitmapPixelChanges pixels, Color color)
         {
             LayerBitmap.Lock();
+
             foreach (var coords in pixels.ChangedCoordinates)
             {
-                LayerBitmap.SetPixel(Math.Clamp(coords.X, 0, Width - 1), Math.Clamp(coords.Y, 0, Height - 1), color);
+                LayerBitmap.SetPixel(Math.Clamp(coords.X, 0, Width - 1), Math.Clamp(coords.Y, 0, Height - 1),
+                    color);
             }
+
+            LayerBitmap.Unlock();
+        }
+
+       
+        public byte[] ConvertBitmapToBytes()
+        {            
+            LayerBitmap.Lock();
+            byte[] byteArray = LayerBitmap.ToByteArray();
             LayerBitmap.Unlock();
+            return byteArray;
         }
+
+        public byte[] ConvertBitmapToBytes(WriteableBitmap bitmap)
+        {
+            bitmap.Lock();
+            byte[] byteArray = bitmap.ToByteArray();
+            bitmap.Unlock();
+            return byteArray;
+        }
+
     }
 }

+ 4 - 1
PixiEditorDotNetCore3/Models/Layers/LayerGenerator.cs

@@ -28,7 +28,10 @@ namespace PixiEditorDotNetCore3.Models.Layers
         public static LightLayer GenerateWithByteArray(int width, int height)
         {
             WriteableBitmap bitmap = GenerateBitmap(width, height);
-            return new LightLayer(bitmap.ToByteArray(), height, width);
+            bitmap.Lock();
+            byte[] byteArray = bitmap.ToByteArray();
+            bitmap.Unlock();
+            return new LightLayer(byteArray, height, width);
         }
 
         /// <summary>

+ 14 - 7
PixiEditorDotNetCore3/Models/Tools/ToolsManager.cs

@@ -42,13 +42,14 @@ namespace PixiEditorDotNetCore3.Models.Tools
         private void LoopTimer_Elapsed(object sender, ElapsedEventArgs e)
         {
             Application.Current.Dispatcher.Invoke(() =>
-            {           
+            {
                 _layer.LayerBitmap.Lock();
+
                 if (_clonedBitmap != null)
                 {
-                    _layer.LayerBitmap.Clear();
-                    _layer.LayerBitmap.Blit(new Rect(new Size(_layer.Width, _layer.Height)), _clonedBitmap, new Rect(new Size(_layer.Width, _layer.Height)), WriteableBitmapExtensions.BlendMode.Additive);
-                }                
+                    RestoreLastBitmap();
+                }
+
                 BitmapPixelChanges changes = SelectedTool.Use(_layer, _startCoordinates, _color, _toolSzie);
 
                 if (!SelectedTool.ExecutesItself)
@@ -60,9 +61,15 @@ namespace PixiEditorDotNetCore3.Models.Tools
             });
         }
 
+        private void RestoreLastBitmap()
+        {
+            _layer.LayerBitmap.Clear();
+            _layer.LayerBitmap.Blit(new Rect(new Size(_layer.Width, _layer.Height)), _clonedBitmap, new Rect(new Size(_layer.Width, _layer.Height)), WriteableBitmapExtensions.BlendMode.Additive);
+        }
+
         public void SetTool(ToolType tool)
         {
-              SelectedTool = Tools.Find(x => x.ToolType == tool);
+            SelectedTool = Tools.Find(x => x.ToolType == tool);
         }
 
         public void StopExectuingTool()
@@ -82,7 +89,7 @@ namespace PixiEditorDotNetCore3.Models.Tools
         {
             if (SelectedTool.GetType().BaseType == typeof(ShapeTool))
             {
-                _clonedBitmap = _layer.LayerBitmap.Clone();                
+                _clonedBitmap = _layer.LayerBitmap.Clone();
             }
         }
 
@@ -100,7 +107,7 @@ namespace PixiEditorDotNetCore3.Models.Tools
             if (toolSize < 1)
                 return;
 
-            if(_toolRecievedData == false || (_toolRecievedData == true && SelectedTool.GetType().BaseType != typeof(ShapeTool)))
+            if (_toolRecievedData == false || (_toolRecievedData == true && SelectedTool.GetType().BaseType != typeof(ShapeTool)))
             {
                 _startCoordinates = startingCoords;
                 _layer = layer;

+ 3 - 0
PixiEditorDotNetCore3/PixiEditorDotNetCore3.csproj

@@ -5,6 +5,8 @@
     <TargetFramework>netcoreapp3.0</TargetFramework>
     <UseWPF>true</UseWPF>
     <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
+    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
+    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
   </PropertyGroup>
   <ItemGroup>
     <PackageReference Include="Expression.Blend.Sdk">
@@ -14,6 +16,7 @@
       <Version>3.5.0</Version>
     </PackageReference>
     <PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
+    <PackageReference Include="System.Drawing.Common" Version="4.6.0" />
     <PackageReference Include="WriteableBitmapEx">
       <Version>1.6.2</Version>
     </PackageReference>

+ 4 - 2
PixiEditorDotNetCore3/ViewModels/ViewModelMain.cs

@@ -69,7 +69,9 @@ namespace PixiEditor.ViewModels
             get 
             {
                 if (_activeLayer != null)
-                    return new LightLayer(_activeLayer.LayerBitmap.ToByteArray(), ActiveLayer.Height, ActiveLayer.Width);
+                    return new LightLayer(
+                        _activeLayer.ConvertBitmapToBytes(), 
+                        ActiveLayer.Height, ActiveLayer.Width);
                 else
                     return null;
             }
@@ -249,7 +251,7 @@ namespace PixiEditor.ViewModels
 
             if (SelectedTool != ToolType.ColorPicker)
             {
-                UndoManager.RecordChanges("ActiveLightLayer", new LightLayer(ActiveLayer.LayerBitmap.ToByteArray(), (int)ActiveLayer.LayerBitmap.Height, 
+                UndoManager.RecordChanges("ActiveLightLayer", new LightLayer(ActiveLayer.ConvertBitmapToBytes(), (int)ActiveLayer.LayerBitmap.Height, 
                     (int)ActiveLayer.LayerBitmap.Width), $"Used {SelectedTool.ToString()}");
                 primaryToolSet.ExecuteTool(ActiveLayer, cords, SelectedColor, ToolSize);
                 RefreshImage();