八一女篮队员:WPF几何绘图(一)画直线

来源:百度文库 编辑:中财网 时间:2024/05/09 09:28:48

WPF几何绘图(一)画直线

分类: WPF Drawing系列 2009-02-26 11:40 5058人阅读 评论(4) 收藏 举报

准备工作,WPF的界面这里就不涉及了,完全是代码工作。

首先,我们要做图必须有个容器,如果你要画画必须有纸张一样。这里我们声明一个Canvas作为容器。

代码如下:

[c-sharp] view plaincopy?

  1. Canvas mainPanel = new Canvas();  
  2. public DrawLine()  
  3. {  
  4.     InitializeComponent();  
  5.     // Add path shape to the UI.   
  6.     this.Content = mainPanel;  
  7. }  

 

总的来说画直线有三种方法:

1。使用LineGeometry,实例代码如下

[c-sharp] view plaincopy?

  1. ///    
  2.         /// 绘制线段   
  3.         ///    
  4.         protected void DrawingLine(Point startPt,Point endPt)  
  5.         {  
  6.             LineGeometry myLineGeometry = new LineGeometry();  
  7.             myLineGeometry.StartPoint = startPt;  
  8.             myLineGeometry.EndPoint = endPt;  
  9.   
  10.             Path myPath = new Path();  
  11.             myPath.Stroke = Brushes.Black;  
  12.             myPath.StrokeThickness = 1;  
  13.             myPath.Data = myLineGeometry;  
  14.   
  15.             mainPanel.Children.Add(myPath);  
  16.               
  17.         }  

这个很简单,就不多作解释了。

2。使用 LineSegment

我们使用LineSegment时,因为它不是geometry的扩展类,所以需要一个包装:PathGeometry才能将它付给path坐Data进而呈现在Canvas上。但是麻烦的是PathGeometry不能直接包容segment,它所包容的是PathFigure,所以我们还需一个中间层。msdn的解释是:

PathGeometry 使用的语法比简单的 LineGeometry 使用的语法要详细得多,在本例中使用 LineGeometry 类可能更有效,但是使用 PathGeometry 的详细语法可以创建极其复杂的几何区域。也就是说使用PathGeometry可以轻松构建更复杂的图形。

代码如下:

[c-sharp] view plaincopy?

  1. // Create a figure that describes a    
  2. // line from (10,20) to (100,130).   
  3. PathFigure myPathFigure = new PathFigure();  
  4. myPathFigure.StartPoint = new Point(10,20);  
  5. myPathFigure.Segments.Add(  
  6.     new LineSegment(new Point(100,130),  
  7.     true /* IsStroked */ ));  
  8.   
  9. /// Create a PathGeometry to contain the figure.   
  10. PathGeometry myPathGeometry = new PathGeometry();  
  11. myPathGeometry.Figures.Add(myPathFigure);  
  12.   
  13. // Display the PathGeometry.    
  14. Path myPath = new Path();  
  15. myPath.Stroke = Brushes.Black;  
  16. myPath.StrokeThickness = 1;  
  17. myPath.Data = myPathGeometry;  

 

3。比较常用的一种方法,用于绘制连续的线段:StreamGeometry 。它与 PathGeometry 类类似,StreamGeometry 定义一个可以包含曲线、弧线和直线的复杂几何形状。与 PathGeometry 不同,StreamGeometry 的内容不支持数据绑定、动画或修改。当您需要描绘复杂的几何图形,但不希望因为支持数据绑定、动画或修改而引入系统开销时,可使用 StreamGeometry。由于它的高效,StreamGeometry 类是描绘装饰物的理想选择。

实例代码如下:

[c-sharp] view plaincopy?

  1. public StreamGeometryTriangleExample()  
  2.         {  
  3.             // Create a path to draw a geometry with.   
  4.             Path myPath = new Path();  
  5.             myPath.Stroke = Brushes.Black;  
  6.             myPath.StrokeThickness = 1;  
  7.   
  8.             // Create a StreamGeometry to use to specify myPath.   
  9.             StreamGeometry geometry = new StreamGeometry();  
  10.             geometry.FillRule = FillRule.EvenOdd;  
  11.   
  12.             // Open a StreamGeometryContext that can be used to describe this StreamGeometry    
  13.             // object's contents.   
  14.             using (StreamGeometryContext ctx = geometry.Open())  
  15.             {  
  16.   
  17.                 // Begin the triangle at the point specified. Notice that the shape is set to    
  18.                 // be closed so only two lines need to be specified below to make the triangle.   
  19.                 ctx.BeginFigure(new Point(10, 100), true /* is filled */, true /* is closed */);  
  20.   
  21.                 // Draw a line to the next specified point.   
  22.                 ctx.LineTo(new Point(100, 100), true /* is stroked */, false /* is smooth join */);  
  23.   
  24.                 // Draw another line to the next specified point.   
  25.                 ctx.LineTo(new Point(100, 50), true /* is stroked */, false /* is smooth join */);  
  26.             }  
  27.   
  28.             // Freeze the geometry (make it unmodifiable)   
  29.             // for additional performance benefits.   
  30.             geometry.Freeze();  
  31.   
  32.             // Specify the shape (triangle) of the Path using the StreamGeometry.   
  33.             myPath.Data = geometry;  
  34.   
  35.             // Add path shape to the UI.   
  36.             StackPanel mainPanel = new StackPanel();  
  37.             mainPanel.Children.Add(myPath);  
  38.             this.Content = mainPanel;  
  39.         }  

 

好了方法说完了。以下是我做的一个练习,效果如图。

 

 

代码如下:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Windows;  
  5. using System.Windows.Controls;  
  6. using System.Windows.Data;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Imaging;  
  11. using System.Windows.Shapes;  
  12.   
  13. namespace WPFDrawingTraning  
  14. {  
  15.     ///    
  16.     /// Interaction logic for DrawLine.xaml   
  17.     ///    
  18.   
  19.     public partial class DrawLine : System.Windows.Window  
  20.     {  
  21.         //StackPanel mainPanel = new StackPanel();   
  22.         Canvas mainPanel = new Canvas();  
  23.         public DrawLine()  
  24.         {  
  25.             InitializeComponent();  
  26.              
  27.             // Add path shape to the UI.   
  28.               
  29.             this.Content = mainPanel;  
  30.   
  31.             Drawpentagon();  
  32.         }  
  33.   
  34.         ///    
  35.         /// 绘制一组线段   
  36.         ///    
  37.         protected void Drawing()  
  38.         {  
  39.             PathFigure myPathFigure = new PathFigure();  
  40.             myPathFigure.StartPoint = new Point(10, 50);  
  41.   
  42.             LineSegment myLineSegment = new LineSegment();  
  43.             myLineSegment.Point = new Point(200, 70);  
  44.   
  45.             PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();  
  46.             myPathSegmentCollection.Add(myLineSegment);  
  47.   
  48.             myPathFigure.Segments = myPathSegmentCollection;  
  49.   
  50.             PathFigureCollection myPathFigureCollection = new PathFigureCollection();  
  51.             myPathFigureCollection.Add(myPathFigure);  
  52.   
  53.             PathGeometry myPathGeometry = new PathGeometry();  
  54.             myPathGeometry.Figures = myPathFigureCollection;  
  55.               
  56.   
  57.             Path myPath = new Path();  
  58.             myPath.Stroke = Brushes.Black;  
  59.             myPath.StrokeThickness = 1;  
  60.             myPath.Data = myPathGeometry;  
  61.   
  62.             // Add path shape to the UI.   
  63.             StackPanel mainPanel = new StackPanel();  
  64.             mainPanel.Children.Add(myPath);  
  65.             this.Content = mainPanel;  
  66.   
  67.         }  
  68.         ///    
  69.         /// 绘制线段   
  70.         ///    
  71.         protected void DrawingLine(Point startPt,Point endPt)  
  72.         {  
  73.             LineGeometry myLineGeometry = new LineGeometry();  
  74.             myLineGeometry.StartPoint = startPt;  
  75.             myLineGeometry.EndPoint = endPt;  
  76.   
  77.             Path myPath = new Path();  
  78.             myPath.Stroke = Brushes.Black;  
  79.             myPath.StrokeThickness = 1;  
  80.             myPath.Data = myLineGeometry;  
  81.   
  82.             mainPanel.Children.Add(myPath);  
  83.               
  84.         }  
  85.         ///    
  86.         /// 绘制星状线   
  87.         ///    
  88.         protected void DrawingAstroid(Point center,double r)  
  89.         {  
  90.   
  91.             double h1 = r * Math.Sin(18 * Math.PI / 180);  
  92.             double h2 = r * Math.Cos(18*Math.PI/180);  
  93.             double h3 = r * Math.Sin(36 * Math.PI / 180);  
  94.             double h4 = r * Math.Cos(36 * Math.PI / 180); ;  
  95.             Point p1 = new Point(r, 0);  
  96.             Point p2 = new Point(r - h2, r - h1);  
  97.             Point p3 = new Point(r - h3, r + h4);  
  98.             Point p4 = new Point(r + h3, p3.Y);  
  99.             Point p5 = new Point(r + h2, p2.Y);  
  100.             Point[] values = new Point[] { p1, p2, p3, p4, p5 };  
  101.             PathFigureCollection myPathFigureCollection = new PathFigureCollection();  
  102.             PathGeometry myPathGeometry = new PathGeometry();  
  103.               
  104.             for (int i = 0; i < values.Length; i++)  
  105.             {  
  106.                 //DrawingLine(center, values[i]);   
  107.                 PathFigure myPathFigure = new PathFigure();  
  108.                 myPathFigure.StartPoint = center;  
  109.   
  110.                 LineSegment myLineSegment = new LineSegment();  
  111.                 myLineSegment.Point = values[i];  
  112.   
  113.                 PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();  
  114.                 myPathSegmentCollection.Add(myLineSegment);  
  115.   
  116.                 myPathFigure.Segments = myPathSegmentCollection;  
  117.   
  118.                 myPathFigureCollection.Add(myPathFigure);  
  119.             }  
  120.             myPathGeometry.Figures = myPathFigureCollection;  
  121.             Path myPath = new Path();  
  122.             myPath.Stroke = Brushes.Black;  
  123.             myPath.StrokeThickness = 1;  
  124.             myPath.Data = myPathGeometry;  
  125.               
  126.             mainPanel.Children.Add(myPath);  
  127.         }  
  128.   
  129.         ///    
  130.         /// 绘制五角星   
  131.         ///    
  132.         private void Drawpentagon()  
  133.         {  
  134.             Point center = new Point(50, 50);  
  135.             double r = 50;  
  136.             DrawingAstroid(center, r);  
  137.   
  138.             double h1 = r * Math.Sin(18 * Math.PI / 180);  
  139.             double h2 = r * Math.Cos(18 * Math.PI / 180);  
  140.             double h3 = r * Math.Sin(36 * Math.PI / 180);  
  141.             double h4 = r * Math.Cos(36 * Math.PI / 180); ;  
  142.             Point p1 = new Point(r, 0);  
  143.             Point p2 = new Point(r - h2, r - h1);  
  144.             Point p3 = new Point(r - h3, r + h4);  
  145.             Point p4 = new Point(r + h3, p3.Y);  
  146.             Point p5 = new Point(r + h2, p2.Y);  
  147.             Point[] values = new Point[] { p1, p3, p5, p2, p4 };  
  148.             // Create a path to draw a geometry with.   
  149.             Path myPath = new Path();  
  150.             myPath.Stroke = Brushes.Black;  
  151.             myPath.StrokeThickness = 1;  
  152.             StreamGeometry theGeometry = BuildRegularPolygon(values, true, false);  
  153.             // Create a StreamGeometry to use to specify myPath.   
  154.             theGeometry.FillRule = FillRule.EvenOdd;  
  155.   
  156.             // Freeze the geometry (make it unmodifiable)   
  157.             // for additional performance benefits.   
  158.             theGeometry.Freeze();  
  159.   
  160.             // Use the StreamGeometry returned by the BuildRegularPolygon to    
  161.             // specify the shape of the path.   
  162.             myPath.Data = theGeometry;  
  163.   
  164.             // Add path shape to the UI.   
  165.   
  166.             mainPanel.Children.Add(myPath);  
  167.   
  168.         }  
  169.   
  170.         ///    
  171.         /// 绘制连续的线段   
  172.         ///    
  173.         ///    
  174.         ///    
  175.         private StreamGeometry BuildRegularPolygon(Point[] values, bool isClosed,bool isfilled)  
  176.         {  
  177.             // c is the center, r is the radius,   
  178.             // numSides the number of sides, offsetDegree the offset in Degrees.   
  179.             // Do not add the last point.   
  180.   
  181.             StreamGeometry geometry = new StreamGeometry();  
  182.   
  183.             using (StreamGeometryContext ctx = geometry.Open())  
  184.             {  
  185.                 ctx.BeginFigure(values[0], isfilled /* is filled */, isClosed /* is closed */);  
  186.                   
  187.                 for (int i = 1; i < values.Length; i++)  
  188.                 {                      
  189.                     ctx.LineTo(values[i], true /* is stroked */, false /* is smooth join */);  
  190.                 }  
  191.             }  
  192.               
  193.             return geometry;  
  194.   
  195.         }  
  196.   
  197.     }  
  198. }