RegionsGraphicsPath.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // Sample application for region graphics functions using GraphicsPaths implementation
  3. //
  4. // Author:
  5. // Jordi Mas i Hernandez, [email protected]
  6. //
  7. //
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Drawing.Imaging;
  31. using System.Drawing;
  32. using System.Drawing.Drawing2D;
  33. //
  34. public class Regions
  35. {
  36. public static void Main ()
  37. {
  38. Bitmap bmp = new Bitmap (600, 300);
  39. Graphics dc = Graphics.FromImage (bmp);
  40. Font fnt = new Font ("Arial", 8);
  41. Font fnttitle = new Font ("Arial", 8, FontStyle.Underline);
  42. Matrix matrix = new Matrix ();
  43. GraphicsPath patha = new GraphicsPath ();
  44. GraphicsPath pathb = new GraphicsPath ();
  45. Pen redPen = new Pen (Color.Red, 2);
  46. Region rgn1;
  47. Region rgn2;
  48. int x = 0;
  49. SolidBrush whiteBrush = new SolidBrush (Color.White);
  50. dc.DrawString ("Region samples using GraphicsPath", fnttitle, whiteBrush, 5, 5);
  51. /* First*/
  52. patha.AddLine (60, 40, 90, 90);
  53. patha.AddLine (90, 90, 10, 90);
  54. patha.AddLine (10, 90, 60, 40);
  55. dc.DrawPath (redPen, patha);
  56. pathb.AddEllipse(30, 55, 60, 60);
  57. dc.DrawPath(redPen, pathb);
  58. rgn1 = new Region (patha);
  59. rgn2 = new Region (pathb);
  60. rgn1.Complement (rgn2);
  61. dc.FillRegion (Brushes.Blue, rgn1);
  62. dc.DrawString ("Complement (" + rgn1.GetRegionScans (matrix).Length +")", fnt, whiteBrush, 10, 140);
  63. dc.DrawRectangles (Pens.Yellow, rgn1.GetRegionScans (matrix));
  64. x += 110;
  65. /* Second*/
  66. patha.Reset ();
  67. pathb.Reset ();
  68. patha.AddLine (60+x, 40, 90+x, 90);
  69. patha.AddLine (90+x, 90, 10+x, 90);
  70. patha.AddLine (10+x, 90, 60+x, 40);
  71. dc.DrawPath (redPen, patha);
  72. pathb.AddEllipse (30+x, 55, 60, 60);
  73. dc.DrawPath(redPen, pathb);
  74. rgn1 = new Region (patha);
  75. rgn2 = new Region (pathb);
  76. rgn1.Exclude (rgn2);
  77. dc.FillRegion (Brushes.Blue, rgn1);
  78. dc.DrawString ("Exclude (" + rgn1.GetRegionScans (matrix).Length +")", fnt, whiteBrush, 140, 140);
  79. dc.DrawRectangles (Pens.Yellow, rgn1.GetRegionScans (matrix));
  80. x += 110;
  81. /* Third*/
  82. patha.Reset ();
  83. pathb.Reset ();
  84. patha.AddLine (60+x, 40, 90+x, 90);
  85. patha.AddLine (90+x, 90, 10+x, 90);
  86. patha.AddLine (10+x, 90, 60+x, 40);
  87. dc.DrawPath (redPen, patha);
  88. pathb.AddEllipse (30+x, 55, 60, 60);
  89. dc.DrawPath (redPen, pathb);
  90. rgn1 = new Region (patha);
  91. rgn2 = new Region (pathb);
  92. rgn1.Intersect (rgn2);
  93. dc.FillRegion (Brushes.Blue, rgn1);
  94. dc.DrawString ("Intersect (" + rgn1.GetRegionScans (matrix).Length +")", fnt, whiteBrush, 270, 140);
  95. dc.DrawRectangles (Pens.Yellow, rgn1.GetRegionScans (matrix));
  96. x += 110;
  97. /* Four*/
  98. patha.Reset ();
  99. pathb.Reset ();
  100. patha.AddLine (60+x, 40, 90+x, 90);
  101. patha.AddLine (90+x, 90, 10+x, 90);
  102. patha.AddLine (10+x, 90, 60+x, 40);
  103. dc.DrawPath (redPen, patha);
  104. pathb.AddEllipse (30+x, 55, 60, 60);
  105. dc.DrawPath (redPen, pathb);
  106. rgn1 = new Region (patha);
  107. rgn2 = new Region (pathb);
  108. rgn1.Xor (rgn2);
  109. dc.FillRegion(Brushes.Blue, rgn1);
  110. dc.DrawString ("Xor (" + rgn1.GetRegionScans (matrix).Length +")", fnt, whiteBrush, 380, 140);
  111. dc.DrawRectangles (Pens.Yellow, rgn1.GetRegionScans (matrix));
  112. x += 110;
  113. /* Fifth */
  114. patha.Reset ();
  115. pathb.Reset ();
  116. patha.AddLine (60+x, 40, 90+x, 90);
  117. patha.AddLine (90+x, 90, 10+x, 90);
  118. patha.AddLine (10+x, 90, 60+x, 40);
  119. dc.DrawPath (redPen, patha);
  120. pathb.AddEllipse (30+x, 55, 60, 60);
  121. dc.DrawPath (redPen, pathb);
  122. rgn1 = new Region (patha);
  123. rgn2 = new Region (pathb);
  124. rgn1.Union (rgn2);
  125. dc.FillRegion(Brushes.Blue, rgn1);
  126. dc.DrawRectangles (Pens.Yellow, rgn1.GetRegionScans (matrix));
  127. dc.DrawString ("Union (" + rgn1.GetRegionScans (matrix).Length +")", fnt, whiteBrush, 490, 140);
  128. bmp.Save("regionsgp.bmp", ImageFormat.Bmp);
  129. }
  130. }