Drawing a line that blends from one colour to another

68 views Asked by At

In WPF, is there a way to draw a line (in any orientation) that blends from one colour to another?

For example, I want to draw a line from point A to point B, where A is Red and B is Black. The line could be vertical, horizontal, or any orientation.

I have tried using a LinearGradientBrush, but this requires you to set an angle or Start/End positions. This won't work for me as I need to be able to draw the line at any two points in any orientation and still get it to blend. I could in theory compute the angle (or Start/End positions) for the LinearGradientBrush for each line and update it when ever the points of the line changes, but this wouldn't be ideal and would be an expensive operation for the number of lines I plan on drawing.

RadialGradientBrush also doesn't seem to help.

1

There are 1 answers

1
mm8 On

This following LinearGradientBrush "blends" from red to black regardless of the Width of the Grid element:

<Grid Height="10" Width="100">
    <Grid.Background>
        <LinearGradientBrush>
            <GradientStop Color="Red" />
            <GradientStop Color="Black" Offset="1" />
        </LinearGradientBrush>
    </Grid.Background>
</Grid>