温馨提示×

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

c#
小樊
83
2024-09-10 07:43:07
栏目: 编程语言

在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应用程序项目中,并运行该项目。

0