-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathGraphicsExtensions.cs
41 lines (36 loc) · 1.39 KB
/
GraphicsExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ched.Drawing
{
public static class GraphicsExtensions
{
// ref: http://csharphelper.com/blog/2016/01/draw-rounded-rectangles-in-c/
internal static GraphicsPath ToRoundedPath(this RectangleF rect, float radius)
{
if (radius * 2 > Math.Min(rect.Width, rect.Height))
{
radius = Math.Min(rect.Width, rect.Height) / 2;
}
var path = new GraphicsPath();
path.AddArc(rect.Left, rect.Top, radius * 2, radius * 2, 180, 90);
path.AddArc(rect.Right - radius * 2, rect.Top, radius * 2, radius * 2, 270, 90);
path.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90);
path.AddArc(rect.Left, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90);
path.CloseFigure();
return path;
}
public static RectangleF Expand(this RectangleF rect, float size)
{
return rect.Expand(size, size);
}
public static RectangleF Expand(this RectangleF rect, float dx, float dy)
{
return new RectangleF(rect.Left - dx, rect.Top - dy, rect.Width + dx * 2, rect.Height + dy * 2);
}
}
}