C# offers many additional graphics capabilities. The Brush hierarchy, for example, also includes HatchBrush, LinearGradientBrush, PathGradientBrush and TextureBrush.
The program in Fig. 17.21 demonstrates several graphics features, such as dashed lines, thick lines and the ability to fill shapes with various patterns. These represent just a few of the additional capabilities of the System.Drawing namespace.
Fig. 17.21
Shapes drawn on a form. (Part 1 of 2.)
1
|
2
|
3 using System;
|
4 using System.Drawing;
|
5 using System.Drawing.Drawing2D;
|
6 using System.Windows.Forms;
|
7
|
8
|
9 public partial class DrawShapesForm : Form
|
10 {
|
11
|
12 public DrawShapesForm()
|
13 {
|
14 InitializeComponent();
|
15 }
|
16
|
17
|
18 private void DrawShapesForm_Paint( object sender, PaintEventArgs e )
|
19 {
|
20
|
21 Graphics graphicsObject = e.Graphics;
|
22
|
23
|
24 Rectangle drawArea1 = new Rectangle( 5, 35, 30, 100 );
|
25 LinearGradientBrush linearBrush =
|
26 new LinearGradientBrush ( drawArea1, Color.Blue,
|
27 Color.Yellow, LinearGradientMode.ForwardDiagonal );
|
28
|
29
|
30 graphicsObject.FillEllipse( linearBrush, 5, 30, 65, 100 );
|
31
|
32
|
33 Pen thickRedPen = new Pen( Color.Red, 10 );
|
34 Rectangle drawArea2 = new Rectangle( 80, 30, 65, 100 );
|
35
|
36
|
37 graphicsObject.DrawRectangle( thickRedPen, drawArea2 );
|
38
|
39
|
40 Bitmap textureBitmap = new Bitmap( 10, 10 );
|
41
|
42
|
43 Graphics graphicsObject2 =
|
44 Graphics.FromImage( textureBitmap );
|
45
|
46
|
47 SolidBrush solidColorBrush =
|
48 new SolidBrush( Color.Red );
|
49 Pen coloredPen = new Pen( solidColorBrush );
|
50
|
51
|
52 solidColorBrush.Color = Color.Yellow;
|
53 graphicsObject2.FillRectangle( solidColorBrush, 0, 0, 10, 10 );
|
54
|
55
|
56 coloredPen.Color = Color.Black;
|
57 graphicsObject2.DrawRectangle( coloredPen, 1, 1, 6, 6 );
|
58
|
59
|
60 solidColorBrush.Color = Color.Blue;
|
61 graphicsObject2.FillRectangle( solidColorBrush, 1, 1, 3, 3 );
|
62
|
63
|
64 solidColorBrush.Color = Color.Red;
|
65 graphicsObject2.FillRectangle( solidColorBrush, 4, 4, 3, 3 );
|
66
|
67
|
68
|
69 TextureBrush texturedBrush =
|
70 new TextureBrush( textureBitmap );
|
71 graphicsObject.FillRectangle( texturedBrush, 155, 30, 75, 100 );
|
72
|
73
|
74 coloredPen.Color = Color.White;
|
75 coloredPen.Width = 6;
|
76 graphicsObject.DrawPie( coloredPen, 240, 30, 75, 100, 0, 270 );
|
77
|
78
|
79 coloredPen.Color = Color.Green;
|
80 coloredPen.Width = 5;
|
81 graphicsObject.DrawLine( coloredPen, 395, 30, 320, 150 );
|
82
|
83
|
84 coloredPen.Color = Color.Yellow;
|
85 coloredPen.DashCap = DashCap.Round;
|
86 coloredPen.DashStyle = DashStyle.Dash;
|
87 graphicsObject.DrawLine( coloredPen, 320, 30, 395,v150 );
|
88 }
|
89 }
|
|
|