Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp_Graphics.doc
Скачиваний:
20
Добавлен:
16.11.2019
Размер:
3.1 Mб
Скачать

Настройка градиента контура

  • Одним из способов настройки кисти градиента контура является установка ее свойства FocusScales. Это свойство определяет внутренний контур, лежащий внутри основного контура. Цвет центра используется везде в пределах этого внутреннего контура, а не только в центральной точке.

В следующем примере кисть градиента контура строится на основе эллипса. В коде в качестве цвета границы устанавливается синий, центрального цвета — голубой; затем кисть градиента контура используется для заливки эллиптического контура.

Далее в коде устанавливаются коэффициенты масштабирования кисти градиента контура. Коэффициент масштабирования по оси X устанавливается равным 0,3, а коэффициент масштабирования по оси Y — 0,8. Код вызывает метод TranslateTransform объекта Graphics, чтобы при последующем вызове метода FillPath был залит эллипс, расположенный справа от первого эллипса.

Чтобы увидеть эффект применения коэффициентов масштабирования, представьте маленький эллипс, центр которого совпадает с центром основного эллипса. Маленький (внутренний) эллипс представляет собой основной эллипс, масштабированный (относительно своего центра) с коэффициентом 0,3 по горизонтали и с коэффициентом 0,8 по вертикали. По мере движения от границы внешнего эллипса к границе внутреннего эллипса цвет плавно меняется от синего к голубому. При перемещении от границы внутреннего эллипса к общему центру эллипсов цвет остается тем же (голубым).

На следующем рисунке показан результат выполнения следующего кода. Эллипс в левой части рисунка имеет голубой цвет только в своем центре. Эллипс в правой части рисунка содержит голубой цвет везде в пределах внутреннего контура.

---------------

To customize with interpolation

  • Another way to customize a path gradient brush is to specify an array of interpolation colors and an array of interpolation positions.

The following example creates a path gradient brush based on a triangle. The code sets the InterpolationColors property of the path gradient brush to specify an array of interpolation colors (dark green, aqua, blue) and an array of interpolation positions (0, 0.25, 1). As you move from the boundary of the triangle to the center point, the color changes gradually from dark green to aqua and then from aqua to blue. The change from dark green to aqua happens in 25 percent of the distance from dark green to blue.

The following illustration shows the triangle filled with the custom path gradient brush.

// Vertices of the outer triangle

Point[] points = {

new Point(100, 0),

new Point(200, 200),

new Point(0, 200)};

// No GraphicsPath object is created. The PathGradientBrush

// object is constructed directly from the array of points.

PathGradientBrush pthGrBrush = new PathGradientBrush(points);

Color[] colors = {

Color.FromArgb(255, 0, 128, 0), // dark green

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

Color.FromArgb(255, 0, 0, 255)}; // blue

float[] relativePositions = {

0f, // Dark green is at the boundary of the triangle.

0.4f, // Aqua is 40 percent of the way from the boundary

// to the center point.

1.0f}; // Blue is at the center point.

ColorBlend colorBlend = new ColorBlend();

colorBlend.Colors = colors;

colorBlend.Positions = relativePositions;

pthGrBrush.InterpolationColors = colorBlend;

// Fill a rectangle that is larger than the triangle

// specified in the Point array. The portion of the

// rectangle outside the triangle will not be painted.

e.Graphics.FillRectangle(pthGrBrush, 0, 0, 200, 200);