-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanvas.cs
243 lines (201 loc) · 9.42 KB
/
Canvas.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System.Drawing;
namespace Giraffics
{
/// <summary>
/// A wrapper for the Graphics class. Exposes useful drawing methods.
/// </summary>
public class Canvas
{
protected Graphics graphics;
protected Bitmap bitmap;
public Canvas(Bitmap bitmap)
{
this.bitmap = bitmap;
graphics = Graphics.FromImage(bitmap);
}
public Canvas(Image image)
{
bitmap = (Bitmap)image;
graphics = Graphics.FromImage(image);
}
public Canvas(Size size)
{
bitmap = new Bitmap(size.Width, size.Height);
graphics = Graphics.FromImage(bitmap);
}
/// <summary>Resizes the canvas to a new size while keeping the old contents.
/// Maintains the SmoothingMode of the previous canvas.</summary>
public void ChangeSize(Size newSize)
{
Bitmap newBitmap = new Bitmap(newSize.Width, newSize.Height);
using (Graphics g = Graphics.FromImage(newBitmap))
{
g.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height);
}
Graphics newGraphics = Graphics.FromImage(newBitmap);
newGraphics.SmoothingMode = graphics.SmoothingMode;
graphics = newGraphics;
bitmap = newBitmap;
}
/// <summary>Clears the screen and fills it with the given color.</summary>
public void Clear(Color color)
{
graphics.Clear(color);
}
/// <summary>Clears the screen clean with transparency.</summary>
public void Clear()
{
graphics.Clear(Color.Transparent);
}
/// <summary>Draws the outline of a rectangle.</summary>
public void DrawRect(Color color, int x, int y, int width, int height, int thickness)
{
graphics.DrawRectangle(new Pen(color, thickness), x, y, width, height);
}
/// <summary>Draws the outline of a rectangle.</summary>
public void DrawRect(Color color, Rectangle rect, int thickness)
{
graphics.DrawRectangle(new Pen(color, thickness), rect);
}
/// <summary>Draws the outline of an ellipse.</summary>
public void DrawEllipse(Color color, int x, int y, int width, int height, int thickness)
{
graphics.DrawEllipse(new Pen(color, thickness), x, y, width, height);
}
/// <summary>Draws the outline of an ellipse.</summary>
public void DrawEllipse(Color color, Rectangle rect, int thickness)
{
graphics.DrawEllipse(new Pen(color, thickness), rect);
}
/// <summary>Draws the outline of a circle with its origin at the given position.</summary>
public void DrawCircle(Color color, int x, int y, int radius, int thickness)
{
DrawEllipse(color, x - radius, y - radius, 2 * radius, 2 * radius, thickness);
}
/// <summary>Draws the outline of a circle with its origin at the given position.</summary>
public void DrawCircle(Color color, Point position, int radius, int thickness)
{
DrawEllipse(color, position.X - radius, position.Y - radius, 2 * radius, 2 * radius, thickness);
}
/// <summary>Draws the outline of a polygon made up of the given points.</summary>
public void DrawPolygon(Color color, Point[] points, int thickness)
{
graphics.DrawPolygon(new Pen(color, thickness), points);
}
/// <summary>Draws a line between two points.</summary>
public void DrawLine(Color color, int x1, int y1, int x2, int y2, int thickness)
{
graphics.DrawLine(new Pen(color, thickness), x1, y1, x2, y2);
}
/// <summary>Draws a line between two points.</summary>
public void DrawLine(Color color, Point point1, Point point2, int thickness)
{
graphics.DrawLine(new Pen(color, thickness), point1, point2);
}
/// <summary>Draws lines between all of the given points in order from start to finish.</summary>
public void DrawLines(Color color, Point[] points, int thickness)
{
graphics.DrawLines(new Pen(color, thickness), points);
}
/// <summary>Draws a curved line spanning between the given points.</summary>
public void DrawCurve(Color color, Point[] points, float tension, int thickness)
{
graphics.DrawCurve(new Pen(color, thickness), points, tension);
}
/// <summary>Draws a looped curvy line spanning between the given points.</summary>
public void DrawClosedCurve(Color color, Point[] points, int thickness)
{
graphics.DrawClosedCurve(new Pen(color, thickness), points);
}
/// <summary>Draws a bezier line between four points.</summary>
public void DrawBezier(Color color, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, int thickness)
{
graphics.DrawBezier(new Pen(color, thickness), x1, y1, x2, y2, x3, y3, x4, y4);
}
/// <summary>Draws a bezier line between four points.</summary>
public void DrawBezier(Color color, Point point1, Point point2, Point point3, Point point4, int thickness)
{
graphics.DrawBezier(new Pen(color, thickness), point1, point2, point3, point4);
}
/// <summary>Draws the portion of an ellipse's outline starting at startAngle and with a segment length defined by sweepAngle.</summary>
public void DrawArc(Color color, int x1, int y1, int width, int height, float startAngle, float sweepAngle, int thickness)
{
graphics.DrawArc(new Pen(color, thickness), x1, y1, width, height, startAngle, sweepAngle);
}
/// <summary>Draws the portion of an ellipse's outline starting at startAngle and with a segment length defined by sweepAngle.</summary>
public void DrawArc(Color color, Rectangle rect, float startAngle, float sweepAngle, int thickness)
{
graphics.DrawArc(new Pen(color, thickness), rect, startAngle, sweepAngle);
}
/// <summary>Draws an image at the specified location.</summary>
public void DrawImage(Image image, int x, int y)
{
graphics.DrawImage(image, x, y);
}
/// <summary>Draws an image with the given width and height at the specified location.</summary>
public void DrawImage(Image image, int x, int y, int width, int height)
{
graphics.DrawImage(image, x, y, width, height);
}
/// <summary>Draws an image whose dimensions and location are defined by the Rectangle argument.</summary>
public void DrawImage(Image image, Rectangle rect)
{
graphics.DrawImage(image, rect);
}
/// <summary>Draws text at the given location. Size decided by font size.</summary>
public void DrawText(Color color, string text, int x, int y, Font font)
{
graphics.DrawString(text, font, new SolidBrush(color), x, y);
}
/// <summary>Draws text at the given location. Size decided by font size.</summary>
public void DrawText(Color color, string text, Point position, Font font)
{
graphics.DrawString(text, font, new SolidBrush(color), position);
}
/// <summary>Draws text inside of the given bounds.</summary>
public void DrawText(Color color, string text, RectangleF bounds, Font font)
{
graphics.DrawString(text, font, new SolidBrush(color), bounds);
}
/// <summary>Draws a filled rectangle.</summary>
public void FillRect(Color color, int x, int y, int width, int height)
{
graphics.FillRectangle(new SolidBrush(color), x, y, width, height);
}
/// <summary>Draws a filled rectangle.</summary>
public void FillRect(Color color, Rectangle rect)
{
graphics.FillRectangle(new SolidBrush(color), rect);
}
/// <summary>Draws a filled ellipse.</summary>
public void FillEllipse(Color color, int x, int y, int width, int height)
{
graphics.FillEllipse(new SolidBrush(color), x, y, width, height);
}
/// <summary>Draws a filled ellipse.</summary>
public void FillEllipse(Color color, Rectangle rect)
{
graphics.FillEllipse(new SolidBrush(color), rect);
}
/// <summary>Draws a filled circle with its origin at the given position.</summary>
public void FillCircle(Color color, int x, int y, int radius)
{
FillEllipse(color, x - radius, y - radius, 2 * radius, 2 * radius);
}
/// <summary>Draws a filled circle with its origin at the given position.</summary>
public void FillCircle(Color color, Point position, int radius)
{
FillEllipse(color, position.X - radius, position.Y - radius, 2 * radius, 2 * radius);
}
/// <summary>Draws a filled polygon.</summary>
public void FillPolygon(Color color, Point[] points)
{
graphics.FillPolygon(new SolidBrush(color), points);
}
/// <summary>Draws a filled looped curvy line spanning between the given points.</summary>
public void FillClosedCurve(Color color, Point[] points)
{
graphics.FillClosedCurve(new SolidBrush(color), points);
}
}
}