当前位置:编程学习 > C#/ASP.NET >>

C# PathGradientBrush 填充矩形问题???

如题:
怎么解决当填充矩形的长和宽不一样时,PathGradientBrush 渐变宽度也不一样的问题.
就象PS里加的渐变边框一样.四周都是一样大小的.
[img=http://s19.photo.store.qq.com/http_imgload.cgi?/rurl4_b=aa31412b26b763e186d9507101fbf5272196ab1b86cd3a92cf5a9385c614cceb43e6b826e7e073a0c17073997ec4876fa3ed7c220d58d15be6eb9c03cc290c08cc0cbeee07974aad0c3c37f87d9b0bbe369f069d][/img]
代码如下:


        private void DrawPeiRect(Graphics g, RectangleF rect)
        {
            //生成一个圆角矩形渐变画刷
            PathGradientBrush pb = new PathGradientBrush(GetFilletRect(rect, 20f, 20f, 20f, 20f));

            //从外到内渐变颜色
            Color[] colors = 
            { 
               Color.FromArgb(120, 247, 190, 247), 
               Color.FromArgb(255, 249, 195, 247), 
               Color.FromArgb(255, 249, 238, 247), 
               Color.FromArgb(255, 249, 238, 247), 
            };

            //从内到外渐变位置百分比
            float[] relativePositions = 
            { 
               0f, 
               0.03f,
               0.15f,
               1f, 
            };

            ColorBlend colorBlend = new ColorBlend();
            colorBlend.Colors = colors;
            colorBlend.Positions = relativePositions;

            pb.InterpolationColors = colorBlend;

            //去圆角毛刺
            g.SmoothingMode = SmoothingMode.HighQuality;
          

            g.FillPath(pb, GetFilletRect(rect, 20f, 20f, 20f, 20f));
        }

        private GraphicsPath GetFilletRect(RectangleF r, float r1, float r2, float r3, float r4)
        {
            float x = r.X, y = r.Y, w = r.Width, h = r.Height;
            GraphicsPath rr = new GraphicsPath();
            rr.AddBezier(x, y + r1, x, y, x + r1, y, x + r1, y);
            rr.AddLine(x + r1, y, x + w - r2, y);
            rr.AddBezier(x + w - r2, y, x + w, y, x + w, y + r2, x + w, y + r2);
            rr.AddLine(x + w, y + r2, x + w, y + h - r3);
            rr.AddBezier(x + w, y + h - r3, x + w, y + h, x + w - r3, y + h, x + w - r3, y + h);
            rr.AddLine(x + w - r3, y + h, x + r4, y + h);
            rr.AddBezier(x + r4, y + h, x, y + h, x, y + h - r4, x, y + h - r4);
            rr.AddLine(x, y + h - r4, x, y + r1);
            return rr;
        }
--------------------编程问答-------------------- 重新发一下图把. --------------------编程问答-------------------- --------------------编程问答-------------------- up --------------------编程问答-------------------- 帮顶~ --------------------编程问答-------------------- up --------------------编程问答-------------------- 渐变位置是按百分比的  所以宽度不一样的话  开始渐变的位置肯定不一样
LZ在borderBrush里渐变试试看? --------------------编程问答--------------------
引用 6 楼 Error_Code 的回复:
渐变位置是按百分比的  所以宽度不一样的话  开始渐变的位置肯定不一样 
LZ在borderBrush里渐变试试看?


我知道是按百分比渐变的。就是不知怎样能做的四周都一样的渐变宽度呀。


BorderBrush?? WPF里的吗?

我这是WinForm呀。 --------------------编程问答-------------------- winform可能要margin padding什么的 应该可以让四周宽度一致    不过你这个效果在wpf里很简单 --------------------编程问答-------------------- up --------------------编程问答-------------------- margin padding 是控制 控件的外边距和内边距的,在GDI上没有地。

WPF当像好做了。这个项目要是能用WPF就好了。 --------------------编程问答-------------------- up --------------------编程问答-------------------- 你要在控件上画  能得到控件大小就能让你的渐变padding一下
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,