minhlap Admin - Quản trị viên
Tổng số bài gửi : 129 Points : 374 Reputation : 5 Join date : 22/07/2009 Age : 34 Đến từ : TP Hồ Chí Minh
| Tiêu đề: Hàm đồ họa trong C# Thu Aug 27, 2009 11:24 am | |
| Một số hàm đồ họa trong C#- DrawArc(): vẽ vòng cung - DrawBezier/DrawBeziers: vẽ hình Bezier - DrawEllipse() : vẽ hình Elip và hình tròn - DrawIcon(): vẽ một icon tượng trưng bởi một đối tượng Icon - DrawImage(): vẽ một hình ảnh tượng trưng bởi 1 đối tượng Image - DrawLine()/DrawLines(): vẽ đường thẳng - DrawPolygon(): vẽ đa giác. - DrawRectangle()/DrawRectangles() vẽ hình chữ nhật - DrawString(): vẽ một chuỗi văn bản. - Code:
-
1. using System; 2. using System.Drawing; 3. using System.IO; 4. using System.Drawing.Printing; 5. using System.Drawing.Imaging; 6. using System.Drawing.Drawing2D; 7. 8. public class PrintingMargins 9. { 10. 11. public static void Main (string[ args) 12. { 13. Bitmap bmp = new Bitmap (600, 600); 14. Graphics gr = Graphics.FromImage (bmp); 15. Rectangle rect = new Rectangle (20, 20, 100, 100); 16. 17. PointF[ polygon_pnts = {new PointF(150.0F, 150.0F), 18. new PointF(200.0F, 125.0F), new PointF(300.0F, 105.0F), 19. new PointF(350.0F, 150.0F), new PointF(400.0F, 200.0F), 20. new PointF(450.0F, 300.0F), new PointF(350.0F, 350.0F) }; 21. 22. 23. // Default Display 24. gr.DrawRectangle (Pens.Red, rect); 25. gr.DrawString ("Unit " + gr.PageUnit, new Font ("Arial", 10), Brushes.Red, 50, 50); 26. gr.DrawArc (Pens.Red, 30, 30, 60, 60, 0, 180); 27. gr.DrawPolygon (Pens.Red, polygon_pnts); 28. 29. // Point 30. gr.PageUnit = GraphicsUnit.Point; 31. gr.DrawRectangle (Pens.Yellow, rect); 32. gr.DrawString ("Unit " + gr.PageUnit, new Font ("Arial", 10), Brushes.Yellow, 50, 50); 33. gr.DrawArc (Pens.Yellow, 30, 30, 60, 60, 0, 180); 34. gr.DrawPolygon (Pens.Yellow, polygon_pnts); 35. 36. // Document 37. gr.PageUnit = GraphicsUnit.Document; 38. gr.DrawRectangle (Pens.Pink, rect); 39. gr.DrawString ("Unit " + gr.PageUnit, new Font ("Arial", 10), Brushes.Pink, 50, 50); 40. gr.DrawArc (Pens.Pink, 30, 30, 60, 60, 0, 180); 41. gr.DrawPolygon (Pens.Pink, polygon_pnts); 42. 43. // Inc 44. gr.PageUnit = GraphicsUnit.Inch; 45. gr.DrawRectangle (Pens.Blue, 3f, 1f, 1f, 1f); 46. gr.DrawString ("Unit " + gr.PageUnit, new Font ("Arial", 10), Brushes.Blue, 0.7f, 0.7f); 47. gr.DrawArc (Pens.Blue, 3f, 3f, 1f, 1f, 0, 180); 48. 49. 50. bmp.Save ("units1.bmp"); 51. bmp.Dispose (); 52. gr.Dispose (); 53. 54. bmp = new Bitmap (600, 600); 55. gr = Graphics.FromImage (bmp); 56. 57. GraphicsPath graphPath = new GraphicsPath(); 58. graphPath.AddEllipse (0, 80, 100, 200); 59. 60. // Default Display 61. gr.DrawBezier (Pens.Red, new Point (10, 10), new Point (20, 10), 62. new Point (35, 50), new Point (50, 10)); 63. 64. gr.DrawEllipse (Pens.Red, 10, 50, 30, 50); 65. gr.DrawPath (Pens.Red, graphPath); 66. gr.DrawPie (Pens.Red, 150, 20, 60, 60, 100, 140); 67. gr.DrawCurve (Pens.Red, polygon_pnts, 2, 4, 0.5f); 68. 69. 70. // Point 71. gr.PageUnit = GraphicsUnit.Display; 72. gr.PageUnit = GraphicsUnit.Point; 73. gr.DrawBezier (Pens.Pink, new Point (10, 10), new Point (20, 10), 74. new Point (35, 50), new Point (50, 10)); 75. gr.DrawCurve (Pens.Pink, polygon_pnts, 2, 4, 0.5f); 76. 77. gr.DrawEllipse (Pens.Pink, 10, 50, 30, 50); 78. gr.DrawPath (Pens.Pink, graphPath); 79. gr.DrawPie (Pens.Pink, 150, 20, 60, 60, 100, 140); 80. 81. // Document 82. gr.PageUnit = GraphicsUnit.Document; 83. gr.DrawBezier (Pens.Yellow, new Point (10, 10), new Point (20, 10), 84. new Point (35, 50), new Point (50, 10)); 85. 86. gr.DrawEllipse (Pens.Yellow, 10, 50, 30, 50); 87. gr.DrawPath (Pens.Yellow, graphPath); 88. gr.DrawPie (Pens.Yellow, 150, 20, 60, 60, 100, 140); 89. gr.DrawCurve (Pens.Yellow, polygon_pnts, 2, 4, 0.5f); 90. 91. // Inc 92. gr.PageUnit = GraphicsUnit.Inch; 93. gr.DrawBezier (Pens.Blue, new Point (10, 10), new Point (20, 10), 94. new Point (35, 50), new Point (50, 10)); 95. 96. gr.DrawEllipse (Pens.Blue, 10, 50, 30, 50); 97. gr.DrawPath (Pens.Blue, graphPath); 98. gr.DrawPie (Pens.Blue, 150, 20, 60, 60, 100, 140); 99. gr.DrawCurve (Pens.Blue, polygon_pnts, 2, 4, 0.5f); 100. 101. bmp.Save ("units2.bmp"); 102. } 103. }
| |
|