Effective way to draw gradient rectangles

I create a bunch of RectangleF objects with different sizes and positions. What would be the best way to fill them with a Brush gradient in GDI +?

In WPF, I can create a LinearGradientBrush, set the start and end points relative, and WPF takes care of the rest.

However, in GDI +, the gradient brush constructor requires position in absolute coordinates, which means that I have to create a brush for each rectangle, which would be a very difficult operation.

Am I missing something or is this really the only way?

+5
source share
2 answers

, , . , , LinearGradientBrush.

LinearGradientBrush.Transform(System.Drawing.Drawing2D)

, , . , , . , , : , , /.

LinearGradientBrush.ResetTransform Method @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 Form with Gradient-Tile Painting

+1

, :

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

, . , .

0

All Articles