如何在c#中绘制圆角矩形

php*_*net 17 c# graphics drawing rectangles winforms

我正在使用此代码来制作圆角矩形.但它只绘制了rectanlge的左上角和右上角,更不能完成下部的矩形.如何使其完整和充实.我应该做些什么改变?

public static Bitmap DrawRoundedRectangle(Bitmap Image, Color BoxColor, int XPosition, int YPosition,
        int Height, int Width, int CornerRadius)
    {
     Bitmap NewBitmap = new Bitmap(Image, Image.Width, Image.Height);
     using (Graphics NewGraphics = Graphics.FromImage(NewBitmap))
    {
        using (Pen BoxPen = new Pen(BoxColor))
        {
            using (GraphicsPath Path = new GraphicsPath())
            {
                   Path.AddLine(XPosition + CornerRadius, YPosition, XPosition + Width - (CornerRadius * 2), YPosition);
                    Path.AddArc(XPosition + Width - (CornerRadius * 2), YPosition, CornerRadius * 2, CornerRadius * 2, 270, 90);
                  Path.AddLine(XPosition + Width, YPosition + CornerRadius, XPosition + Width, YPosition + Height - (CornerRadius * 2));
                    Path.AddArc(XPosition + Width - (CornerRadius * 2), YPosition + Height - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 0, 90);
                 Path.AddLine(XPosition + Width - (CornerRadius * 2), YPosition + Height, XPosition + CornerRadius, YPosition + Height);
                   Path.AddArc(XPosition, YPosition + Height - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 90, 90);
                   Path.AddLine(XPosition, YPosition + Height - (CornerRadius * 2), XPosition, YPosition + CornerRadius);
                    Path.AddArc(XPosition, YPosition, CornerRadius * 2, CornerRadius * 2, 180, 90);
                     Path.CloseFigure();
                     NewGraphics.DrawPath(BoxPen, Path);
                 }
              }
          }
         return NewBitmap;
     }
Run Code Online (Sandbox Code Playgroud)

Gyö*_*zeg 41

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

        // top left arc  
        path.AddArc(arc, 180, 90);

        // top right arc  
        arc.X = bounds.Right - diameter;
        path.AddArc(arc, 270, 90);

        // bottom right arc  
        arc.Y = bounds.Bottom - diameter;
        path.AddArc(arc, 0, 90);

        // bottom left arc 
        arc.X = bounds.Left;
        path.AddArc(arc, 90, 90);

        path.CloseFigure();
        return path;
    }
Run Code Online (Sandbox Code Playgroud)

并且您可以为该Graphics类型创建两个扩展方法,以便您可以将它们用作常规Draw...Fill...形状绘制方法.

    public static void DrawRoundedRectangle(this Graphics graphics, Pen pen, Rectangle bounds, int cornerRadius)
    {
        if (graphics == null)
            throw new ArgumentNullException("graphics");
        if (pen == null)
            throw new ArgumentNullException("pen");

        using (GraphicsPath path = RoundedRect(bounds, cornerRadius))
        {
            graphics.DrawPath(pen, path);
        }
    }

    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);
        }
    }
Run Code Online (Sandbox Code Playgroud)


小智 8

我已经为那些想要不同角落的人修改了 Gy\xc3\xb6rgy K\xc5\x91szeg 的答案。

\n
public static GraphicsPath RoundedRect(Rectangle bounds, int radius1, int radius2, int radius3, int radius4)\n    {\n        int diameter1 = radius1 * 2;\n        int diameter2 = radius2 * 2;\n        int diameter3 = radius3 * 2;\n        int diameter4 = radius4 * 2;\n\n        Rectangle arc1 = new Rectangle(bounds.Location, new Size(diameter1, diameter1));\n        Rectangle arc2 = new Rectangle(bounds.Location, new Size(diameter2, diameter2));\n        Rectangle arc3 = new Rectangle(bounds.Location, new Size(diameter3, diameter3));\n        Rectangle arc4 = new Rectangle(bounds.Location, new Size(diameter4, diameter4));\n        GraphicsPath path = new GraphicsPath();\n\n        // top left arc  \n        if (radius1 == 0)\n        {\n            path.AddLine(arc1.Location, arc1.Location);\n        }\n        else\n        {\n            path.AddArc(arc1, 180, 90);\n        }\n\n        // top right arc  \n        arc2.X = bounds.Right - diameter2;\n        if (radius2 == 0)\n        {\n            path.AddLine(arc2.Location, arc2.Location);\n        }\n        else\n        {\n            path.AddArc(arc2, 270, 90);\n        }\n\n        // bottom right arc  \n\n        arc3.X = bounds.Right - diameter3;\n        arc3.Y = bounds.Bottom - diameter3;\n        if (radius3 == 0)\n        {\n            path.AddLine(arc3.Location, arc3.Location);\n        }\n        else\n        {\n            path.AddArc(arc3, 0, 90);\n        }\n\n        // bottom left arc \n        arc4.X = bounds.Right - diameter4;\n        arc4.Y = bounds.Bottom - diameter4;\n        arc4.X = bounds.Left;\n        if (radius4 == 0)\n        {\n            path.AddLine(arc4.Location, arc4.Location);\n        }\n        else\n        {\n            path.AddArc(arc4, 90, 90);\n        }\n\n        path.CloseFigure();\n        return path;\n    }\n
Run Code Online (Sandbox Code Playgroud)\n