如何使用GDI+绘制一个环形(甜甜圈)?

4

我一直在尝试用C#绘制一个带有透明孔和渐变边缘的环形图案,但成功率很低。有人有关于如何做到这一点的建议吗?

这里有一个不错的混合工具

这是最终结果——感谢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);

    }

http://www.freeimagehosting.net/uploads/th.515733e62e.jpg


你使用的绘图框架是什么?GDI、WPF、XNA...? - Noldorin
GDI 可能(drawing2d 标签表明它) - Mehrdad Afshari
是的,我也这么认为,但我只是想确认一下。使用GDI绘制3D图形肯定会非常痛苦,对吧?(除非您实际上指的是环体[http://en.wikipedia.org/wiki/Annulus_(mathematics)]而不是一个环面,环体基本上是二维等效物。) - Noldorin
我已经修改了问题,将其引用为圆环,这是我认为你的意思。如果不是,请原谅 - 只需回滚即可。 - Noldorin
我对这行代码"pgb.InterpolationColors = Cb;"感到惊讶。难道你不应该设置Blend属性而不是InterpolationColors属性吗? - BlueMonkMN
我想说的是,我正在使用ColorBlend而不是Blend类。我无法将ColorBlend分配给PathGradiantBrush.Blend——类型错误。 - Brad
2个回答

7
这是我在Scrolling Game Development Kit中的做法:
pTemp = new GraphicsPath();
pTemp.AddEllipse(Start.X, Start.Y, End.X - Start.X, End.Y - Start.Y);
pTemp.AddEllipse((Start.X * 3 + End.X) / 4f,
                 (Start.Y * 3 + End.Y) / 4f,
                 (End.X - Start.X) / 2f,
                 (End.Y - Start.Y) / 2f);
PathGradientBrush pgb = new PathGradientBrush(pTemp);
Blend b = new Blend();
b.Factors = new float[] { 0, 1, 1 };
b.Positions = new float[] { 0, .5F, 1 };
pgb.Blend = b;
pgb.CenterColor = ((SolidBrush)CurrentBrush).Color;
pgb.SurroundColors = new Color[] {CurrentPen.Color};
gTarget.FillPath(pgb, pTemp);
pgb.Dispose();
pTemp.Dispose();

annulus screenshot
(source: enigmadream.com)

I edited the original SGDK code for this sample because originally I wasn't smart enough to scale the gradient to exclude the hole, but now I guess I am :).

If you would rather see the gradient like this:

alt text
(source: enigmadream.com)

Then change the blend code to look like this:

Blend blend = new Blend();
blend.Factors = new float[] { 0, 1, 0, 0 };
blend.Positions = new float[] { 0, 0.25F, .5F, 1 };
pgb.Blend = blend;


0

您可以使用两个调用Graphics.DrawArc组合,一次绘制环形的顶部和底部或左侧和右侧部分,逐部分地绘制。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接