如何绘制在C#中的圆角矩形

如何绘制在C#中的圆角矩形

本文介绍了如何绘制在C#中的圆角矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码做一个圆角矩形。但它只能绘制左上角和rectanlge的右上角,它越行得在下部完成矩形。如何使它完全和填补。我应该什么样的变化?



 公共静态位图DrawRoundedRectangle(位图图像​​,颜色BoxColor,诠释xPosition位置,诠释YPosition,
INT高度,诠释宽度,诠释CornerRadius)
{
位图NewBitmap =新位图(图片,Image.Width,Image.Height);使用
(图形NewGraphics = Graphics.FromImage(NewBitmap))
{使用(笔BoxPen =新笔(BoxColor))
{
使用(GraphicsPath的路径
=新的GraphicsPath())
{
Path.AddLine(xPosition位置+ CornerRadius,YPosition xPosition位置+宽度 - (CornerRadius * 2),YPosition);
Path.AddArc(xPosition位置+宽度 - (CornerRadius * 2),YPosition,CornerRadius * 2,CornerRadius * 2,270,90);
Path.AddLine(xPosition位置+宽度,YPosition + CornerRadius xPosition位置+宽度,YPosition +高度 - (CornerRadius * 2));
Path.AddArc(xPosition位置+宽度 - (CornerRadius * 2),YPosition +高度 - (CornerRadius * 2),CornerRadius * 2,CornerRadius * 2,0,90);
Path.AddLine(xPosition位置+宽度 - (CornerRadius * 2),YPosition +身高xPosition位置+ CornerRadius,YPosition +高);
Path.AddArc(xPosition位置,YPosition +高度 - (CornerRadius * 2),CornerRadius * 2,CornerRadius * 2,90,90);
Path.AddLine(xPosition位置,YPosition +高度 - (CornerRadius * 2),xPosition位置,YPosition + CornerRadius);
Path.AddArc(xPosition位置,YPosition,CornerRadius * 2,CornerRadius * 2,180,90);
Path.CloseFigure();
NewGraphics.DrawPath(BoxPen,路径);
}
}
}
返回NewBitmap;
}


解决方案

 公共静态的GraphicsPath圆角矩形(矩形范围,INT半径)
{
INT直径为半径* 2;
尺寸大小=新的大小(直径,直径);
长方形弧=新的Rectangle(bounds.Location,大小);
GraphicsPath的路径=新的GraphicsPath();

如果(半径== 0)
{
path.AddRectangle(边界);
返回路径;
}

//左上方弧
path.AddArc(弧,180,90);

//右上弧
arc.X = bounds.Right - 直径;
path.AddArc(弧,270,90);

//右下弧
arc.Y = bounds.Bottom - 直径;
path.AddArc(弧,0,90);

//左下弧
arc.X = bounds.Left;
path.AddArc(弧,90,90);

path.CloseFigure();
返回路径;
}

和您可以为图形两个扩展方法键入所以你可以用它们作为通常的绘制... 填充... 形状绘制方法。

 公共静态无效DrawRoundedRectangle(此图形显卡,笔笔,矩形范围,诠释cornerRadius)
{
如果(图形== NULL)
抛出新的ArgumentNullException(图形);
如果(笔== NULL)
抛出新的ArgumentNullException(笔);使用(GraphicsPath的路径=圆角矩形(边界,cornerRadius))
{
graphics.DrawPath(钢笔,路径)

;
}
}

公共静态无效FillRoundedRectangle(此图形显卡,刷刷,矩形范围,诠释cornerRadius)
{
如果(图形== NULL)
抛出新的ArgumentNullException(图形);
如果(刷== NULL)
抛出新的ArgumentNullException(刷);使用(GraphicsPath的路径=圆角矩形(边界,cornerRadius))
{
graphics.FillPath(刷,路径)

;
}
}


I am using this code to make a rounded rectangle. But it only draws upper left and right corners of rectanlge , more it doest not complete the rectangle at lower part. How to make it complete and filled . What changes should I make ?

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;
     }
解决方案
    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;
    }

And you can make two extension methods for the Graphics type so you can use them as the usual Draw... and Fill... shape-drawing methods.

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

这篇关于如何绘制在C#中的圆角矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:27