Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 73 additions & 22 deletions CropperImage.MAUI/CropperImageView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
Expand All @@ -785,4 +836,4 @@ private static void DefineDefaultStyles()
STYLESINITIATED = true;
}
}
}
}
3 changes: 2 additions & 1 deletion CropperImage.MAUI/GeometryType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public enum GeometryType
{
Circle,
Square
Square,
Rectangle
}