【发布时间】:2010-10-17 13:10:26
【问题描述】:
我一直试图在 C# 中绘制一个带有透明孔和渐变边缘的 annulus(有厚度的环),但收效甚微。有人对如何做到这一点有任何建议吗?
这是一个不错的Blend Utility
这是最终结果 - 感谢 BlueMonkMN
Rectangle GetSquareRec(double radius, int x, int y)
{
double r = radius;
double side = Math.Sqrt(Math.Pow(r, 2) / 2);
Rectangle rec = new Rectangle(x - ((int)side), y - ((int)side), (int)(side * 2) + x, (int)(side * 2) + y);
return rec;
}
void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics gTarget = e.Graphics;
gTarget.SmoothingMode = SmoothingMode.AntiAlias;
GraphicsPath pTemp = new GraphicsPath();
Rectangle r = GetSquareRec(200, 225, 225);
pTemp.AddEllipse(r);
pTemp.AddEllipse(GetSquareRec(50, 225, 225));
Color[] colors = new Color[5];
colors[0] = Color.FromArgb(192, 192, 192);
colors[1] = Color.FromArgb(105, 0, 0);
colors[2] = Color.FromArgb(169, 169, 169);
colors[3] = Color.FromArgb(0, 0, 0);
colors[4] = Color.FromArgb(0, 0, 0);
float[] positions = new float[5];
positions[0] = 0f;
positions[1] = 0.1f;
positions[2] = 0.35f;
positions[3] = 0.5f;
positions[4] = 1f;
ColorBlend Cb = new ColorBlend();
Cb.Colors = colors;
Cb.Positions = positions;
PathGradientBrush pgb = new PathGradientBrush(pTemp);
pgb.InterpolationColors = Cb;
pgb.CenterPoint = new PointF(r.X + (r.Width / 2), r.Y + (r.Height / 2));
gTarget.FillPath(pgb, pTemp);
}
【问题讨论】:
-
你用的是什么绘图框架? GDI、WPF、XNA...?
-
GDI 大概(drawing2d 标签说的)
-
是的,我想是的,虽然我只是想检查一下。使用 GDI 绘制 3D 形状肯定会很痛苦……? (当然,除非您实际上是指环形 [en.wikipedia.org/wiki/Annulus_(mathematics)] 而不是环形,这基本上是 2D 等价物。)
-
我已经继续并编辑了这个问题以引用一个环,这就是我认为你的意思。如果不是,请道歉 - 只是回滚。
-
我对“pgb.InterpolationColors = Cb;”这一行感到惊讶。不需要设置 Blend 属性而不是 InterpolationColors 属性吗?
标签: c# .net gdi+ shapes drawing2d