我如何画使用GDI +的环(圆环)? [英] How do I draw an annulus (doughnut) using GDI+?

查看:782
本文介绍了我如何画使用GDI +的环(圆环)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想画一个(环厚度)具有透明的孔,并在C#中的梯度边缘很少成功。有没有人对如何做到这一点有什么建议?

I have been trying to draw an annulus (ring with thickness) with a transparent hole and a gradient rim in C# with very little success. Does anyone have any suggestions on how to do this?

下面是一个很好的<一个href="http://www.$c$cproject.com/KB/miscctrl/ColorBlender.aspx?fid=1524852&df=90&mpp=25&noise=3&sort=Position&view=Quick"相对=nofollow>混合工具

下面是最终的结果 - 由于BlueMonkMN

Here's the Final result - thanks to 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);

    }

推荐答案

这是我在滚动游戏开发成功了套件

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();

我编辑原来SGDK code这个样本,因为本来我是不是足够聪明,缩放梯度排除孔,但现在我想我:)。

如果您更愿意看到这样的梯度:

然后改变混合code看起来是这样的:

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:

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;

这篇关于我如何画使用GDI +的环(圆环)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆