首页 > 解决方案 > C# 区域平滑问题。路径是平滑的,直到区域设置为路径

问题描述

我附上平滑版本的 ss,然后是丑陋的版本。每当我将区域设置为圆角矩形时,我的表单的背面颜色就会显示在路径像素的后面,从而使平滑的外观变得难看。我怎样才能更好地做到这一点?

尝试制作平滑的圆形形式!

平滑路径

将表单区域设置为路径后,平滑度被表单破坏

我用来绘制矩形的代码

        SolidBrush sb = new SolidBrush(Color.Black);
        Size s = new Size(10, 10);
        Rectangle r = new Rectangle(new Point(0, 0), s);
        r.Width = this.Width;
        r.Height = this.Height;

        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        e.Graphics.SmoothingMode = SmoothingMode.HighQuality;

        SetNewRegion.FillRoundedRectangle(e.Graphics, sb, r, 25);

SetNewRegion 文件函数。

    public static void FillRoundedRectangle(this Graphics graphics, Brush brush, Rectangle bounds, int cornerRadius)
    {
        if (graphics == null)
            throw new ArgumentNullException("graphics");
        if (brush == null)
            throw new ArgumentNullException("brush");

        using (GraphicsPath path = RoundedRect(bounds, cornerRadius))
        {
            graphics.FillPath(brush, path);
            
        }
    }

    public static GraphicsPath RoundedRect(Rectangle bounds, int radius)
    {
            int diameter = radius * 2;
            Size size = new Size(diameter, diameter);
            Rectangle arc = new Rectangle(bounds.Location, size);
            GraphicsPath path = new GraphicsPath();

            if (radius == 0)
            {
                path.AddRectangle(bounds);
                return path;
            }

            path.AddArc(arc, 180, 90);
            arc.X = bounds.Right - diameter;
            path.AddArc(arc, 270, 90);
            arc.Y = bounds.Bottom - diameter;
            path.AddArc(arc, 0, 90);
            arc.X = bounds.Left;
            path.AddArc(arc, 90, 90);

            path.CloseFigure();
            return path;
    }

最后,通过使用 FillRoundedRectangle 的更改版本来简单地添加区域,使我能够使用相同的路径但以区域形式构建区域。

this.Region = SetNewRegion.WindowBounds(e.Graphics, sb, r, 25);

标签: c#winformsuser-interfacegraphicsdrawing

解决方案


推荐阅读