graphicsUi.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // A sample application for some graphics.cs functions implementation
  3. //
  4. // Author:
  5. // Jordi Mas i Hernàndez, [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. namespace MonoSamples.System.Drawing
  34. {
  35. public class graphicsUI
  36. {
  37. public static void Main () {
  38. Bitmap bmp = new Bitmap (500, 250);
  39. Graphics dc = Graphics.FromImage (bmp);
  40. Pen BluePen = new Pen (Color.Blue, 3);
  41. Pen GreenPen = new Pen (Color.Green, 3);
  42. Pen RedPen = new Pen (Color.Red, 3);
  43. SolidBrush redBrush = new SolidBrush (Color.Red);
  44. SolidBrush blueBrush = new SolidBrush (Color.Blue);
  45. int x = 0;
  46. int y = 0;
  47. /* First Row */
  48. dc.DrawRectangle (BluePen, x, y, 50, 50);
  49. x += 50;
  50. dc.DrawEllipse (RedPen, x, y, 70, 50);
  51. dc.DrawArc (BluePen, x, y, 50, 40, (float) 0, (float) 120);
  52. x += 70;
  53. dc.DrawBezier (GreenPen, new Point (x, y + 5),
  54. new Point (x + 50, y + 5),
  55. new Point (x + 20, y + 20),
  56. new Point (x + 50, y + 50));
  57. x += 50;
  58. PointF point1 = new PointF (10.0F + x, 10.0F);
  59. PointF point2 = new PointF (10.0F + x, 5.0F);
  60. PointF point3 = new PointF (40.0F + x, 5.0F);
  61. PointF point4 = new PointF (50.0F + x, 10.0F);
  62. PointF point5 = new PointF (60.0F + x, 20.0F);
  63. PointF point6 = new PointF (70.0F + x, 40.0F);
  64. PointF point7 = new PointF (50.0F + x, 50.0F);
  65. PointF[] curvePoints = {point1, point2, point3, point4,
  66. point5, point6, point7};
  67. dc.DrawLines (RedPen, curvePoints);
  68. float tension = 1.0F;
  69. FillMode aFillMode = FillMode.Alternate;
  70. dc.DrawClosedCurve (GreenPen, curvePoints, tension, aFillMode);
  71. x += 80;
  72. // FillClosedCurve
  73. PointF point10 = new PointF (x, y + 15.0F);
  74. PointF point20 = new PointF (x + 40.0F, y + 10.0F);
  75. PointF point30 = new PointF (x + 50.0F, y + 40.0F);
  76. PointF point40 = new PointF (x + 10.0F, y + 30.0F);
  77. PointF[] points = {point10, point20, point30, point40};
  78. FillMode newFillMode = FillMode.Winding;
  79. dc.FillClosedCurve (redBrush, points, newFillMode, tension);
  80. // Fill pie to screen.
  81. dc.FillPie (blueBrush, x, 0, 200.0F, 100.0f, 300.0F, 45.0F);
  82. /* second row */
  83. y += 80;
  84. x = 0;
  85. // Clipping and Graphics container test
  86. dc.SetClip (new Rectangle (5 + x, 5 + y, 75, 75));
  87. // Begin a graphics container.
  88. GraphicsContainer container = dc.BeginContainer ();
  89. // Set an additional clipping region for the container.
  90. dc.SetClip (new Rectangle (50 + x, 25 + y, 50, 37));
  91. // Fill a red rectangle in the container.
  92. dc.FillRectangle (redBrush, 0, 0, 200, 200);
  93. dc.EndContainer (container);
  94. SolidBrush blueBrushLight = new SolidBrush (
  95. Color.FromArgb (128, 0, 0, 255));
  96. dc.FillRectangle (blueBrushLight, 0, 0, 200, 200);
  97. dc.ResetClip ();
  98. Pen blackPen = new Pen (Color.FromArgb (255, 0, 0, 0), 2.0f);
  99. dc.DrawRectangle (blackPen, 5 + x, 5 + y, 75, 75);
  100. dc.DrawRectangle (blackPen, 50 + x, 25 + y, 50, 37);
  101. x = 100;
  102. y += 10;
  103. Point[] ptstrans = {new Point(x, y), new Point (50 + x, 25 + y)};
  104. dc.DrawLine (BluePen, ptstrans [0], ptstrans [1]);
  105. dc.TranslateTransform (40.0F, 30.0F);
  106. dc.TransformPoints (CoordinateSpace.Page, CoordinateSpace.World,
  107. ptstrans);
  108. dc.ResetTransform ();
  109. dc.DrawLine (RedPen, ptstrans [0], ptstrans [1]);
  110. bmp.Save ("graphicsui.bmp", ImageFormat.Bmp);
  111. }
  112. }
  113. }