代码之家  ›  专栏  ›  技术社区  ›  user3163300

如何在椭圆上绘制阴影?

  •  0
  • user3163300  · 技术社区  · 11 年前

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

    1 回复  |  直到 11 年前
        1
  •  2
  •   TaW    11 年前

    没有现成的投影子 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);
    
    }
    

    注意,对于非黑色,您通常希望使用黑色或灰色或该颜色的非常暗的色调。。

    请注意,你的代码并不能像发布的那样工作:你只能用钢笔画画或用画笔填充!