繁体   English   中英

如何在C#Winform中用颜色填充弯曲区域?

[英]How to fill curved area with color in C# Winform?

我想在矩形中绘制带有颜色(在我的情况下为黑色)的弯曲区域。

我在OnPaint事件中尝试了多个方法FillPie,FillEllipse,但无法执行,我想像这样绘制,绘制弯曲区域

我尝试了下面的代码。 但这不是我想要的

protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        Graphics gr = e.Graphics;
        int x = 50;
        int y = 50;
        int width = 100;
        int height = 100;
    Rectangle rect = new Rectangle(x, y, width / 2, height / 2);
    gr.FillRectangle(Brushes.Black, rect);
    gr.FillPie(Brushes.White, x, y, width, height, 180, 90);

    using (Pen pen = new Pen(Color.Yellow, 1))
        gr.DrawArc(pen, x, y, width, height, 180, 90);
}

此代码是这样绘制的。 我不想创建额外的矩形。 我的密码

我不确定100%是否是您想要的:

在此处输入图片说明

我通过创建一个四分之一指定矩形的Region来实现这一目标。 然后,我使用GraphicsPath将饼图排除在外。 然后使用Graphics.FillRegion用黑色笔刷填充生成的曲线:

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        Graphics gr = e.Graphics;
        int x = 50;
        int y = 50;
        int width = 100;
        int height = 100;

        Rectangle rect = new Rectangle(x, y, width/ 2, height / 2);
        Region r = new Region(rect);

        GraphicsPath path = new GraphicsPath();
        path.AddPie(x, y, width, height, 180, 90);
        r.Exclude(path);
        gr.FillRegion(Brushes.Black,r);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM