c#如何使用图形路径制作平滑的圆弧区域

标签 c# winforms graphics controls

我正在尝试制作一个带有圆边的标签控件。 这是我在从 Label 继承的类中的 OnPaint() 重写方法中的代码。

protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
        e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
        e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

        LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, GradientColor1, GradientColor2, 90);
        e.Graphics.FillRectangle(brush, new Rectangle(new Point(0, 0), new Size(this.Width, this.Height)));

        using (GraphicsPath gp = new GraphicsPath())
        {
            gp.AddArc(new Rectangle(new Point(0, 0), new Size(this.Height, this.Height)), 90, 180);
            gp.AddLine(new Point(this.Height / 2, 0), new Point(this.Width - (this.Height / 2), 0));
            gp.AddArc(new Rectangle(new Point(this.Width - this.Height, 0), new Size(this.Height, this.Height)), -90, 180);
            gp.CloseFigure();

            this.Region = new Region(gp);
        }

        base.OnPaint(e);
    }

问题是它没有平滑两侧的曲线并显示毛刺。 这是控件的屏幕截图:

enter image description here

最佳答案

如评论中所述,您无法对该区域进行抗锯齿处理,因此您必须使用图形的 FillPath 方法才能对绘图进行抗锯齿处理

protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, Color.Aqua, Color.Blue, 90);

            using (GraphicsPath gp = new GraphicsPath())
            {
                gp.AddArc(new Rectangle(new Point(0, 0), new Size(this.Height, this.Height)), 90, 180);
                gp.AddLine(new Point(this.Height / 2, 0), new Point(this.Width - (this.Height / 2), 0));
                gp.AddArc(new Rectangle(new Point(this.Width - this.Height, 0), new Size(this.Height, this.Height)), -90, 180);
                gp.CloseFigure();

                e.Graphics.FillPath(brush, gp);
            }

            base.OnPaint(e);
        }

如果您真的需要更改区域,我想您必须使用比用于绘图的区域稍大的区域:

protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
        e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
        e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

        LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, Color.Aqua, Color.Blue, 90);

        using (GraphicsPath gp = new GraphicsPath())
        {
            AddRoundedRectangle(gp, new Point(1, 1), new Size(this.Size.Width - 2, Size.Height - 2));
            e.Graphics.FillPath(brush, gp);
        }

        using (GraphicsPath gp = new GraphicsPath())
        {
            AddRoundedRectangle(gp, new Point(0, 0), this.Size);
            this.Region = new Region(gp);
        }

        base.OnPaint(e);
    }

    private void AddRoundedRectangle(GraphicsPath gp, Point upperLeft, Size size)
    {
        gp.AddArc(new Rectangle(upperLeft, new Size(size.Height, size.Height)), 90, 180);
        gp.AddLine(new Point(size.Height / 2 , upperLeft.Y), new Point(size.Width - (size.Height / 2), upperLeft.Y));
        gp.AddArc(new Rectangle(new Point(size.Width - size.Height, upperLeft.Y), new Size(size.Height, size.Height)), -90, 180);
        gp.CloseFigure();
    }

第二个选项的优点是可以影响 MouseHover 等事件,但可能会遇到您看到区域“边界”的问题。

关于c#如何使用图形路径制作平滑的圆弧区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33878184/

相关文章:

c# - 将新工作表插入电子表格文档 OpenXml

c# - 如何检查文本框内容中是否有空格?

c# - C# 的 lambda 表达式中的默认参数值

c# - DataGridView 选择 RowHeader 或 ColumnHeader

c# - 使用 c#.net winforms 从 mysql 数据库中选择 - 如何检查响应?

ios - 打开 GL : convert pan size in screen to world size

silverlight - 重新学习图形编程的最佳位置在哪里

c# - 如何制作自己的图形界面?

c# - "Neighbor function"使用 A* 算法优化解决 8-Puzzle

winforms - PowerShell 脚本返回错误的屏幕分辨率