6

我正在生成一堆具有不同大小和位置的 RectangleF 对象。在 GDI+ 中用渐变画笔填充它们的最佳方法是什么?

在 WPF 中,我可以创建一个 LinearGradientBrush,设置起点和终点relative,然后 WPF 将负责其余的工作。

然而,在 GDI+ 中,渐变画笔构造函数需要绝对坐标中的位置,这意味着我必须为每个矩形创建一个画笔,这将是一个非常复杂的操作。

我错过了什么还是那确实是唯一的方法?

4

2 回答 2

1

如果您只想声明一次画笔,您可以在应用渐变之前指定一个变换。请注意,使用转换将覆盖许多可以在LinearGradientBrush.

LinearGradientBrush.Transform 属性 (System.Drawing.Drawing2D)

要修改转换,请调用与所需矩阵运算相对应的画笔对象上的方法。请注意,矩阵运算不可交换,因此顺序很重要。出于您的目的,您可能希望针对矩形的每个再现按以下顺序执行它们:缩放、旋转、偏移/平移。

LinearGradientBrush.ResetTransform 方法@MSDN

LinearGradientBrush.ScaleTransform 方法(Single、Single、MatrixOrder)@MSDN

LinearGradientBrush.RotateTransform 方法(Single,MatrixOrder)@MSDN

LinearGradientBrush.TranslateTransform 方法(Single、Single、MatrixOrder)@MSDN

请注意,系统级绘图工具实际上并不包含渐变画笔的库存定义,因此如果您对制作多个画笔有性能问题,创建大量渐变画笔的成本不应超过 GDI+/System 的开销.Drawing 维护定义渐变和样式所需的数据。您可以根据需要为每个矩形创建一个画笔,而不必深入研究通过变换自定义画笔所需的数学。

刷机功能 (Windows) @ MSDN

这是您可以在 WinForms 应用程序中测试的代码示例。这个应用程序使用 45 度渐变使用渐变画笔绘制瓷砖,缩放到瓷砖的最大尺寸(天真计算)。如果您摆弄值和转换,您可能会发现如果您有非平凡的渐变定义,则不值得使用为所有矩形设置转换的技术。否则,请记住您的转换是在世界级别应用的,在 GDI 世界中,y 轴是颠倒的,而在笛卡尔数学世界中,它是从下到上排序的。这也导致角度顺时针应用,而在三角学中,角度逆时针方向前进,y 轴指向上方的值增加。

using System.Drawing.Drawing2D;

namespace TestMapTransform
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Rectangle rBrush = new Rectangle(0,0,1,1);
            Color startColor = Color.DarkRed;
            Color endColor = Color.White;
            LinearGradientBrush br = new LinearGradientBrush(rBrush, startColor, endColor, LinearGradientMode.Horizontal);

            int wPartitions = 5;
            int hPartitions = 5;

            int w = this.ClientSize.Width;
            w = w - (w % wPartitions) + wPartitions;
            int h = this.ClientSize.Height;
            h = h - (h % hPartitions) + hPartitions;

            for (int hStep = 0; hStep < hPartitions; hStep++)
            {
                int hUnit = h / hPartitions;
                for (int wStep = 0; wStep < wPartitions; wStep++)
                {
                    int wUnit = w / wPartitions;

                    Rectangle rTile = new Rectangle(wUnit * wStep, hUnit * hStep, wUnit, hUnit);

                    if (e.ClipRectangle.IntersectsWith(rTile))
                    {
                        int maxUnit = wUnit > hUnit ? wUnit : hUnit;

                        br.ResetTransform();
                        br.ScaleTransform((float)maxUnit * (float)Math.Sqrt(2d), (float)maxUnit * (float)Math.Sqrt(2d), MatrixOrder.Append);
                        br.RotateTransform(45f, MatrixOrder.Append);
                        br.TranslateTransform(wUnit * wStep, hUnit * hStep, MatrixOrder.Append);

                        e.Graphics.FillRectangle(br, rTile);

                        br.ResetTransform();
                    }
                }
            }
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            this.Invalidate();
        }
    }
}

这是输出的快照:

带有渐变瓷砖绘画的 5x5 表格

于 2013-02-25T20:29:12.700 回答
0

我建议您创建一个像这样的通用方法:

public void Paint_rectangle(object sender, PaintEventArgs e)
    {
        RectangleF r = new RectangleF(0, 0, e.ClipRectangle.Width, e.ClipRectangle.Height);
        if (r.Width > 0 && r.Height > 0)
        {
            Color c1 = Color.LightBlue;
            Color c2 = Color.White;
            Color c3 = Color.LightBlue;

            LinearGradientBrush br = new LinearGradientBrush(r, c1, c3, 90, true);
            ColorBlend cb = new ColorBlend();
            cb.Positions = new[] { 0, (float)0.5, 1 };
            cb.Colors = new[] { c1, c2, c3 };
            br.InterpolationColors = cb;

            // paint
            e.Graphics.FillRectangle(br, r);
        }
    }

然后,对于每个矩形只需调用:

yourrectangleF.Paint += new PaintEventHandler(Paint_rectangle);

如果渐变颜色都相同,则可以使该方法更短。希望有帮助..

于 2013-02-25T18:59:44.557 回答