韩剧之美甲店paris:WPF几何绘图(一)画直线
WPF几何绘图(一)画直线
分类: WPF Drawing系列 2009-02-26 11:40 5058人阅读 评论(4) 收藏 举报准备工作,WPF的界面这里就不涉及了,完全是代码工作。
首先,我们要做图必须有个容器,如果你要画画必须有纸张一样。这里我们声明一个Canvas作为容器。
代码如下:
[c-sharp] view plaincopy?
- Canvas mainPanel = new Canvas();
- public DrawLine()
- {
- InitializeComponent();
- // Add path shape to the UI.
- this.Content = mainPanel;
- }
总的来说画直线有三种方法:
1。使用LineGeometry,实例代码如下
[c-sharp] view plaincopy?
- ///
- /// 绘制线段
- ///
- protected void DrawingLine(Point startPt,Point endPt)
- {
- LineGeometry myLineGeometry = new LineGeometry();
- myLineGeometry.StartPoint = startPt;
- myLineGeometry.EndPoint = endPt;
- Path myPath = new Path();
- myPath.Stroke = Brushes.Black;
- myPath.StrokeThickness = 1;
- myPath.Data = myLineGeometry;
- mainPanel.Children.Add(myPath);
- }
这个很简单,就不多作解释了。
2。使用 LineSegment
我们使用LineSegment时,因为它不是geometry的扩展类,所以需要一个包装:PathGeometry才能将它付给path坐Data进而呈现在Canvas上。但是麻烦的是PathGeometry不能直接包容segment,它所包容的是PathFigure,所以我们还需一个中间层。msdn的解释是:
PathGeometry 使用的语法比简单的 LineGeometry 使用的语法要详细得多,在本例中使用 LineGeometry 类可能更有效,但是使用 PathGeometry 的详细语法可以创建极其复杂的几何区域。也就是说使用PathGeometry可以轻松构建更复杂的图形。
代码如下:
[c-sharp] view plaincopy?
- // Create a figure that describes a
- // line from (10,20) to (100,130).
- PathFigure myPathFigure = new PathFigure();
- myPathFigure.StartPoint = new Point(10,20);
- myPathFigure.Segments.Add(
- new LineSegment(new Point(100,130),
- true /* IsStroked */ ));
- /// Create a PathGeometry to contain the figure.
- PathGeometry myPathGeometry = new PathGeometry();
- myPathGeometry.Figures.Add(myPathFigure);
- // Display the PathGeometry.
- Path myPath = new Path();
- myPath.Stroke = Brushes.Black;
- myPath.StrokeThickness = 1;
- myPath.Data = myPathGeometry;
3。比较常用的一种方法,用于绘制连续的线段:StreamGeometry 。它与 PathGeometry 类类似,StreamGeometry 定义一个可以包含曲线、弧线和直线的复杂几何形状。与 PathGeometry 不同,StreamGeometry 的内容不支持数据绑定、动画或修改。当您需要描绘复杂的几何图形,但不希望因为支持数据绑定、动画或修改而引入系统开销时,可使用 StreamGeometry。由于它的高效,StreamGeometry 类是描绘装饰物的理想选择。
实例代码如下:
[c-sharp] view plaincopy?
- public StreamGeometryTriangleExample()
- {
- // Create a path to draw a geometry with.
- Path myPath = new Path();
- myPath.Stroke = Brushes.Black;
- myPath.StrokeThickness = 1;
- // Create a StreamGeometry to use to specify myPath.
- StreamGeometry geometry = new StreamGeometry();
- geometry.FillRule = FillRule.EvenOdd;
- // Open a StreamGeometryContext that can be used to describe this StreamGeometry
- // object's contents.
- using (StreamGeometryContext ctx = geometry.Open())
- {
- // Begin the triangle at the point specified. Notice that the shape is set to
- // be closed so only two lines need to be specified below to make the triangle.
- ctx.BeginFigure(new Point(10, 100), true /* is filled */, true /* is closed */);
- // Draw a line to the next specified point.
- ctx.LineTo(new Point(100, 100), true /* is stroked */, false /* is smooth join */);
- // Draw another line to the next specified point.
- ctx.LineTo(new Point(100, 50), true /* is stroked */, false /* is smooth join */);
- }
- // Freeze the geometry (make it unmodifiable)
- // for additional performance benefits.
- geometry.Freeze();
- // Specify the shape (triangle) of the Path using the StreamGeometry.
- myPath.Data = geometry;
- // Add path shape to the UI.
- StackPanel mainPanel = new StackPanel();
- mainPanel.Children.Add(myPath);
- this.Content = mainPanel;
- }
好了方法说完了。以下是我做的一个练习,效果如图。
代码如下:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- namespace WPFDrawingTraning
- {
- ///
- /// Interaction logic for DrawLine.xaml
- ///
- public partial class DrawLine : System.Windows.Window
- {
- //StackPanel mainPanel = new StackPanel();
- Canvas mainPanel = new Canvas();
- public DrawLine()
- {
- InitializeComponent();
- // Add path shape to the UI.
- this.Content = mainPanel;
- Drawpentagon();
- }
- ///
- /// 绘制一组线段
- ///
- protected void Drawing()
- {
- PathFigure myPathFigure = new PathFigure();
- myPathFigure.StartPoint = new Point(10, 50);
- LineSegment myLineSegment = new LineSegment();
- myLineSegment.Point = new Point(200, 70);
- PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
- myPathSegmentCollection.Add(myLineSegment);
- myPathFigure.Segments = myPathSegmentCollection;
- PathFigureCollection myPathFigureCollection = new PathFigureCollection();
- myPathFigureCollection.Add(myPathFigure);
- PathGeometry myPathGeometry = new PathGeometry();
- myPathGeometry.Figures = myPathFigureCollection;
- Path myPath = new Path();
- myPath.Stroke = Brushes.Black;
- myPath.StrokeThickness = 1;
- myPath.Data = myPathGeometry;
- // Add path shape to the UI.
- StackPanel mainPanel = new StackPanel();
- mainPanel.Children.Add(myPath);
- this.Content = mainPanel;
- }
- ///
- /// 绘制线段
- ///
- protected void DrawingLine(Point startPt,Point endPt)
- {
- LineGeometry myLineGeometry = new LineGeometry();
- myLineGeometry.StartPoint = startPt;
- myLineGeometry.EndPoint = endPt;
- Path myPath = new Path();
- myPath.Stroke = Brushes.Black;
- myPath.StrokeThickness = 1;
- myPath.Data = myLineGeometry;
- mainPanel.Children.Add(myPath);
- }
- ///
- /// 绘制星状线
- ///
- protected void DrawingAstroid(Point center,double r)
- {
- double h1 = r * Math.Sin(18 * Math.PI / 180);
- double h2 = r * Math.Cos(18*Math.PI/180);
- double h3 = r * Math.Sin(36 * Math.PI / 180);
- double h4 = r * Math.Cos(36 * Math.PI / 180); ;
- Point p1 = new Point(r, 0);
- Point p2 = new Point(r - h2, r - h1);
- Point p3 = new Point(r - h3, r + h4);
- Point p4 = new Point(r + h3, p3.Y);
- Point p5 = new Point(r + h2, p2.Y);
- Point[] values = new Point[] { p1, p2, p3, p4, p5 };
- PathFigureCollection myPathFigureCollection = new PathFigureCollection();
- PathGeometry myPathGeometry = new PathGeometry();
- for (int i = 0; i < values.Length; i++)
- {
- //DrawingLine(center, values[i]);
- PathFigure myPathFigure = new PathFigure();
- myPathFigure.StartPoint = center;
- LineSegment myLineSegment = new LineSegment();
- myLineSegment.Point = values[i];
- PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
- myPathSegmentCollection.Add(myLineSegment);
- myPathFigure.Segments = myPathSegmentCollection;
- myPathFigureCollection.Add(myPathFigure);
- }
- myPathGeometry.Figures = myPathFigureCollection;
- Path myPath = new Path();
- myPath.Stroke = Brushes.Black;
- myPath.StrokeThickness = 1;
- myPath.Data = myPathGeometry;
- mainPanel.Children.Add(myPath);
- }
- ///
- /// 绘制五角星
- ///
- private void Drawpentagon()
- {
- Point center = new Point(50, 50);
- double r = 50;
- DrawingAstroid(center, r);
- double h1 = r * Math.Sin(18 * Math.PI / 180);
- double h2 = r * Math.Cos(18 * Math.PI / 180);
- double h3 = r * Math.Sin(36 * Math.PI / 180);
- double h4 = r * Math.Cos(36 * Math.PI / 180); ;
- Point p1 = new Point(r, 0);
- Point p2 = new Point(r - h2, r - h1);
- Point p3 = new Point(r - h3, r + h4);
- Point p4 = new Point(r + h3, p3.Y);
- Point p5 = new Point(r + h2, p2.Y);
- Point[] values = new Point[] { p1, p3, p5, p2, p4 };
- // Create a path to draw a geometry with.
- Path myPath = new Path();
- myPath.Stroke = Brushes.Black;
- myPath.StrokeThickness = 1;
- StreamGeometry theGeometry = BuildRegularPolygon(values, true, false);
- // Create a StreamGeometry to use to specify myPath.
- theGeometry.FillRule = FillRule.EvenOdd;
- // Freeze the geometry (make it unmodifiable)
- // for additional performance benefits.
- theGeometry.Freeze();
- // Use the StreamGeometry returned by the BuildRegularPolygon to
- // specify the shape of the path.
- myPath.Data = theGeometry;
- // Add path shape to the UI.
- mainPanel.Children.Add(myPath);
- }
- ///
- /// 绘制连续的线段
- ///
- ///
- ///
- private StreamGeometry BuildRegularPolygon(Point[] values, bool isClosed,bool isfilled)
- {
- // c is the center, r is the radius,
- // numSides the number of sides, offsetDegree the offset in Degrees.
- // Do not add the last point.
- StreamGeometry geometry = new StreamGeometry();
- using (StreamGeometryContext ctx = geometry.Open())
- {
- ctx.BeginFigure(values[0], isfilled /* is filled */, isClosed /* is closed */);
- for (int i = 1; i < values.Length; i++)
- {
- ctx.LineTo(values[i], true /* is stroked */, false /* is smooth join */);
- }
- }
- return geometry;
- }
- }
- }