xamarin-docs icon indicating copy to clipboard operation
xamarin-docs copied to clipboard

Can we have a page detailing the cropping technique along with this one that is about the user interface?

Open omatrot opened this issue 3 years ago • 0 comments

I'm trying to write backend code to crop a bitmap. So far, my code is not working. I've found this article confusing about the necessary steps to crop away from any interface.

For instance, I have this code that is not working and I do not know why...

        private string ConvertSVGToPNGRectangleWithSkiaSharpExtended(string path, double left, double top, double right, double bottom)
        {
            var svg = new SkiaSharp.Extended.Svg.SKSvg();
            var picture = svg.Load(path);

            // Get the initial map size
            var sourceRect = new SKRect(picture.CullRect.Left, picture.CullRect.Top, picture.CullRect.Width, picture.CullRect.Height);

            // Croppng Rect
            var cropRect = new SKRect((int)left, (int)top, (int)right, (int)bottom);

            var croppedBitmap = new SKBitmap((int)cropRect.Width, (int)cropRect.Height);

            using var canvas = new SKCanvas(croppedBitmap);
            canvas.Clear(SKColors.Transparent);
            canvas.DrawBitmap(croppedBitmap, sourceRect, cropRect);

            var data = croppedBitmap.Encode(SKEncodedImageFormat.Png, 100);

            path = Path.ChangeExtension(path, "PNG");
            using var imageStream = new FileStream(path, FileMode.Create);
            data.SaveTo(imageStream);

            return path;
        }

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

omatrot avatar Aug 25 '22 13:08 omatrot