How to draw a drop shadow on ellipse?

2019-09-20 03:17发布

问题:

g.FillEllipse(Pens.Black, ClientRectangle.X, ClientRectangle.Y, 60, 60); This is my code of ellipse. So I want to make transparent shadow for it, with adjustable size of shadow if possible.

回答1:

There is no ready-made drop shadow in winforms but you can get a nice effect by drawing a few semitransparent ellipses before drawing the real one:

Don't let the code fool you: The drop shadow is effectively created by only three lines.

private void panel1_Paint_1(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    g.SmoothingMode = SmoothingMode.AntiAlias;
    Color color = Color.Blue;
    Color shadow = Color.FromArgb(255, 16, 16, 16);

    for (int i = 0; i < 8; i++ )
        using (SolidBrush brush = new SolidBrush(Color.FromArgb(80 - i * 10, shadow)))
        { g.FillEllipse(brush, panel1.ClientRectangle.X + i*2, 
                               panel1.ClientRectangle.Y + i, 60, 60); }
    using (SolidBrush brush = new SolidBrush(color))
        g.FillEllipse(brush, panel1.ClientRectangle.X, panel1.ClientRectangle.Y, 60, 60);

    // move to the right to use the same coordinates again for the drawn shape
    g.TranslateTransform(80, 0);

    for (int i = 0; i < 8; i++ )
        using (Pen pen = new Pen(Color.FromArgb(80 - i * 10, shadow), 2.5f))
        { g.DrawEllipse(pen, panel1.ClientRectangle.X + i * 1.25f,
                             panel1.ClientRectangle.Y + i, 60, 60); }
    using (Pen pen = new Pen(color))
        g.DrawEllipse(pen, panel1.ClientRectangle.X, panel1.ClientRectangle.Y, 60, 60);

}

Note that for non-black colors you will usually want to use either black or gray or a very dark hue of that color..

Note that your code doesn't work as posted: You can only draw with a pen or fill with a brush!