mirror of https://github.com/SixLabors/ImageSharp
9 changed files with 179 additions and 58 deletions
@ -0,0 +1,12 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<OutputType>Exe</OutputType> |
||||
|
<TargetFramework>netcoreapp1.1</TargetFramework> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\..\src\ImageSharp.Drawing\ImageSharp.Drawing.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,63 @@ |
|||||
|
|
||||
|
|
||||
|
namespace AvatarWithRoundedCorner |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Numerics; |
||||
|
using ImageSharp; |
||||
|
using SixLabors.Shapes; |
||||
|
|
||||
|
class Program |
||||
|
{ |
||||
|
static void Main(string[] args) |
||||
|
{ |
||||
|
|
||||
|
using (var image = Image.Load("fb.jpg")) |
||||
|
{ |
||||
|
image.Resize(new ImageSharp.Processing.ResizeOptions |
||||
|
{ |
||||
|
Size = new ImageSharp.Size(200, 200), |
||||
|
Mode = ImageSharp.Processing.ResizeMode.Crop |
||||
|
}); |
||||
|
|
||||
|
ApplyRoundedCourners(image, 30); |
||||
|
System.IO.Directory.CreateDirectory("output"); |
||||
|
image.Save("output/fb.png"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static void ApplyRoundedCourners(Image<Rgba32> img, float cornerRadius) |
||||
|
{ |
||||
|
var corners = BuildCorners(img.Width, img.Height, cornerRadius); |
||||
|
// now we have our corners time to draw them
|
||||
|
img.Fill(Rgba32.Transparent, corners, new GraphicsOptions(true) |
||||
|
{ |
||||
|
BlenderMode = ImageSharp.PixelFormats.PixelBlenderMode.Src // enforces that any part of this shape that has color is punched out of the background
|
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
public static IPathCollection BuildCorners(int imageWidth, int imageHeight, float cornerRadius) |
||||
|
{ |
||||
|
// first create a square
|
||||
|
var rect = new SixLabors.Shapes.Rectangle(-0.5f, -0.5f, cornerRadius, cornerRadius); |
||||
|
|
||||
|
// then cut out of the square a circle so we are left with a corner
|
||||
|
var cornerToptLeft = rect.Clip(new SixLabors.Shapes.Ellipse(cornerRadius-0.5f, cornerRadius - 0.5f, cornerRadius)); |
||||
|
|
||||
|
// corner is now a corner shape positions top left
|
||||
|
//lets make 3 more positioned correctly, we cando that by translating the orgional artound the center of the image
|
||||
|
var center = new Vector2(imageWidth / 2, imageHeight / 2); |
||||
|
var angle = Math.PI / 2f; |
||||
|
|
||||
|
float rightPos = imageWidth - cornerToptLeft.Bounds.Width +1; |
||||
|
float bottomPos = imageHeight - cornerToptLeft.Bounds.Height + 1; |
||||
|
|
||||
|
// move it across the widthof the image - the width of the shape
|
||||
|
var cornerTopRight = cornerToptLeft.RotateDegree(90).Translate(rightPos, 0); |
||||
|
var cornerBottomLeft = cornerToptLeft.RotateDegree(-90).Translate(0, bottomPos); |
||||
|
var cornerBottomRight = cornerToptLeft.RotateDegree(180).Translate(rightPos, bottomPos); |
||||
|
|
||||
|
return new PathCollection(cornerToptLeft, cornerBottomLeft, cornerTopRight, cornerBottomRight); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:93bb4d6281dc1e845db57e836e0dca30b7a4062e81044efb27ad4d8b1a33130c |
||||
|
size 15787 |
||||
Loading…
Reference in new issue