1 |
using System; |
2 |
using System.Drawing; |
3 |
using System.Drawing.Imaging; |
4 |
|
5 |
namespace BasicDrawingSample |
6 |
{ |
7 |
class EllipseDrawer |
8 |
{ |
9 |
public Image Draw(int width, int height, int |
10 |
strokeWidth, Color strokeColor, Color fillColor) |
11 |
{ |
12 |
|
13 |
Image image = new Bitmap(width + strokeWidth, |
14 |
height + strokeWidth); |
15 |
|
16 |
|
17 |
width for use later |
18 |
float halfStrokeWidth = strokeWidth / 2F; |
19 |
|
20 |
|
21 |
ellipse we want to draw |
22 |
RectangleF ellipseBound = new RectangleF( |
23 |
halfStrokeWidth, halfStrokeWidth, |
24 |
width, height); |
25 |
|
26 |
|
27 |
using(Graphics graphics = |
28 |
Graphics.FromImage(image)) |
29 |
{ |
30 |
|
31 |
using(Brush fillBrush = new |
32 |
SolidBrush(fillColor)) |
33 |
{ |
34 |
|
35 |
specified by the rectangle calculated above |
36 |
graphics.FillEllipse(fillBrush, ellipseBound); |
37 |
} |
38 |
|
39 |
|
40 |
using(Pen pen = new |
41 |
Pen(strokeColor, strokeWidth)) |
42 |
{ |
43 |
|
44 |
the ellipse specified by the rectangle calculated above |
45 |
|
46 |
graphics.DrawEllipse(pen, ellipseBound); |
47 |
} |
48 |
} |
49 |
|
50 |
return image; |
51 |
} |
52 |
|
53 |
[STAThread] |
54 |
static void Main(string[] args) |
55 |
{ |
56 |
if(args.Length != 5) |
57 |
{ |
58 |
Console.WriteLine("Usage: |
59 |
ellipseDrawer width height stroke-width stroke-color fill-color"); |
60 |
} |
61 |
else |
62 |
{ |
63 |
|
64 |
line arguments |
65 |
int width = Int32.Parse(args[0]); |
66 |
int height = Int32.Parse(args[1]); |
67 |
int strokeWidth = Int32.Parse(args[2]); |
68 |
Color strokeColor = |
69 |
ColorTranslator.FromHtml(args[3]); |
70 |
Color fillColor = |
71 |
ColorTranslator.FromHtml(args[4]); |
72 |
|
73 |
|
74 |
EllipseDrawer |
75 |
EllipseDrawer ellipseDrawer = |
76 |
new EllipseDrawer(); |
77 |
|
78 |
|
79 |
Image image = |
80 |
ellipseDrawer.Draw(width, height, strokeWidth, strokeColor, |
81 |
fillColor); |
82 |
|
83 |
|
84 |
image.Save("ellipse.png", |
85 |
ImageFormat.Png); |
86 |
|
87 |
|
88 |
image.Dispose(); |
89 |
} |
90 |
} |
91 |
} |
92 |
} |
93 |
|