首页 > 代码库 > C#WPF 如何绘制几何图形 图示教程 绘制sin曲线 正弦 绘制2D坐标系 有图有代码

C#WPF 如何绘制几何图形 图示教程 绘制sin曲线 正弦 绘制2D坐标系 有图有代码

C#WPF 如何绘制几何图形? 怎么绘制坐标系?

 

这离不开Path(System.Windows.Shapes)和StreamGeometry(System.Windows.Media)类。

 

一、建立WPF工程

技术分享

 

技术分享

 

二、添加代码

技术分享

MainWindow.xaml 中代码

<Window x:Class="WPFDrawingTraning.MainWindow"        xmlns="<a target=_blank href=http://www.mamicode.com/"http://schemas.microsoft.com/winfx/2006/xaml/presentation">http://schemas.microsoft.com/winfx/2006/xaml/presentation">

MainWindow.xaml.cs中代码

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;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.Navigation;using System.Windows.Shapes;namespace WPFDrawingTraning{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    public partial class MainWindow : System.Windows.Window    {        //Canvas mainPanel = new Canvas();        public MainWindow()        {            InitializeComponent();            Drawsin();//绘制2D坐标系和sin曲线                Drawpentagon();        }        /// <summary>        /// 绘制一组线段        /// </summary>        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 = http://www.mamicode.com/myPathGeometry;>
        {            point.X += xypoint.X;            point.Y = xypoint.Y - point.Y;            return point;//显示屏幕坐标系的位置        }    }}

三、页面效果

技术分享

 

C#WPF 如何绘制几何图形 图示教程 绘制sin曲线 正弦 绘制2D坐标系 有图有代码