如何在椭圆上绘制阴影? [英] How to draw a drop shadow on ellipse?
问题描述
g.FillEllipse(Pens.Black,ClientRectangle.X,ClientRectangle.Y,60,60);
这是我的椭圆代码。
因此,我想为其制作透明阴影,并尽可能调整阴影大小。
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.
推荐答案
没有 winforms
中的现成的投影,但是您可以通过在绘制真实的椭圆之前画一些半透明的椭圆来获得不错的效果:
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!
这篇关于如何在椭圆上绘制阴影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!