c# - 如何在钢笔绘制的形状中填充颜色

标签 c# c#-4.0 graphics

我正在用笔用 drawcurve 绘制一个形状。

我需要在该图形中填充颜色,我该怎么做?

这是我的代码:

Pen p1 = new Pen(Color.Red);
Graphics g1 = panel1.CreateGraphics();
g1.DrawCurve(p1, new Point[] { new Point(470, 470), new Point(430, 440), new Point(400, 480), new Point(470, 560), });
Graphics g2 = panel1.CreateGraphics();
g2.DrawCurve(p1, new Point[] { new Point(470, 470), new Point(510, 440), new Point(540, 480), new Point(470, 560), });

我找到了填充路径,但我不知道如何使用它。

最佳答案

使用 GraphicsPath 类。您可以使用 Graphics.FillPath 填充它并在必要时使用 Graphics.DrawPath 绘制轮廓。并确保只在 Paint 事件处理程序中绘制,无论您使用 CreateGraphics() 绘制什么都不会在面板自行重绘时持续很长时间。

using System.Drawing.Drawing2D;
...
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            panelPath = new GraphicsPath();
            panelPath.AddCurve(new Point[] { new Point(470, 470), new Point(430, 440), new Point(400, 480), new Point(470, 560), });
            panelPath.AddCurve(new Point[] { new Point(470, 470), new Point(510, 440), new Point(540, 480), new Point(470, 560), });
            panel1.Paint += new PaintEventHandler(panel1_Paint);
        }

        void panel1_Paint(object sender, PaintEventArgs e) {
            e.Graphics.TranslateTransform(-360, -400);
            e.Graphics.FillPath(Brushes.Green, panelPath);
            e.Graphics.DrawPath(Pens.Red, panelPath);
        }
        GraphicsPath panelPath;
    }

产生:

enter image description here

关于c# - 如何在钢笔绘制的形状中填充颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10773655/

相关文章:

python - 使用 Python 图形模块 : is there any way to save the current window as an image?

java - SWT 在外壳上绘制一个 "transparent"矩形

jquery - 如何在asp.net中使用jquery创建 GridView 并单击选择字段列,然后更新、删除和插入新记录

c# - 列表框;所选项目数

java - java 如何知道鼠标点击是否在图形图像上

c# - 如何使用最小起订量模拟 Controller.User

automation - C# Office 2010 自动化

c# - 如何在 WPF 中捕获从子组件到父组件的激活命令?

c# - 如何强制多个接口(interface)包含相同的属性?

c# - C# 中自己的 DSL 的代码解析器