W
W
WayMax2019-11-14 12:02:55
WPF
WayMax, 2019-11-14 12:02:55

What is the correct way to implement a specific gradient in C#?

Below is the code that creates a line and divides it equally into 2 segments. The first segment has a gradient from Red to orange, the second - from orange to green. But the lengths of these segments can only be specified as a percentage. How to make, for example, red color for the first 50 pixels, and then just start a gradient from red to green or immediately from orange to green on the remaining length of the line?

Line line = new Line();
line.StrokeThickness = 10;
line.X1 = 10;
line.X2 = 10;
line.Y1 = 0;
line.Y2 = 200;
grid.Children.Add(line);

LinearGradientBrush linGrBrush = new LinearGradientBrush(Colors.Red, Colors.Green, 90);
linGrBrush.MappingMode = BrushMappingMode.Absolute;
linGrBrush.StartPoint = new Point(0, line.Y1);
linGrBrush.EndPoint = new Point(0, line.Y2);
linGrBrush.GradientStops.Add(new GradientStop(Colors.Red, 0));
linGrBrush.GradientStops.Add(new GradientStop(Colors.Orange, 0.5));
linGrBrush.GradientStops.Add(new GradientStop(Colors.Green, 1));
line.Stroke = linGrBrush;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2019-11-15
@yarosroman

In the GradientStop constructor, the second argument is just a fractional value to which the gradient will be calculated, to calculate 50 pixels from 200 still in school.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question