浏览代码

Crop - SelectionWidth & SelectionHeight

Ruben 10 月之前
父节点
当前提交
25b325e436

+ 33 - 9
src/PicView.Avalonia/ViewModels/ImageCropperViewModel.cs

@@ -58,24 +58,50 @@ public class ImageCropperViewModel : ViewModelBase
     public double SelectionWidth
     {
         get;
-        set => this.RaiseAndSetIfChanged(ref field, value);
+        set
+        {
+            this.RaiseAndSetIfChanged(ref field, value);
+            PixelSelectionWidth = Convert.ToUInt32(SelectionWidth / AspectRatio);
+        }
     } = 100;
+    
+    public uint PixelSelectionWidth
+    {
+        get
+        {
+            return Convert.ToUInt32(SelectionWidth / AspectRatio);
+        }
+        set => this.RaiseAndSetIfChanged(ref field, value);
+    }
 
     public double SelectionHeight
     {
         get;
-        set => this.RaiseAndSetIfChanged(ref field, value);
+        set
+        {
+            this.RaiseAndSetIfChanged(ref field, value);
+            PixelSelectionHeight = Convert.ToUInt32(SelectionHeight / AspectRatio);
+        } 
     } = 100;
+
+    public uint PixelSelectionHeight
+    {
+        get
+        {
+            return Convert.ToUInt32(SelectionHeight / AspectRatio);
+        }
+        set => this.RaiseAndSetIfChanged(ref field, value);
+    }
     
     public double ImageWidth
     {
         get;
-        set => this.RaiseAndSetIfChanged(ref field, value);
+        init => this.RaiseAndSetIfChanged(ref field, value);
     }
     public double ImageHeight
     {
         get;
-        set => this.RaiseAndSetIfChanged(ref field, value);
+        init => this.RaiseAndSetIfChanged(ref field, value);
     }
     
     public double AspectRatio
@@ -126,8 +152,8 @@ public class ImageCropperViewModel : ViewModelBase
         var fileName = $"{TranslationHelper.Translation.Crop} {new Random().Next(9999)}.png";
         var x = Convert.ToInt32(SelectionX / AspectRatio);
         var y = Convert.ToInt32(SelectionY / AspectRatio);
-        var width = Convert.ToInt32(SelectionWidth / AspectRatio);
-        var height = Convert.ToInt32(SelectionHeight / AspectRatio);
+        var width = (int)PixelSelectionWidth;
+        var height = (int)PixelSelectionHeight;
         var croppedBitmap = new CroppedBitmap(Bitmap, new PixelRect(x, y, width, height));
         var bitmap = ConvertCroppedBitmapToBitmap(croppedBitmap);
         return (fileName, new FileInfo(fileName), bitmap);
@@ -149,9 +175,7 @@ public class ImageCropperViewModel : ViewModelBase
         using var image = new MagickImage(fileInfo.FullName);
         var x = Convert.ToInt32(SelectionX / AspectRatio);
         var y = Convert.ToInt32(SelectionY / AspectRatio);
-        var width = Convert.ToUInt32(SelectionWidth / AspectRatio);
-        var height = Convert.ToUInt32(SelectionHeight / AspectRatio);
-        var geometry = new MagickGeometry(x, y, width, height);
+        var geometry = new MagickGeometry(x, y, PixelSelectionWidth, PixelSelectionHeight);
         
         image.Crop(geometry);
         await image.WriteAsync(saveFilePath);

+ 2 - 2
src/PicView.Avalonia/Views/UC/CropControl.axaml

@@ -65,11 +65,11 @@
                 <TextBlock Classes="txt" FontSize="14">
                     <Run Text="{CompiledBinding Width, Mode=OneWay}" />
                     <Run Text=": " />
-                    <Run Text="{CompiledBinding SelectionWidth, Mode=OneWay}" />
+                    <Run Text="{CompiledBinding PixelSelectionWidth}" />
                     <Run Text=" x " />
                     <Run Text="{CompiledBinding Height, Mode=OneWay}" />
                     <Run Text=": " />
-                    <Run Text="{CompiledBinding SelectionHeight, Mode=OneWay}" />
+                    <Run Text="{CompiledBinding PixelSelectionHeight}" />
                 </TextBlock>
             </Border>