I try to get a polygonal cropping from a photo i capture from my app in Xamarin Foms. I use SkiaSharp .So the first step is ok i get a canvas from my EdgeCroppingView like this
So now I want crop the selection and "resize" in full screen only the selection (around the cat) and i have this :
The selected skpath (the borders) are correctly "resizing" but not the inside content(the cat's area) : i would like to strech this area ...
Any idea ?
private void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs e)
{
SKImageInfo info = e.Info;
SKSurface surface = e.Surface;
SKCanvas canvas = surface.Canvas;
canvas.Clear();
if (_pathToClip != null)
{
SKRect rectSource;
_pathToClip.GetTightBounds(out rectSource);
e.Surface.Canvas.Clear();
SKRect canvasRect = SKRect.Create(e.Info.Size);
//I need to find the size of the path
SKRect pathRect = _pathToClip.TightBounds;
//I want to find the largest rectangle that can fit on my canvas maintaining the path's aspect ratio
//SkiaSharp added a builtin method for this based on code from me
SKRect drawPathRect = canvasRect.AspectFit(pathRect.Size);
//Now I need to transform the path to draw within the drawPathRect
//First translate original path to its own origin
SKMatrix firstTranslateM = SKMatrix.CreateTranslation(-pathRect.Left, -pathRect.Top);
//Next handle scaling. Since I maintained aspect ratio, I should be able to use either
//width or height to figure out scaling factor
float scalingFactor = drawPathRect.Width / pathRect.Width;
SKMatrix scaleM = SKMatrix.CreateScale(scalingFactor, scalingFactor);
//Last I need to handle translation so path is centered on canvas
SKMatrix secondTranslateM = SKMatrix.CreateTranslation(drawPathRect.Left, drawPathRect.Top);
//Now combine the translation, scaling, and translation into a single matrix by matrix multiplication/concatentation
SKMatrix transformM = SKMatrix.CreateIdentity();
SKMatrix.PostConcat(ref transformM, firstTranslateM);
SKMatrix.PostConcat(ref transformM, scaleM);
SKMatrix.PostConcat(ref transformM, secondTranslateM);
//Now apply the transform to the path
_pathToClip.Transform(transformM);
canvas.ClipPath(_pathToClip);
using (var paint = new SKPaint())
{
paint.Style = SKPaintStyle.Stroke;
paint.Color = SKColors.Magenta;
paint.StrokeWidth = 5;
canvas.DrawPath(_pathToClip, paint);
canvas.DrawBitmap(Bitmap, info.Rect, BitmapStretch.UniformToFill );
}
}
SKMatrix.CreateIdentity
,SKMatrix.CreateScale
,SKMatrix.CreateTranslation
. Am i missing something for this? Do you use the Xamarin Skiasharp?