就是上图这样的效果,圆形渐变、对称渐变和菱形实现都比较简单。这种就是不会,那位有做过啊?

解决方案 »

  1.   

    http://www.myfirm.cn/News/dotNetGUIAPI/20080319073617439.htmlPathGradientBrush是渐变色填充工具,对于复杂的路径(如多边形和不规则形状),使用PathGradientBrush来填充颜色是最合适的,因为它的填充的对象是闭合的路径,这可以是很多种图形,当前,也可以使用它来填充一个长方形。只要闭合的路径表示的是长方形,它就可以做到。再加上渐变的,真是GUI编程中的秘密武器。效果图
    绘制这个图形,要设计8个顶点,然后计算出这些顶点的坐标,使用一个数组保存起来,将顶点集合作为参数传递给PathGradientBrush对象,再设置对象的中点,中心颜色和各顶点关联的颜色,根据顶点颜色与中点颜色,形成过渡效果。 源代码
    PathGradientBrush 是System.Drawing.Drawing2D下面的类,用使用它,你必须先引用该命名空间。如using System.Drawing.Drawing2D;下面的代码做了这样的事情,设置8个点的坐标,当然,你可以先在草稿在设计一下要画的图形,再计算出这几个点的坐标。下一步是创建PathGradientBrush对象并传入点坐标的集合,PathGradientBrush对象一般有三个很必要的属性要设置,CenterPoint闭合路径的中点,CenterColor中点颜色,SurroundColors各个顶点的颜色,如果设置的颜色数量多于顶点数,就会出错,小于则没有问题,最后一个颜色将为剩下的顶点关联的颜色。每个顶点关联一个颜色,中点也关联一个颜色,这样就可以做颜色的过渡效果从而达到渐变。
    如果你想设计的图形是一个,而不是分离的,如,上面可以是一个八边形,只要将_pointfs[4]改为_pointfs[7], _pointfs[5]改为_pointfs[6],如是类推。
            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                //绘画一个八边形
                PointF[] _pointfs = new PointF[8];            _pointfs[0] = new PointF(100, 10);
                _pointfs[1] = new PointF(50, 40);
                _pointfs[2] = new PointF(50, 70);
                _pointfs[3] = new PointF(100, 100);            _pointfs[4] = new PointF(150, 10);            
                _pointfs[5] = new PointF(200, 40);
                _pointfs[6] = new PointF(200, 70);
                _pointfs[7] = new PointF(150, 100);
                
                Graphics g = e.Graphics;
                //设置渐变色填充画笔
                PathGradientBrush br = new PathGradientBrush(_pointfs);
                //设置渐变路径中点
                br.CenterPoint = new PointF((_pointfs[0].X + _pointfs[4].X) / 2, (_pointfs[0].Y + _pointfs[3].Y) / 2);
                //设置路径渐变的中心处的颜色
                br.CenterColor = Color.White;
                //设置与PathGradientBrush 填充的路径中的点相对应的颜色的数组
                br.SurroundColors = new Color[] {
                        Color.Pink, Color.Blue, Color.Pink, Color.Blue,
                        Color.Pink, Color.Pink, Color.Blue, Color.Pink
                    };
                //填充中心区域
                g.FillPolygon(br, _pointfs);
                //绘制边框
                g.DrawPolygon(Pens.White, _pointfs);
                br.Dispose();
            }出处:小作坊网ChakMan原创 
      

  2.   

    Brush这样的类在
    System.Drawing.Drawing2D.PathGradientBrush;
    System.Drawing.Drawing2D.LinearGradientBrush;
    System.Drawing.TextureBrush;
               
    可以实现你想要的效果 
      

  3.   

    个人的建议, 这个还是混搭LinearGradientBrush比较好。