Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using SixLabors.ImageSharp;
- using SixLabors.ImageSharp.PixelFormats;
- using SixLabors.ImageSharp.Processing;
- using SixLabors.Primitives;
- using SixLabors.Shapes;
- namespace EQL.ImageSharp
- {
- public static class RoundCornersProcessor
- {
- public static IImageProcessingContext ApplyRoundedCorners(this IImageProcessingContext ctx, float cornerPercentX, float cornerPercentY)
- {
- Size size = ctx.GetCurrentSize();
- IPathCollection corners = BuildCorners(size.Width, size.Height, cornerPercentX, cornerPercentY);
- var graphicOptions = new GraphicsOptions(true)
- {
- AlphaCompositionMode = PixelAlphaCompositionMode.DestOut
- };
- return ctx.Fill(graphicOptions, Rgba32.Black, corners);
- }
- private static IPathCollection BuildCorners(int imageWidth, int imageHeight, float cornerPercentX, float cornerPercentY)
- {
- var cornerRadiusX = imageWidth * (cornerPercentX / 100.0f);
- var cornerRadiusY = imageHeight * (cornerPercentY / 100.0f);
- var rightOffset = imageWidth - cornerRadiusX;
- var bottomOffset = imageHeight - cornerRadiusY;
- var rect = new RectangularPolygon(0, 0, cornerRadiusX, cornerRadiusY);
- IPath cornerTopLeft = rect.Clip(new EllipsePolygon(cornerRadiusX, cornerRadiusY, cornerRadiusX * 2, cornerRadiusY * 2));
- IPath cornerTopRight = rect.Clip(new EllipsePolygon(0, cornerRadiusY, cornerRadiusX * 2, cornerRadiusY * 2)).Translate(rightOffset, 0);
- IPath cornerBottomLeft = rect.Clip(new EllipsePolygon(cornerRadiusX, 0, cornerRadiusX * 2, cornerRadiusY * 2)).Translate(0, bottomOffset);
- IPath cornerBottomRight = rect.Clip(new EllipsePolygon(0, 0, cornerRadiusX * 2, cornerRadiusY * 2)).Translate(rightOffset, bottomOffset);
- return new PathCollection(cornerTopLeft, cornerTopRight, cornerBottomLeft, cornerBottomRight);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement