1

I'm trying to create a line with a 'barber shop pole' gradient effect in C# (WPF). The code below works, but when I move the line the gradient becomes distorted. Is there a way to move/rotate the line object and have the gradient's angle remain stationary relative to the line itself.

// Create a linear gradient brush 
LinearGradientBrush redWhiteStripes = new LinearGradientBrush();
redWhiteStripes.StartPoint = new Point(0, 0);
redWhiteStripes.EndPoint = new Point(1, 1);
redWhiteStripes.SpreadMethod = GradientSpreadMethod.Reflect;

ScaleTransform s = new ScaleTransform();  
s.ScaleX = 0.125;
s.ScaleY = 0.125;

RotateTransform rot = new RotateTransform();
rot.Angle = 20;
rot.CenterX = 0.0625;
rot.CenterY = 0.0625;

TransformGroup tgroup = new TransformGroup();
tgroup.Children.Add(s);
tgroup.Children.Add(rot);

redWhiteStripes.RelativeTransform = tgroup;

// Create and add Gradient stops
GradientStop point1 = new GradientStop();
point1.Color = Colors.DarkRed;
point1.Offset = 0.0;
redWhiteStripes.GradientStops.Add(point1);

// Create and add Gradient stops
GradientStop point2 = new GradientStop();
point2.Color = Colors.DarkRed;
point2.Offset = 0.5;
redWhiteStripes.GradientStops.Add(point2);

// Create and add Gradient stops
GradientStop point3 = new GradientStop();
point3.Color = Colors.White;
point3.Offset = 0.5;
redWhiteStripes.GradientStops.Add(point3);

// Create and add Gradient stops
GradientStop point4 = new GradientStop();
point4.Color = Colors.White;
point4.Offset = 1.0;
redWhiteStripes.GradientStops.Add(point4);

Line l = new Line();
l.Stroke = redWhiteStripes;
l.StrokeThickness = 8;
Harri Bell-Thomas
  • 674
  • 1
  • 7
  • 18
  • 2
    Can you add a picture where problem can be seen and another one which shows what you want? Or prepare [mcve](http://stackoverflow.com/help/mcve) to play with? – Sinatr Dec 10 '15 at 10:22
  • Please be more specific: when you move the line _how_, exactly? The gradient becomes distorted _in what way_, exactly? Please provide a good [mcve] that reliably reproduces your problem, with a precise explanation of what that code does now and how that's different from what you want. – Peter Duniho Dec 11 '15 at 08:00
  • Better late than never! :) I think this may answer the question: https://stackoverflow.com/questions/47515810/keeping-a-rotated-linear-gradient-unrotated-when-shape-is-rotated-in-svg – Todd Main Aug 15 '18 at 05:45

0 Answers0