C# 如何使用GDI+;绘制环空(甜甜圈);?

C# 如何使用GDI+;绘制环空(甜甜圈);?,c#,.net,gdi+,shapes,drawing2d,C#,.net,Gdi+,Shapes,Drawing2d,我一直在尝试画一个带有透明孔和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 Rec

我一直在尝试画一个带有透明孔和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);

    }

这是我在


(来源:)
我编辑了这个示例的原始SGDK代码,因为最初我不够聪明,无法缩放渐变以排除洞,但现在我想我是:)。 如果您希望看到这样的渐变:
(来源:)
然后将混合代码更改为如下所示:

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

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

您使用的是什么绘图框架?GDI,WPF,XNA…?GDI可能(drawing2d标签上写着)是的,我想是的,尽管我只是想检查一下。当然,使用GDI绘制3D形状将是相当痛苦的。。。?(当然,除非你实际上指的是一个环(而不是一个环面,它基本上是2D等价物。)我已经继续编辑了这个问题,指的是一个环,这就是我认为你的意思。如果不是的话,我道歉——只是回滚。我对这行字感到惊讶,它的内容是“pgb.InterpolationColors=Cb.您不需要设置混合属性而不是插值颜色属性吗?
Blend blend = new Blend();
blend.Factors = new float[] { 0, 1, 0, 0 };
blend.Positions = new float[] { 0, 0.25F, .5F, 1 };
pgb.Blend = blend;