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

1949idc 2年前 (2024-09-19) 阅读数 411 #编程语言

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

版权声明

本文内容由互联网用户自发贡献,该文观点仅代表作者本人
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

© 2010 首途云安 & 厦门硕顿信息技术有限公司 & 闽ICP备11016866号  增值电信业务经营许可证:B1-20203020 地址:福建厦门思明区嘉禾路297号1806
高新技术企业
软件产品证书
计算机软件著作权
ISO认证
国家3A企业