c# - 如何使用 GDI+ 绘制圆环( donut )?

标签 c# .net gdi+ shapes drawing2d

我一直在尝试画一个 annulus (带厚度的环)在 C# 中带有透明孔和渐变边缘,但收效甚微。有人对如何执行此操作有任何建议吗?

这是一个不错的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);

    }

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

最佳答案

这就是我在 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
(来源:enigmadream.com)

我为此示例编辑了原始 SGDK 代码,因为最初我不够聪明,无法缩放渐变以排除孔洞,但现在我想我是:)。

如果你希望看到这样的渐变:

alt text
(来源:enigmadream.com)

然后将混合代码更改为如下所示:

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

关于c# - 如何使用 GDI+ 绘制圆环( donut )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/716984/

相关文章:

image - 如何使用 GDI+ 绘制 ARGB 位图?

.Net 不一致的字体渲染

c# - 根据 BackColor 反转文本颜色

c# - .NET - 数字的字母数字表示

c# - 如何使用 C# 通过 .NET 获取 PC 中的所有驱动器

C# 打开 DBF 文件

c# - 用于商业目的的 iText/iTextSharp : not recommended?

c# - UWP 约会 : Appointment ID returning empty

c# - C#/.NET 中的可变字符串编辑

.net - 如何使用反射判断对象的类型是否为Nullable <T>?