如何在椭圆上绘制阴影?

时间:2015-03-23 19:04:42

标签: c# winforms drawing shadow

g.FillEllipse(Pens.Black, ClientRectangle.X, ClientRectangle.Y, 60, 60); 这是我的椭圆代码。 所以我想为它制作透明阴影,如果可能的话,可以调整阴影大小。

1 个答案:

答案 0 :(得分:2)

winforms中没有现成的投影,但在绘制真实的椭圆之前,你可以通过绘制几个半透明的椭圆来获得很好的效果:

enter image description here enter image description here enter image description here

不要让代码欺骗你:投影只能由三行有效创建。

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

}

请注意,对于非黑色颜色,您通常需要使用黑色或灰色或该颜色的深色调。

请注意,您的代码并不像发布的那样有效:您只能使用钢笔绘图或使用画笔填充!