Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question