Click here to Skip to main content
15,885,782 members
Articles / Multimedia / GDI+
Tip/Trick

Beveled Panel with Shadow Effect

Rate me:
Please Sign up or sign in to vote.
4.84/5 (12 votes)
8 Aug 2017CPOL1 min read 23.8K   2.8K   10   8
A panel that has rounded borders, gradients, edges/bevels and a shadow

Image 1

Introduction

By applying bevels, you can add depth to flat objects, it simulates the nicety of a real beveled shape. This article briefly explains how to design and implement the ability to paint the bevel onto a panel and how to apply the soft shadow for this control.

Background

The bevel effect is the simulation of two layers. The bottom layer should contain the shape, which will be split diagonally. Each half has to be filled with light and dark color. This will create edges effect. The upper level should create shape with the smaller size for the panel main area. The pictures below give detailed explanation.

Sample Image - maximum width is 600 pixels

Implementation

The bottom shape in the given control was drawn as a rounded rectangle with linear gradient filling. To split the area by diagonal, component uses Blend function.

C#
private void DrawEdges(Graphics g, ref Rectangle edgeRect)
{
    Rectangle lgbRect = edgeRect;
    lgbRect.Inflate(1, 1);

    var edgeBlend = new Blend();
    switch (Style)
    {
        case BevelStyle.Lowered:
            edgeBlend.Positions = new float[] { 0.0f, .49f, .52f, 1.0f };
            edgeBlend.Factors = new float[] { .0f, .6f, .99f, 1f };


            break;
        case BevelStyle.Raised:
            edgeBlend.Positions = new float[] { 0.0f, .45f, .51f, 1.0f };
            edgeBlend.Factors = new float[] { .0f, .0f, .2f, 1f };
            break;
    }

    using (var edgeBrush = new LinearGradientBrush(lgbRect,
                                        edgeColor1,
                                        edgeColor2,
                                        LinearGradientMode.ForwardDiagonal))
    {
        edgeBrush.Blend = edgeBlend;
        RoundedRectangle.DrawFilledRoundedRectangle(g, edgeBrush, edgeRect, _rectRadius);
    }
}

The top shape is the simple rounded rectangle.

C#
private void DrawPanelStyled(Graphics g, Rectangle rect)
{
    using (Brush pgb = new LinearGradientBrush(rect, _startColor, _endColor,
        (LinearGradientMode)this.BackgroundGradientMode))
    {
        RoundedRectangle.DrawFilledRoundedRectangle(g, pgb, rect, _rectRadius);
    }
}

The basic idea for the soft shadow effect was described in this article.

This control uses PathGradientBrush with transparent outline color.

C#
using (PathGradientBrush shadowBrush = new PathGradientBrush(path))
{
    shadowBrush.CenterPoint = new PointF(rect.Width / 2,
        rect.Height / 2);

    // Set the color along the entire boundary 
    Color[] color = { Color.Transparent };
    shadowBrush.SurroundColors = color;

    // Set the center color 
    shadowBrush.CenterColor = _shadowColor;
    graphics.FillPath(shadowBrush, path);

    shadowBrush.FocusScales = new PointF(0.95f, 0.85f);
    graphics.FillPath(shadowBrush, path);

}

The described technique gives control with the following features:

  • Bevel Effect
  • Rounded corners
  • Gradients
  • Soft shadow
  • Edge thickness
  • Shadow depth

History

  • 8th August, 2017: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Other Other
.NET, Web developer
MSc in Computer Science

Comments and Discussions

 
GeneralMy vote of 5 Pin
User 1522683322-Nov-21 0:55
User 1522683322-Nov-21 0:55 
QuestionIf only this provided semi-transparency Pin
tom kefauver26-Dec-19 8:35
tom kefauver26-Dec-19 8:35 
QuestionThis helps me a lot ! Pin
Sergio1712-Oct-19 2:43
Sergio1712-Oct-19 2:43 
QuestionThanks! Pin
Ignacioooooo20-Jun-19 13:19
Ignacioooooo20-Jun-19 13:19 
Questioncomment Pin
avisal10-Aug-17 4:38
professionalavisal10-Aug-17 4:38 
QuestionGood useful trick Pin
Member 1086218010-Aug-17 3:35
Member 1086218010-Aug-17 3:35 
QuestionOK I Guess, But... Pin
Kevin Marois9-Aug-17 10:52
professionalKevin Marois9-Aug-17 10:52 
AnswerRe: OK I Guess, But... Pin
practicalDeveloper9-Aug-17 19:17
practicalDeveloper9-Aug-17 19:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.