c#如何使用图形路径制作平滑的圆弧区域 [英] c# How to make smooth arc region using graphics path

查看:26
本文介绍了c#如何使用图形路径制作平滑的圆弧区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make a label control with rounded sides. Here is my code in OnPaint() overridden method in myclass inherited from Label.

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);
    }

问题在于它没有平滑侧面的曲线并显示故障.这是控件的屏幕截图:

The problem is that it is not smoothing the curve on the sides and showing the glitches. Here is the screenshot of the control :

推荐答案

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

As stated in the comments, you can't anti-alias the region, so you have to use graphic's FillPath method in order for the drawing to be anti-aliased

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);
        }

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

If you really need to change the region I guess you would have to use a slightly bigger region then the one used for the drawing:

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 这样的事件,但可能会遇到您看到区域边界"的问题.

The second option has the advantage of affecting events like MouseHover but may run into problems of you seeing the region's "border".

这篇关于c#如何使用图形路径制作平滑的圆弧区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆