C# Graphics 使用 Pen 绘制光滑贝塞尔曲线详解

今天早睡
今天早睡
发布时间:2026-10-13| 4人浏览过
原创

本文介绍了在 C# WinForms 应用中利用 System.Drawing.Drawing2D 命名空间下的 Pen 类与 Graphics 对象的配合,结合 SmoothingMode.AntiAlias 模式绘制贝塞尔曲线(Bézier curve)的具体方法。

平滑曲线实现原理

在 GDI+ 绘图体系中,默认线条呈现为锯齿状。若要获得视觉效果更平滑的曲线,需通过以下两个条件协同实现:

  1. 启用抗锯齿:将 Graphics 实例的 SmoothingMode 属性设置为 SmoothingMode.AntiAlias。
  2. 自定义画刷:通过 LinearGradientBrush 配合 ColorBlend 设置渐变色参数,赋值给 Pen,使得曲线本身具备色彩过渡效果。

操作步骤

第1步:引入必要命名空间

确保文件头部包含绘图相关的基础库引用。这是创建 Brush、Pen 以及处理颜色混合的前提。

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

第2步:创建带有渐变色的画笔 (Pen)

本例演示了如何构建一个三色渐变画笔,使其在曲线上呈现由红到绿再到蓝的效果。具体代码逻辑如下:

  1. 初始化 LinearGradientBrush,指定矩形区域及方向(角度 100 度)。
  2. 构建 ColorBlend 对象,设定三个关键颜色(红、绿、蓝)及其对应的位置索引(0, 0.5, 1)。
  3. 将调色板分配给画刷的 InterpolationColors 属性。
  4. 使用此画刷实例化 Pen 对象。
LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, Color.Empty, Color.Empty, 100);
ColorBlend blend = new ColorBlend();
blend.Colors = new Color[] { Color.Red, Color.Green, Color.Blue };
blend.Positions = new float[] { 0, .5f, 1 };
brush.InterpolationColors = blend;
Pen pen5 = new Pen(brush); // 创建具有渐变效果的画笔

第3步:设置平滑模式并执行绘制

获取当前窗体的 Graphics 上下文后,开启抗锯齿功能,并通过 DrawCurve 方法传入预设的点集完成曲线绘制。

  • 入口对象:this.CreateGraphics() 或重写 OnPaint 中的 e.Graphics。
  • 关键设置:g.SmoothingMode = SmoothingMode.AntiAlias;
  • 绘制动作:g.DrawCurve(pen5, points);
Point[] p = new Point[] {
    new Point(0, 0),
    new Point(100, 100),
    new Point(50, 100),
    new Point(200, 100)
};

g5.SmoothingMode = SmoothingMode.AntiAlias; // 启用抗锯齿
g5.DrawCurve(pen5, p); // 绘制平滑曲线

注意事项与完整结构

在实际项目中,直接调用 CreateGraphics() 属于临时绘图。若需在窗体移动、重绘后依然保持曲线不消失,应将上述绘图逻辑封装并重写窗体的 OnPaint 事件,或使用双缓冲技术减少闪烁。

以下是整合后的完整类结构参考:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.DoubleBuffered = true; // 建议开启双缓冲
    }

    private void button1_Click(object sender, EventArgs e)
    {
        using (Graphics g5 = this.CreateGraphics())
        using (Pen pen5 = CreateGradientPen())
        {
            g5.SmoothingMode = SmoothingMode.AntiAlias;
            Point[] p = new Point[] {
                new Point(0, 0), new Point(100, 100), 
                new Point(50, 100), new Point(200, 100)
            };
            g5.DrawCurve(pen5, p);
        }
    }

    private Pen CreateGradientPen()
    {
        LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, Color.Empty, Color.Empty, 100);
        ColorBlend blend = new ColorBlend();
        blend.Colors = new Color[] { Color.Red, Color.Green, Color.Blue };
        blend.Positions = new float[] { 0, .5f, 1 };
        brush.InterpolationColors = blend;
        return new Pen(brush);
    }
}

总结

通过结合 LinearGradientBrush 与 SmoothingMode.AntiAlias,开发者可以显著改善 C# 应用程序中矢量图形的视觉质感。以上就是C#画笔Pen绘制光滑模式曲线的方法详细内容,更多关于C#图形编程的资料请关注本站其它相关文章!

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。