public static IImageProcessingContext ApplyRoundedCorners(this IImageProcessingContext ctx, float cornerRadius) { Size size = ctx.GetCurrentSize(); IPathCollection corners = BuildCorners(size.Width, size.Height, cornerRadius); ctx.SetGraphicsOptions(new GraphicsOptions() { Antialias = true, AlphaCompositionMode = PixelAlphaCompositionMode.DestOut }); return(ctx.Fill(Color.Red, corners)); }
// https://github.com/SixLabors/Samples/blob/master/ImageSharp/AvatarWithRoundedCorner/Program.cs public static IImageProcessingContext ApplyRoundedCorners(this IImageProcessingContext ctx, float cornerRadius) { Size size = ctx.GetCurrentSize(); IPathCollection corners = BuildCorners(size.Width, size.Height, cornerRadius); ctx.SetGraphicsOptions(new GraphicsOptions() { Antialias = true, AlphaCompositionMode = PixelAlphaCompositionMode.DestOut // enforces that any part of this shape that has color is punched out of the background }); foreach (var c in corners) { ctx = ctx.Fill(SixLabors.ImageSharp.Color.Red, c); } return(ctx); }
// This method can be seen as an inline implementation of an `IImageProcessor`: // (The combination of `IImageOperations.Apply()` + this could be replaced with an `IImageProcessor`) private static IImageProcessingContext ApplyRoundedCorners(this IImageProcessingContext ctx, float cornerRadius) { Size size = ctx.GetCurrentSize(); IPathCollection corners = BuildCorners(size.Width, size.Height, cornerRadius); ctx.SetGraphicsOptions(new GraphicsOptions() { Antialias = true, AlphaCompositionMode = PixelAlphaCompositionMode.DestOut // enforces that any part of this shape that has color is punched out of the background }); // mutating in here as we already have a cloned original // use any color (not Transparent), so the corners will be clipped foreach (var c in corners) { ctx = ctx.Fill(Color.Red, c); } return(ctx); }