From d089ffd43efa8041faff4c4bbd9911e43b5082d8 Mon Sep 17 00:00:00 2001 From: JoaoCarlosBessa Date: Tue, 11 Mar 2025 15:34:55 -0300 Subject: [PATCH 1/2] Update GeometryType.cs --- CropperImage.MAUI/GeometryType.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CropperImage.MAUI/GeometryType.cs b/CropperImage.MAUI/GeometryType.cs index 1d03134..a70a1b9 100644 --- a/CropperImage.MAUI/GeometryType.cs +++ b/CropperImage.MAUI/GeometryType.cs @@ -3,5 +3,6 @@ public enum GeometryType { Circle, - Square + Square, + Rectangle } From 8cc37aeef7479d0d9c552b229ae928ecb249aede Mon Sep 17 00:00:00 2001 From: JoaoCarlosBessa Date: Tue, 11 Mar 2025 16:20:31 -0300 Subject: [PATCH 2/2] Update CropperImageView.xaml.cs --- CropperImage.MAUI/CropperImageView.xaml.cs | 95 +++++++++++++++++----- 1 file changed, 73 insertions(+), 22 deletions(-) diff --git a/CropperImage.MAUI/CropperImageView.xaml.cs b/CropperImage.MAUI/CropperImageView.xaml.cs index d87e5f3..ba9e9a7 100644 --- a/CropperImage.MAUI/CropperImageView.xaml.cs +++ b/CropperImage.MAUI/CropperImageView.xaml.cs @@ -720,32 +720,63 @@ private void SkCanvas_PaintSurface(object sender, SKPaintSurfaceEventArgs e) if (cropping) { canvas.Clear(); - if (CropperRadiusPer > 0) + float CropRadius = Math.Min(e.Info.Width, e.Info.Height) /2 * CropperRadiusPer / 100f; + float cropX, cropY, cropWidth, cropHeight; + + if (CropperFigure == GeometryType.Circle) { - float CropRadius = Math.Min(e.Info.Width, e.Info.Height) / 2 * CropperRadiusPer / 100f; - if (!cropRect && CropperFigure == GeometryType.Circle) - canvas.ClipRoundRect(new SKRoundRect(SKRect.Create(e.Info.Width / 2 - CropRadius, e.Info.Height / 2 - CropRadius, CropRadius * 2, CropRadius * 2), CropRadius * 2)); - var pictureFrameCrop = SKRect.Create(imageX, imageY, e.Info.Width * imageScale, e.Info.Height * imageScale); - var destCrop = pictureFrameCrop.AspectFit(new SKSize(image.Width, image.Height)); - canvas.DrawImage(image, destCrop); - var snap = e.Surface.Snapshot(SKRectI.Create((int)(e.Info.Width / 2 - CropRadius), (int)(e.Info.Height / 2 - CropRadius), (int)(CropRadius * 2), (int)(CropRadius * 2))); - croppedImage = snap; - imageData = snap.Encode().ToArray(); + cropX = e.Info.Width / 2 - CropRadius; + cropY = e.Info.Height / 2 - CropRadius; + cropWidth = CropRadius * 2; + cropHeight = CropRadius * 2; + + //I didn't understand the reason to put this here but I left it here anyway + if (!cropRect) + { + canvas.ClipRoundRect(new SKRoundRect(SKRect.Create(cropX, cropY, cropWidth, cropHeight), CropRadius * 2)); + } } - else + else if (CropperFigure == GeometryType.Square) { - var pictureFrameCrop = SKRect.Create(imageX, imageY, e.Info.Width * imageScale, e.Info.Height * imageScale); - var destCrop = pictureFrameCrop.AspectFit(new SKSize(image.Width, image.Height)); - canvas.DrawImage(image, destCrop); - var snap = e.Surface.Snapshot(); - croppedImage = snap; - imageData = snap.Encode().ToArray(); + cropX = e.Info.Width / 2 - CropRadius; + cropY = e.Info.Height / 2 - CropRadius; + cropWidth = CropRadius * 2; + cropHeight = CropRadius * 2; + + canvas.ClipRect(SKRect.Create(cropX, cropY, cropWidth, cropHeight)); } - if (EditMode) - canvas.Clear(BackgroundColor != null ? BackgroundColor.ToSKColor() : SKColors.White); else - canvas.Clear(); + { + //Rectangle crop + //4:3 aspectRatio + float aspectRatio = 4f / 3f; + + // I didn't think about having a way to let people change this + cropWidth = e.Info.Width * 0.9f; // adjust width + cropHeight = (cropWidth / aspectRatio) * 0.8f // adjust height + + //Just to ensure it fits within bounds + if (cropHeight > e.Info.Height) + { + cropHeight = e.Info.Width * 0.7f; + cropWidth = cropHeight * aspectRatio; + } + + cropX = (e.Info.Width - cropWidth) / 2; + cropY = (e.Info.Height - cropHeight) / 2; + + canvas.ClipRect(SKRect.Create(cropX, cropY, cropWidth, cropHeight)); + } + + var pictureFrameCrop = SKRect.Create(imageX, imageY, e.Info.Width * imageScale, e.Info.Height * imageScale); + var destCrop = pictureFrameCrop.AspectFit(new SKSize(image.Width, image.Height)); + canvas.DrawImage (image, destCrop); + + var snap = e.Surface.Snapshot(SKRectI.Create((int)cropX, (int)cropY, (int)cropWidth, (int)cropHeight)); + croppedImage = snap; + imageData = snap.Encode().ToArray(); } + var pictureFrame = SKRect.Create(imageX, imageY, e.Info.Width * imageScale, e.Info.Height * imageScale); var dest = pictureFrame.AspectFit(new SKSize(image.Width, image.Height)); canvas.DrawImage(image, dest); @@ -765,8 +796,28 @@ private void SkCanvas_PaintSurface(object sender, SKPaintSurfaceEventArgs e) maskPath.Close(); if (CropperFigure == GeometryType.Circle) canvas.ClipRoundRect(new SKRoundRect(SKRect.Create(e.Info.Width / 2 - CropRadius, e.Info.Height / 2 - CropRadius, CropRadius * 2, CropRadius * 2), CropRadius * 2), SKClipOperation.Difference); - else + else if (CropperFigure == GeometryType.Square) canvas.ClipRect(SKRect.Create(e.Info.Width / 2 - CropRadius, e.Info.Height / 2 - CropRadius, CropRadius * 2, CropRadius * 2), SKClipOperation.Difference); + else + { + //Rectangle outline + float aspectRatio = 4f / 3f; + + float cropWidth = e.Info.Width * 0.9f; //Adjust width + float cropHeight = (cropWidth / aspectRatio) * 0.8f; + + //Just to ensure it fits within the canvas + if (cropHeight > e.Info.Height) + { + cropHeight = e.Info.Height * 0.7f; + cropWidth = cropHeight * aspectRatio; + } + + float cropX = (e.Info.Width - cropWidth) / 2; + float cropY - (e,Info.Height - cropHeight) / 2; + + canvas.ClipRect(SKRect.Create(cropX, cropY, cropWidth, cropHeight), SKClipOperation.Difference); + } canvas.DrawPaint(cropPaint); } } @@ -785,4 +836,4 @@ private static void DefineDefaultStyles() STYLESINITIATED = true; } } -} \ No newline at end of file +}