xamarin-docs
xamarin-docs copied to clipboard
Can we have a page detailing the cropping technique along with this one that is about the user interface?
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.
- ID: c2103650-966c-ac1f-1422-db102483444e
- Version Independent ID: cf19f39c-5fe6-0fe5-653c-a747579886fa
- Content: Cropping SkiaSharp bitmaps - Xamarin
- Content Source: docs/xamarin-forms/user-interface/graphics/skiasharp/bitmaps/cropping.md
- Product: xamarin
- Technology: xamarin-skiasharp
- GitHub Login: @davidbritch
- Microsoft Alias: dabritch