Is it possible to retrieve the angle of a LinearGradientBrush in code?

1k views Asked by At

I'm looking at the constructor for the LinearGradientBrush class and I see it has an override which takes a collection of GradientStops and a double as an angle.

When I look at the properties of it, I can't find how to get the angle form the brush once it's been defined.

Is there a way to do this, or am I going to have to write some function which looks at the Start and End points and calculates the angle from those? (Blech - please do not tell me that is my only option...)

1

There are 1 answers

3
AlexD On BEST ANSWER

I can't find how to get the angle form the brush once it's been defined.

According to referencesource.microsoft.com, angle is not stored, but just used to calculate EndPoint:

public LinearGradientBrush(GradientStopCollection gradientStopCollection,
                           double angle) : base (gradientStopCollection)
{
    EndPoint = EndPointFromAngle(angle);
}

private Point EndPointFromAngle(double angle)
{
     // Convert the angle from degrees to radians
     angle = angle * (1.0/180.0) * System.Math.PI;

     return (new Point(System.Math.Cos(angle), System.Math.Sin(angle)));            
}

Getting angle from EndPoint should be straightforward.