c# drawing中如何绘制复杂路径

   2024-09-30 6660
核心提示:在C#中,要绘制复杂路径,可以使用System.Drawing命名空间中的GraphicsPath类using System;using System.Drawing;using System.W

在C#中,要绘制复杂路径,可以使用System.Drawing命名空间中的GraphicsPath类

using System;using System.Drawing;using System.Windows.Forms;public class DrawingComplexPath : Form{    public DrawingComplexPath()    {        this.Text = "绘制复杂路径";        this.Size = new Size(600, 400);        this.Paint += new PaintEventHandler(this.DrawingComplexPath_Paint);    }    private void DrawingComplexPath_Paint(object sender, PaintEventArgs e)    {        Graphics g = e.Graphics;        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;        // 创建一个GraphicsPath对象        GraphicsPath path = new GraphicsPath();        // 添加一个圆形到路径        path.AddEllipse(50, 50, 100, 100);        // 添加一个矩形到路径        path.AddRectangle(new RectangleF(150, 50, 100, 100));        // 添加一个三角形到路径        Point[] trianglePoints = new Point[] { new Point(300, 50), new Point(350, 150), new Point(250, 150) };        path.AddPolygon(trianglePoints);        // 添加一个曲线到路径        Point[] curvePoints = new Point[] { new Point(400, 50), new Point(450, 100), new Point(400, 150), new Point(350, 100) };        path.AddCurve(curvePoints);        // 使用指定的颜色和填充模式填充路径        g.FillPath(Brushes.Blue, path);        // 使用指定的颜色和宽度绘制路径        g.DrawPath(new Pen(Color.Red, 2), path);    }    [STAThread]    static void Main()    {        Application.EnableVisualStyles();        Application.SetCompatibleTextRenderingDefault(false);        Application.Run(new DrawingComplexPath());    }}

这个示例将在窗体上绘制一个复杂的路径,包括一个圆形、一个矩形、一个三角形和一个曲线。路径使用蓝色填充,并使用红色绘制边界。要运行此示例,请将代码复制到一个新的C# Windows Forms应用程序项目中,并运行该项目。

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式 | 网站留言    |  赣ICP备2021007278号