Example #1
0
        private void DrawRoundedBox(Graphics g, Rectangle bounds, RoundedTypes type, int radius, Color fillColor, Color borderColor)
        {
            GraphicsPath path = new GraphicsPath();
            Pen          pen  = new Pen(borderColor);

            pen.EndCap = pen.StartCap = LineCap.Round;
            switch (type)
            {
            case RoundedTypes.Left:
                /*Left top*/ path.AddArc(bounds.X, bounds.Y, radius, radius, 180, 90);
                /*Right side*/ path.AddLine(bounds.X + bounds.Width, bounds.Y, bounds.X + bounds.Width, bounds.Y + bounds.Height);
                /*Left bottom*/ path.AddArc(bounds.X, bounds.Y + bounds.Height - radius, radius, radius, 90, 90);
                break;

            case RoundedTypes.Top:
                /*Left top*/ path.AddArc(bounds.X, bounds.Y, radius, radius, 180, 90);
                /*Right top*/ path.AddArc(bounds.X + bounds.Width - radius, bounds.Y, radius, radius, 270, 90);
                /*Bottom side*/ path.AddLine(bounds.X + bounds.Width, bounds.Y + bounds.Height, bounds.X, bounds.Y + bounds.Height);
                break;

            case RoundedTypes.Right:
                /*Right top*/ path.AddArc(bounds.X + bounds.Width - radius, bounds.Y, radius, radius, 270, 90);
                /*Right bottom*/ path.AddArc(bounds.X + bounds.Width - radius, bounds.Y + bounds.Height - radius, radius, radius, 0, 90);
                /*Left side*/ path.AddLine(bounds.X, bounds.Y + bounds.Height, bounds.X, bounds.Y);
                break;

            case RoundedTypes.Bottom:
                /*Top side*/ path.AddLine(bounds.X, bounds.Y, bounds.X + bounds.Width, bounds.Y);
                /*Right bottom*/ path.AddArc(bounds.X + bounds.Width - radius, bounds.Y + bounds.Height - radius, radius, radius, 0, 90);
                /*Left bottom*/ path.AddArc(bounds.X, bounds.Y + bounds.Height - radius, radius, radius, 90, 90);
                break;

            case RoundedTypes.Full:
                /*Left top*/ path.AddArc(bounds.X, bounds.Y, radius, radius, 180, 90);
                /*Right top*/ path.AddArc(bounds.X + bounds.Width - radius, bounds.Y, radius, radius, 270, 90);
                /*Right bottom*/ path.AddArc(bounds.X + bounds.Width - radius, bounds.Y + bounds.Height - radius, radius, radius, 0, 90);
                /*Left bottom*/ path.AddArc(bounds.X, bounds.Y + bounds.Height - radius, radius, radius, 90, 90);
                break;

            case RoundedTypes.None:
                /*Top side*/ path.AddLine(bounds.X, bounds.Y, bounds.X + bounds.Width, bounds.Y);
                /*Right side*/ path.AddLine(bounds.X + bounds.Width, bounds.Y, bounds.X + bounds.Width, bounds.Y + bounds.Height);
                /*Bottom side*/ path.AddLine(bounds.X + bounds.Width, bounds.Y + bounds.Height, bounds.X, bounds.Y + bounds.Height);
                /*Left side*/ path.AddLine(bounds.X, bounds.Y + bounds.Height, bounds.X, bounds.Y);
                break;
            }
            path.CloseAllFigures();
            path.Flatten();

            g.SmoothingMode = SmoothingMode.HighQuality;

            g.FillPath(new SolidBrush(fillColor), path);
            g.DrawPath(pen, path);
        }
Example #2
0
        public static Bitmap DrawRoundedBox(Bitmap image, Rectangle bounds,
                                            RoundedTypes type, int radius, float lineWidth,
                                            Color fillColor, Color borderColor)
        {
            Graphics     g    = Graphics.FromImage(image);
            GraphicsPath path = new GraphicsPath();
            Pen          pen  = new Pen(borderColor, lineWidth);

            if (type != RoundedTypes.Transparent)
            {
                pen.EndCap = pen.StartCap = LineCap.Round;
                path.StartFigure();
                switch (type)
                {
                case RoundedTypes.Left:
                    /*Left top*/
                    path.AddArc(bounds.X, bounds.Y, radius, radius, 180, 90);
                    /*Right side*/
                    path.AddLine(bounds.X + bounds.Width, bounds.Y, bounds.X + bounds.Width, bounds.Y + bounds.Height);
                    /*Left bottom*/
                    path.AddArc(bounds.X, bounds.Y + bounds.Height - radius, radius, radius, 90, 90);
                    break;

                case RoundedTypes.Top:
                    /*Left top*/
                    path.AddArc(bounds.X, bounds.Y, radius, radius, 180, 90);
                    /*Right top*/
                    path.AddArc(bounds.X + bounds.Width - radius, bounds.Y, radius, radius, 270, 90);
                    /*Bottom side*/
                    path.AddLine(bounds.X + bounds.Width, bounds.Y + bounds.Height, bounds.X, bounds.Y + bounds.Height);
                    break;

                case RoundedTypes.Right:
                    /*Right top*/
                    path.AddArc(bounds.X + bounds.Width - radius, bounds.Y, radius, radius, 270, 90);
                    /*Right bottom*/
                    path.AddArc(bounds.X + bounds.Width - radius, bounds.Y + bounds.Height - radius, radius, radius, 0, 90);
                    /*Left side*/
                    path.AddLine(bounds.X, bounds.Y + bounds.Height, bounds.X, bounds.Y);
                    break;

                case RoundedTypes.Bottom:
                    /*Top side*/
                    path.AddLine(bounds.X, bounds.Y, bounds.X + bounds.Width, bounds.Y);
                    /*Right bottom*/
                    path.AddArc(bounds.X + bounds.Width - radius, bounds.Y + bounds.Height - radius, radius, radius, 0, 90);
                    /*Left bottom*/
                    path.AddArc(bounds.X, bounds.Y + bounds.Height - radius, radius, radius, 90, 90);
                    break;

                case RoundedTypes.Full:
                    /*Left top*/
                    path.AddArc(bounds.X, bounds.Y, radius, radius, 180, 90);
                    /*Right top*/
                    path.AddArc(bounds.X + bounds.Width - radius, bounds.Y, radius, radius, 270, 90);
                    /*Right bottom*/
                    path.AddArc(bounds.X + bounds.Width - radius, bounds.Y + bounds.Height - radius, radius, radius, 0, 90);
                    /*Left bottom*/
                    path.AddArc(bounds.X, bounds.Y + bounds.Height - radius, radius, radius, 90, 90);
                    break;

                case RoundedTypes.None:
                    /*Top side*/
                    path.AddLine(bounds.X, bounds.Y, bounds.X + bounds.Width, bounds.Y);
                    /*Right side*/
                    path.AddLine(bounds.X + bounds.Width, bounds.Y, bounds.X + bounds.Width, bounds.Y + bounds.Height);
                    /*Bottom side*/
                    path.AddLine(bounds.X + bounds.Width, bounds.Y + bounds.Height, bounds.X, bounds.Y + bounds.Height);
                    /*Left side*/
                    path.AddLine(bounds.X, bounds.Y + bounds.Height, bounds.X, bounds.Y);
                    break;

                case RoundedTypes.TopLeft:
                    /*Left top*/
                    path.AddArc(bounds.X, bounds.Y, radius, radius, 180, 90);
                    /*Right side*/
                    path.AddLine(bounds.X + bounds.Width, bounds.Y, bounds.X + bounds.Width, bounds.Y + bounds.Height);
                    /*Bottom side*/
                    path.AddLine(bounds.X + bounds.Width, bounds.Y + bounds.Height, bounds.X, bounds.Y + bounds.Height);
                    break;

                case RoundedTypes.TopRight:
                    /*Right top*/
                    path.AddArc(bounds.X + bounds.Width - radius, bounds.Y, radius, radius, 270, 90);
                    /*Right side*/
                    path.AddLine(bounds.X + bounds.Width, bounds.Y + radius, bounds.X + bounds.Width, bounds.Y + bounds.Height);
                    /*Bottom side*/
                    path.AddLine(bounds.X + bounds.Width, bounds.Y + bounds.Height, bounds.X, bounds.Y + bounds.Height);
                    break;

                case RoundedTypes.BottomLeft:
                    /*Top side*/
                    path.AddLine(bounds.X, bounds.Y, bounds.X + bounds.Width, bounds.Y);
                    /*Right side*/
                    path.AddLine(bounds.X + bounds.Width, bounds.Y, bounds.X + bounds.Width, bounds.Y + bounds.Height);
                    /*Left bottom*/
                    path.AddArc(bounds.X, bounds.Y + bounds.Height - radius, radius, radius, 90, 90);
                    break;

                case RoundedTypes.BottomRight:
                    /*Left side*/
                    path.AddLine(bounds.X, bounds.Y, bounds.X, bounds.Y + bounds.Height);
                    /*Bottom side*/
                    path.AddLine(bounds.X, bounds.Y + bounds.Height, bounds.X + bounds.Width - radius, bounds.Y + bounds.Height);
                    /*Right bottom*/
                    path.AddArc(bounds.X + bounds.Width - radius, bounds.Y + bounds.Height - radius, radius, radius, 0, 90);
                    break;
                }
                path.CloseAllFigures();

                g.SmoothingMode = SmoothingMode.HighQuality;

                g.FillPath(new SolidBrush(fillColor), path);
                g.DrawPath(pen, path);
            }
            else
            {
                g.FillRectangle(new SolidBrush(fillColor), bounds);
            }

            return(image);
        } //public void DrawRoundedBox( ...