2 // Sample application for region graphics functions using GraphicsPaths implementation
5 // Jordi Mas i Hernandez, jordi@ximian.com
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Drawing.Imaging;
34 using System.Drawing.Drawing2D;
40 public static void Main ()
43 Bitmap bmp = new Bitmap (600, 300);
44 Graphics dc = Graphics.FromImage (bmp);
45 Font fnt = new Font ("Arial", 8);
46 Font fnttitle = new Font ("Arial", 8, FontStyle.Underline);
47 Matrix matrix = new Matrix ();
48 GraphicsPath patha = new GraphicsPath ();
49 GraphicsPath pathb = new GraphicsPath ();
50 Pen redPen = new Pen (Color.Red, 2);
55 SolidBrush whiteBrush = new SolidBrush (Color.White);
57 dc.DrawString ("Region samples using GraphicsPath", fnttitle, whiteBrush, 5, 5);
60 patha.AddLine (60, 40, 90, 90);
61 patha.AddLine (90, 90, 10, 90);
62 patha.AddLine (10, 90, 60, 40);
63 dc.DrawPath (redPen, patha);
65 pathb.AddEllipse(30, 55, 60, 60);
66 dc.DrawPath(redPen, pathb);
68 rgn1 = new Region (patha);
69 rgn2 = new Region (pathb);
70 rgn1.Complement (rgn2);
71 dc.FillRegion (Brushes.Blue, rgn1);
72 dc.DrawString ("Complement (" + rgn1.GetRegionScans (matrix).Length +")", fnt, whiteBrush, 10, 140);
73 dc.DrawRectangles (Pens.Yellow, rgn1.GetRegionScans (matrix));
79 patha.AddLine (60+x, 40, 90+x, 90);
80 patha.AddLine (90+x, 90, 10+x, 90);
81 patha.AddLine (10+x, 90, 60+x, 40);
83 dc.DrawPath (redPen, patha);
85 pathb.AddEllipse (30+x, 55, 60, 60);
86 dc.DrawPath(redPen, pathb);
88 rgn1 = new Region (patha);
89 rgn2 = new Region (pathb);
91 dc.FillRegion (Brushes.Blue, rgn1);
92 dc.DrawString ("Exclude (" + rgn1.GetRegionScans (matrix).Length +")", fnt, whiteBrush, 140, 140);
93 dc.DrawRectangles (Pens.Yellow, rgn1.GetRegionScans (matrix));
99 patha.AddLine (60+x, 40, 90+x, 90);
100 patha.AddLine (90+x, 90, 10+x, 90);
101 patha.AddLine (10+x, 90, 60+x, 40);
103 dc.DrawPath (redPen, patha);
105 pathb.AddEllipse (30+x, 55, 60, 60);
106 dc.DrawPath (redPen, pathb);
108 rgn1 = new Region (patha);
109 rgn2 = new Region (pathb);
110 rgn1.Intersect (rgn2);
111 dc.FillRegion (Brushes.Blue, rgn1);
112 dc.DrawString ("Intersect (" + rgn1.GetRegionScans (matrix).Length +")", fnt, whiteBrush, 270, 140);
113 dc.DrawRectangles (Pens.Yellow, rgn1.GetRegionScans (matrix));
119 patha.AddLine (60+x, 40, 90+x, 90);
120 patha.AddLine (90+x, 90, 10+x, 90);
121 patha.AddLine (10+x, 90, 60+x, 40);
123 dc.DrawPath (redPen, patha);
125 pathb.AddEllipse (30+x, 55, 60, 60);
126 dc.DrawPath (redPen, pathb);
128 rgn1 = new Region (patha);
129 rgn2 = new Region (pathb);
131 dc.FillRegion(Brushes.Blue, rgn1);
132 dc.DrawString ("Xor (" + rgn1.GetRegionScans (matrix).Length +")", fnt, whiteBrush, 380, 140);
133 dc.DrawRectangles (Pens.Yellow, rgn1.GetRegionScans (matrix));
139 patha.AddLine (60+x, 40, 90+x, 90);
140 patha.AddLine (90+x, 90, 10+x, 90);
141 patha.AddLine (10+x, 90, 60+x, 40);
143 dc.DrawPath (redPen, patha);
145 pathb.AddEllipse (30+x, 55, 60, 60);
146 dc.DrawPath (redPen, pathb);
148 rgn1 = new Region (patha);
149 rgn2 = new Region (pathb);
151 dc.FillRegion(Brushes.Blue, rgn1);
152 dc.DrawRectangles (Pens.Yellow, rgn1.GetRegionScans (matrix));
153 dc.DrawString ("Union (" + rgn1.GetRegionScans (matrix).Length +")", fnt, whiteBrush, 490, 140);
155 bmp.Save("regionsgp.bmp", ImageFormat.Bmp);