Skip to content

Commit f59bc5a

Browse files
committed
Refactor and enhance cropping and shape detection logic
Refactored cropping rectangle adjustments for better scaling and bounds handling. Improved type safety and clarity in `DetectShapeButton_Click`. Enhanced error handling and user feedback. Added namespaces for file handling and helper utilities. Cleaned up rotate adorner logic and ensured proper resource management. General code cleanup for readability and maintainability.
1 parent 8e6f76d commit f59bc5a

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

MagickCrop/Helpers/QuadrilateralDetector.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Emgu.CV.Structure;
44
using Emgu.CV.Util;
55
using System.Drawing;
6+
using System.IO;
67
using System.Windows;
78

89
namespace MagickCrop.Helpers;

MagickCrop/MainWindow.xaml.cs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ImageMagick;
22
using MagickCrop.Controls;
3+
using MagickCrop.Helpers;
34
using MagickCrop.Models;
45
using MagickCrop.Models.MeasurementControls;
56
using MagickCrop.Services;
@@ -548,7 +549,7 @@ private async void ApplyButton_Click(object sender, RoutedEventArgs e)
548549
Size originalDisplaySize = new(MainImage.ActualWidth, MainImage.ActualHeight);
549550
bool cropRectangleVisible = CroppingRectangle.Visibility == Visibility.Visible;
550551
double originalCropLeft = 0, originalCropTop = 0, originalCropWidth = 0, originalCropHeight = 0;
551-
552+
552553
if (cropRectangleVisible)
553554
{
554555
originalCropLeft = Canvas.GetLeft(CroppingRectangle);
@@ -576,28 +577,28 @@ private async void ApplyButton_Click(object sender, RoutedEventArgs e)
576577
{
577578
// Force layout update to ensure MainImage has updated its ActualWidth/Height
578579
UpdateLayout();
579-
580+
580581
Size newDisplaySize = new(MainImage.ActualWidth, MainImage.ActualHeight);
581-
582-
if (newDisplaySize.Width > 0 && newDisplaySize.Height > 0 &&
582+
583+
if (newDisplaySize.Width > 0 && newDisplaySize.Height > 0 &&
583584
originalDisplaySize.Width > 0 && originalDisplaySize.Height > 0)
584585
{
585586
// Calculate scale factors for the display size change
586587
double widthScale = newDisplaySize.Width / originalDisplaySize.Width;
587588
double heightScale = newDisplaySize.Height / originalDisplaySize.Height;
588-
589+
589590
// Transform the crop rectangle position and size
590591
double newCropLeft = originalCropLeft * widthScale;
591592
double newCropTop = originalCropTop * heightScale;
592593
double newCropWidth = originalCropWidth * widthScale;
593594
double newCropHeight = originalCropHeight * heightScale;
594-
595+
595596
// Ensure the adjusted rectangle stays within bounds
596597
newCropLeft = Math.Max(0, Math.Min(newCropLeft, newDisplaySize.Width - newCropWidth));
597598
newCropTop = Math.Max(0, Math.Min(newCropTop, newDisplaySize.Height - newCropHeight));
598599
newCropWidth = Math.Min(newCropWidth, newDisplaySize.Width - newCropLeft);
599600
newCropHeight = Math.Min(newCropHeight, newDisplaySize.Height - newCropTop);
600-
601+
601602
// Apply the adjusted position and size
602603
Canvas.SetLeft(CroppingRectangle, newCropLeft);
603604
Canvas.SetTop(CroppingRectangle, newCropTop);
@@ -1813,7 +1814,7 @@ private async void DetectShapeButton_Click(object sender, RoutedEventArgs e)
18131814
{
18141815
if (string.IsNullOrEmpty(imagePath) || !File.Exists(imagePath))
18151816
{
1816-
_ = System.Windows.MessageBox.Show("Please open an image first.", "No Image", MessageBoxButton.OK, MessageBoxImage.Warning);
1817+
_ = System.Windows.MessageBox.Show("Please open an image first.", "No Image", System.Windows.MessageBoxButton.OK, MessageBoxImage.Warning);
18171818
return;
18181819
}
18191820

@@ -1823,27 +1824,27 @@ private async void DetectShapeButton_Click(object sender, RoutedEventArgs e)
18231824
try
18241825
{
18251826
// Detect quadrilaterals in background thread
1826-
var detectionResult = await Task.Run(() =>
1827-
Helpers.QuadrilateralDetector.DetectQuadrilateralsWithDimensions(imagePath, minArea: QuadDetectionMinArea, maxResults: QuadDetectionMaxResults));
1827+
QuadrilateralDetector.DetectionResult detectionResult = await Task.Run(() =>
1828+
QuadrilateralDetector.DetectQuadrilateralsWithDimensions(imagePath, minArea: QuadDetectionMinArea, maxResults: QuadDetectionMaxResults));
18281829

18291830
if (detectionResult.Quadrilaterals.Count == 0)
18301831
{
18311832
_ = System.Windows.MessageBox.Show(
1832-
"No quadrilaterals detected in the image.\n\nPlease position the corner markers manually.",
1833-
"No Shapes Detected",
1834-
MessageBoxButton.OK,
1833+
"No quadrilaterals detected in the image.\n\nPlease position the corner markers manually.",
1834+
"No Shapes Detected",
1835+
System.Windows.MessageBoxButton.OK,
18351836
MessageBoxImage.Information);
18361837
}
18371838
else
18381839
{
18391840
// Scale quadrilaterals to display coordinates
1840-
var scaledQuads = detectionResult.Quadrilaterals.Select(q =>
1841-
Helpers.QuadrilateralDetector.ScaleToDisplay(
1841+
List<QuadrilateralDetector.DetectedQuadrilateral> scaledQuads = [.. detectionResult.Quadrilaterals.Select(q =>
1842+
QuadrilateralDetector.ScaleToDisplay(
18421843
q,
18431844
detectionResult.ImageWidth,
18441845
detectionResult.ImageHeight,
18451846
MainImage.ActualWidth,
1846-
MainImage.ActualHeight)).ToList();
1847+
MainImage.ActualHeight))];
18471848

18481849
// Show selector
18491850
QuadrilateralSelectorControl.SetQuadrilaterals(scaledQuads);
@@ -1853,9 +1854,9 @@ private async void DetectShapeButton_Click(object sender, RoutedEventArgs e)
18531854
catch (Exception ex)
18541855
{
18551856
_ = System.Windows.MessageBox.Show(
1856-
$"Error detecting quadrilaterals: {ex.Message}",
1857-
"Detection Error",
1858-
MessageBoxButton.OK,
1857+
$"Error detecting quadrilaterals: {ex.Message}",
1858+
"Detection Error",
1859+
System.Windows.MessageBoxButton.OK,
18591860
MessageBoxImage.Error);
18601861
}
18611862
finally
@@ -3465,7 +3466,7 @@ private void ToggleRotateMode(bool enable)
34653466
{
34663467
return;
34673468
}
3468-
3469+
34693470
try
34703471
{
34713472
rotateAdornerLayer ??= AdornerLayer.GetAdornerLayer(ImageGrid);
@@ -3660,7 +3661,7 @@ private void RemoveRotateAdorner()
36603661
if (rotateAdornerLayer != null && rotateAdorner != null)
36613662
{
36623663
rotateAdorner.AngleChanging -= RotateAdorner_AngleChanging;
3663-
rotateAdorner.AngleChangedFinal -= RotateAdorner_AngleChangedFinal;
3664+
rotateAdorner.AngleChangedFinal -= RotateAdorner_AngleChangedFinal;
36643665
rotateAdornerLayer.Remove(rotateAdorner);
36653666
rotateAdorner = null;
36663667
}

0 commit comments

Comments
 (0)