logo
CSharp_Graphics

Указание точек на границе

----------

// Construct a path gradient brush based on an array of points.

PointF[] ptsF = {

new PointF(0, 0),

new PointF(160, 0),

new PointF(160, 200),

new PointF(80, 150),

new PointF(0, 200)};

PathGradientBrush pBrush = new PathGradientBrush(ptsF);

// An array of five points was used to construct the path gradient

// brush. Set the color of each point in that array.

Color[] colors = {

Color.FromArgb(255, 255, 0, 0), // (0, 0) red

Color.FromArgb(255, 0, 255, 0), // (160, 0) green

Color.FromArgb(255, 0, 255, 0), // (160, 200) green

Color.FromArgb(255, 0, 0, 255), // (80, 150) blue

Color.FromArgb(255, 255, 0, 0)}; // (0, 200) red

pBrush.SurroundColors = colors;

// Set the center color to white.

pBrush.CenterColor = Color.White;

// Use the path gradient brush to fill a rectangle.

e.Graphics.FillRectangle(pBrush, new Rectangle(0, 0, 160, 200));

----------

To customize a path gradient

The following example creates a path gradient brush based on an elliptical path. The code sets the boundary color to blue, sets the center color to aqua, and then uses the path gradient brush to fill the elliptical path.

Next, the code sets the focus scales of the path gradient brush. The x focus scale is set to 0.3, and the y focus scale is set to 0.8. The code calls the TranslateTransform method of a Graphics object so that the subsequent call to FillPath fills an ellipse that sits to the right of the first ellipse.

To see the effect of the focus scales, imagine a small ellipse that shares its center with the main ellipse. The small (inner) ellipse is the main ellipse scaled (about its center) horizontally by a factor of 0.3 and vertically by a factor of 0.8. As you move from the boundary of the outer ellipse to the boundary of the inner ellipse, the color changes gradually from blue to aqua. As you move from the boundary of the inner ellipse to the shared center, the color remains aqua.

The following illustration shows the output of the following code. The ellipse on the left is aqua only at the center point. The ellipse on the right is aqua everywhere inside the inner path.

// Create a path that consists of a single ellipse.

GraphicsPath path = new GraphicsPath();

path.AddEllipse(0, 0, 200, 100);

// Create a path gradient brush based on the elliptical path.

PathGradientBrush pthGrBrush = new PathGradientBrush(path);

// Set the color along the entire boundary to blue.

Color[] color = { Color.Blue };

pthGrBrush.SurroundColors = color;

// Set the center color to aqua.

pthGrBrush.CenterColor = Color.Aqua;

// Use the path gradient brush to fill the ellipse.

e.Graphics.FillPath(pthGrBrush, path);

// Set the focus scales for the path gradient brush.

pthGrBrush.FocusScales = new PointF(0.3f, 0.8f);

// Use the path gradient brush to fill the ellipse again.

// Show this filled ellipse to the right of the first filled ellipse.

e.Graphics.TranslateTransform(220.0f, 0.0f);

e.Graphics.FillPath(pthGrBrush, path);