How to draw a drop shadow on ellipse?

风格不统一 提交于 2019-12-11 18:05:07

问题


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!



来源:https://stackoverflow.com/questions/29218347/how-to-draw-a-drop-shadow-on-ellipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!