编辑
2025-02-03
C# 应用
00
请注意,本文编写于 92 天前,最后修改于 92 天前,其中某些信息可能已经过时。

目录

摘要
正文

摘要

在C#中,可以通过DrawPath方法来实现绘制路径的操作。DrawPath方法可以接受一个Path对象作为参数,该对象包含了要绘制的路径描述。下面我们来看一下具体的绘制过程。

首先,我们需要定义一个Path对象。Path对象可以通过AddPath方法来创建,也可以通过添加多个Path对象的方式来创建。在添加Path对象时,可以通过AddLine方法来添加直线段,通过AddCurve方法来添加曲线段,通过AddArc方法来添加圆弧等等。通过这些方法,我们可以将一条路径绘制出来。

我们可以使用DrawPath方法来将这个Path对象绘制出来。DrawPath方法可以接受一个Graphics对象作为参数,Graphics对象可以用来设置绘图参数、设置绘图样式等等。通过Graphics对象,我们可以控制绘图的颜色、线型、线宽、填充颜色等等,从而达到绘制出我们所需要的图形的目的。

正文

一个例子

C#
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; //定义一下路径 GraphicsPath graphPath = new GraphicsPath(); graphPath.AddEllipse(10, 10, 200, 100); graphPath.AddRectangle(new Rectangle(10, 10, 200, 100)); //创建笔 Pen blackPen = new Pen(Color.Black, 3); //用笔绘制 e.Graphics.DrawPath(blackPen, graphPath); }

一个例子

C#
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; // 圆角半径 int cRadius = 40; // 要实现 圆角化的 矩形 Rectangle rect = new Rectangle(20, 20, this.Width - cRadius-20, this.Height - cRadius-40); GraphicsPath myPath = new GraphicsPath(); myPath.StartFigure(); myPath.AddArc(new Rectangle(new Point(rect.X, rect.Y), new Size(2 * cRadius, 2 * cRadius)), 180, 90); myPath.AddLine(new Point(rect.X + cRadius, rect.Y), new Point(rect.Right - cRadius, rect.Y)); myPath.AddArc(new Rectangle(new Point(rect.Right - 2 * cRadius, rect.Y), new Size(2 * cRadius, 2 * cRadius)), 270, 90); myPath.AddLine(new Point(rect.Right, rect.Y + cRadius), new Point(rect.Right, rect.Bottom - cRadius)); myPath.AddArc(new Rectangle(new Point(rect.Right - 2 * cRadius, rect.Bottom - 2 * cRadius), new Size(2 * cRadius, 2 * cRadius)), 0, 90); myPath.AddLine(new Point(rect.Right - cRadius, rect.Bottom), new Point(rect.X + cRadius, rect.Bottom)); myPath.AddArc(new Rectangle(new Point(rect.X, rect.Bottom - 2 * cRadius), new Size(2 * cRadius, 2 * cRadius)), 90, 90); myPath.AddLine(new Point(rect.X, rect.Bottom - cRadius), new Point(rect.X, rect.Y + cRadius)); myPath.CloseFigure(); e.Graphics.DrawPath(new Pen(Color.Red, 2), myPath); }

image.png

StartFigure

不闭合当前图形即开始一个新图形。 后面添加到该路径的所有点都被添加到此新图形中。

本文作者:rick

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!