Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. using SixLabors.ImageSharp;
  2. using SixLabors.ImageSharp.PixelFormats;
  3. using SixLabors.ImageSharp.Processing;
  4. using SixLabors.Primitives;
  5. using SixLabors.Shapes;
  6.  
  7. namespace EQL.ImageSharp
  8. {
  9. public static class RoundCornersProcessor
  10. {
  11. public static IImageProcessingContext ApplyRoundedCorners(this IImageProcessingContext ctx, float cornerPercentX, float cornerPercentY)
  12. {
  13. Size size = ctx.GetCurrentSize();
  14. IPathCollection corners = BuildCorners(size.Width, size.Height, cornerPercentX, cornerPercentY);
  15. var graphicOptions = new GraphicsOptions(true)
  16. {
  17. AlphaCompositionMode = PixelAlphaCompositionMode.DestOut
  18. };
  19. return ctx.Fill(graphicOptions, Rgba32.Black, corners);
  20. }
  21.  
  22. private static IPathCollection BuildCorners(int imageWidth, int imageHeight, float cornerPercentX, float cornerPercentY)
  23. {
  24. var cornerRadiusX = imageWidth * (cornerPercentX / 100.0f);
  25. var cornerRadiusY = imageHeight * (cornerPercentY / 100.0f);
  26.  
  27. var rightOffset = imageWidth - cornerRadiusX;
  28. var bottomOffset = imageHeight - cornerRadiusY;
  29.  
  30. var rect = new RectangularPolygon(0, 0, cornerRadiusX, cornerRadiusY);
  31. IPath cornerTopLeft = rect.Clip(new EllipsePolygon(cornerRadiusX, cornerRadiusY, cornerRadiusX * 2, cornerRadiusY * 2));
  32. IPath cornerTopRight = rect.Clip(new EllipsePolygon(0, cornerRadiusY, cornerRadiusX * 2, cornerRadiusY * 2)).Translate(rightOffset, 0);
  33. IPath cornerBottomLeft = rect.Clip(new EllipsePolygon(cornerRadiusX, 0, cornerRadiusX * 2, cornerRadiusY * 2)).Translate(0, bottomOffset);
  34. IPath cornerBottomRight = rect.Clip(new EllipsePolygon(0, 0, cornerRadiusX * 2, cornerRadiusY * 2)).Translate(rightOffset, bottomOffset);
  35. return new PathCollection(cornerTopLeft, cornerTopRight, cornerBottomLeft, cornerBottomRight);
  36. }
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement