微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xamarin SkiaSharp 缩放裁剪画布

如何解决Xamarin SkiaSharp 缩放裁剪画布

我尝试从我在 Xamarin Foms 中的应用程序中捕获的照片中获得多边形裁剪。 我使用 SkiaSharp 。所以第一步是好的,我像这样从我的 EdgeCroppingView 得到一个画布

enter image description here

所以现在我想在全屏中裁剪选择并“调整大小”,只有选择(围绕猫),我有这个:

enter image description here

The selected skpath (the borders) are correctly "resizing" but not the inside content(the cat's area) : i would like to strech this area ...

有什么想法吗?

 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 );
            }

          
        }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。