Skip to content

Commit 64fd5ec

Browse files
committed
Working cropping method
1 parent 9c4951a commit 64fd5ec

File tree

5 files changed

+74
-13
lines changed

5 files changed

+74
-13
lines changed

PicView/Editing/ImageCropping.cs

+53-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ internal static class ImageCropping
1616
{
1717
internal static void StartCrop()
1818
{
19+
if (mainWindow.img.Source == null) { return; }
20+
1921
if (cropppingTool == null)
2022
{
2123
LoadControls.LoadCroppingTool();
@@ -30,7 +32,7 @@ internal static void StartCrop()
3032
)));
3133
}
3234

33-
if (mainWindow.img.Source == null) { return; }
35+
mainWindow.Bar.Text = "Press Esc to close, Enter to save";
3436

3537
if (!mainWindow.bg.Children.Contains(cropppingTool))
3638
{
@@ -48,9 +50,19 @@ internal static void StartCrop()
4850
1);
4951

5052
cropppingTool.CropTool.SetImage(i);
51-
cropppingTool.CropTool.SetSize(mainWindow.img.Source.Width, mainWindow.img.Source.Height);
52-
cropppingTool.CropTool.Width = xWidth;
53-
cropppingTool.CropTool.Height = xHeight;
53+
if (Rotateint == 0 || Rotateint == 180)
54+
{
55+
cropppingTool.CropTool.SetSize(mainWindow.img.Source.Width, mainWindow.img.Source.Height);
56+
cropppingTool.CropTool.Width = xWidth;
57+
cropppingTool.CropTool.Height = xHeight;
58+
}
59+
else
60+
{
61+
cropppingTool.CropTool.SetSize(mainWindow.img.Source.Height, mainWindow.img.Source.Width);
62+
cropppingTool.CropTool.Width = xHeight;
63+
cropppingTool.CropTool.Height = xWidth;
64+
}
65+
5466
}
5567

5668
internal static async void SaveCrop()
@@ -83,11 +95,43 @@ internal static Int32Rect GetCrop()
8395
{
8496
var cropArea = cropppingTool.CropTool.CropService.GetCroppedArea();
8597

86-
// Incorrect coordinates calculated if image resized to fit app, help!
87-
var x = Convert.ToInt32(cropArea.CroppedRectAbsolute.X);
88-
var y = Convert.ToInt32(cropArea.CroppedRectAbsolute.Y);
89-
var width = Convert.ToInt32(cropArea.CroppedRectAbsolute.Width);
90-
var height = Convert.ToInt32(cropArea.CroppedRectAbsolute.Height);
98+
int x, y, width, height;
99+
100+
if (AspectRatio != 0)
101+
{
102+
if (Rotateint == 0 || Rotateint == 180)
103+
{
104+
x = Convert.ToInt32(cropArea.CroppedRectAbsolute.X / AspectRatio);
105+
y = Convert.ToInt32(cropArea.CroppedRectAbsolute.Y / AspectRatio);
106+
width = Convert.ToInt32(cropArea.CroppedRectAbsolute.Width / AspectRatio);
107+
height = Convert.ToInt32(cropArea.CroppedRectAbsolute.Height / AspectRatio);
108+
}
109+
else
110+
{
111+
x = Convert.ToInt32(cropArea.CroppedRectAbsolute.Y / AspectRatio);
112+
y = Convert.ToInt32(cropArea.CroppedRectAbsolute.X / AspectRatio);
113+
width = Convert.ToInt32(cropArea.CroppedRectAbsolute.Height / AspectRatio);
114+
height = Convert.ToInt32(cropArea.CroppedRectAbsolute.Width / AspectRatio);
115+
}
116+
117+
}
118+
else
119+
{
120+
if (Rotateint == 0 || Rotateint == 180)
121+
{
122+
x = Convert.ToInt32(cropArea.CroppedRectAbsolute.X);
123+
y = Convert.ToInt32(cropArea.CroppedRectAbsolute.Y);
124+
width = Convert.ToInt32(cropArea.CroppedRectAbsolute.Width);
125+
height = Convert.ToInt32(cropArea.CroppedRectAbsolute.Height);
126+
}
127+
else
128+
{
129+
x = Convert.ToInt32(cropArea.CroppedRectAbsolute.Y);
130+
y = Convert.ToInt32(cropArea.CroppedRectAbsolute.X);
131+
width = Convert.ToInt32(cropArea.CroppedRectAbsolute.Height);
132+
height = Convert.ToInt32(cropArea.CroppedRectAbsolute.Width);
133+
}
134+
}
91135

92136
return new Int32Rect(x, y, width, height);
93137
}

PicView/Image encoding and decoding/SaveImages.cs

+5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ internal static bool TrySaveImage(Int32Rect rect, string path, string destinatio
6565

6666
SaveImage.Read(path);
6767

68+
if (Fields.Rotateint != 0)
69+
{
70+
SaveImage.Rotate(Fields.Rotateint);
71+
}
72+
6873
SaveImage.Crop(new MagickGeometry(rect.X, rect.Y, rect.Width, rect.Height));
6974

7075
SaveImage.Write(destination);

PicView/Shortcuts/Shortcuts.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ internal static void MainWindow_KeysDown(object sender, KeyEventArgs e)
3737
{
3838
if (e.Key == Key.Escape)
3939
{
40+
SetTitle.SetTitleString((int)mainWindow.img.Source.Width, (int)mainWindow.img.Source.Height, FolderIndex);
4041
mainWindow.bg.Children.Remove(cropppingTool);
4142
return;
4243
}
4344

4445
if (e.Key == Key.Enter)
4546
{
47+
SetTitle.SetTitleString((int)mainWindow.img.Source.Width, (int)mainWindow.img.Source.Height, FolderIndex);
4648
ImageCropping.SaveCrop();
4749
mainWindow.bg.Children.Remove(cropppingTool);
4850
}
@@ -373,9 +375,7 @@ internal static void MainWindow_KeysDown(object sender, KeyEventArgs e)
373375
}
374376
else
375377
{
376-
#if DEBUG
377378
ImageCropping.StartCrop();
378-
#endif
379379
}
380380
break;
381381

PicView/UserControls/Menus/ToolsAndEffectsMenu.xaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@
137137
Height="51"
138138
Style="{StaticResource SexyBorder}">
139139
<Border.Background>
140-
<SolidColorBrush x:Name="CopyButtonBrush" Color="{StaticResource BackgroundColorAlt}" />
140+
<SolidColorBrush x:Name="CropButtonBrush" Color="{StaticResource BackgroundColorAlt}" />
141141
</Border.Background>
142142
<Button
143143
x:Name="CropButton"
144144
FontWeight="Bold"
145-
ToolTip="Crop image [C] (not implemented yet)">
145+
ToolTip="Crop image [C] (Rotation not yet working)">
146146
<Canvas>
147147
<TextBlock
148148
Canvas.Left="-59"

PicView/UserControls/Menus/ToolsAndEffectsMenu.xaml.cs

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public ToolsAndEffectsMenu()
1212
{
1313
InitializeComponent();
1414

15+
// ResizeButton
1516
ResizeButton.PreviewMouseLeftButtonDown += (s, x) => PreviewMouseButtonDownAnim(ResizeButtonBrush);
1617
ResizeButton.MouseEnter += (s, x) => ButtonMouseOverAnim(ResizeButtonBrush, true);
1718
ResizeButton.MouseLeave += (s, x) => ButtonMouseLeaveAnimBgColor(ResizeButtonBrush, false);
@@ -22,6 +23,7 @@ public ToolsAndEffectsMenu()
2223
Batch_Resize.UpdateValues();
2324
};
2425

26+
// EffectsButton
2527
EffectsButton.PreviewMouseLeftButtonDown += (s, x) => PreviewMouseButtonDownAnim(EffectsButtonBrush);
2628
EffectsButton.MouseEnter += (s, x) => ButtonMouseOverAnim(EffectsButtonBrush, true);
2729
EffectsButton.MouseLeave += (s, x) => ButtonMouseLeaveAnimBgColor(EffectsButtonBrush, false);
@@ -31,6 +33,16 @@ public ToolsAndEffectsMenu()
3133
LoadWindows.EffectsWindow();
3234
};
3335

36+
// CropButton
37+
CropButton.PreviewMouseLeftButtonDown += (s, x) => PreviewMouseButtonDownAnim(CropButtonBrush);
38+
CropButton.MouseEnter += (s, x) => ButtonMouseOverAnim(CropButtonBrush, true);
39+
CropButton.MouseLeave += (s, x) => ButtonMouseLeaveAnimBgColor(CropButtonBrush, false);
40+
CropButton.Click += delegate
41+
{
42+
UC.Close_UserControls();
43+
ImageCropping.StartCrop();
44+
};
45+
3446
}
3547
}
3648
}

0 commit comments

Comments
 (0)