SVG & # 8594; linear gradient wpf

Does anyone know if the equivalent SVG attribute "gradientUnits = userSpaceOnUse" exists in WPF for LinearGradientBrush? I can't seem to find it.

If not, does anyone know how to calculate it (C # or VB.NET)? For example, if I have StartPoint 0,0 and EndPoint 1,1 on a rectangle that is 100x100, the angle is 45 degrees. However, when I change the width or height of the rectangle, for example Width = 150, the axis is no longer equal to 45 degrees. How could I calculate that the 45 degree angle is in a rectangle that is not a square, so it works from the lower left corner to the upper right corner for the middle gradient stop.

+3
source share
2 answers

This works like a charm now in the new Silverlight 4 - setting the angle to 45 degrees in RotateTransform does it for a bounding box instead of a shape. Like this:

  <Rectangle Width="70" Height="50">
    <Rectangle.Fill>
        <LinearGradientBrush  EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FFF70202" Offset="0"/>
            <GradientStop Color="#FFF7F206" Offset="1"/>
            <GradientStop Color="Black" Offset="0.49"/>
            <GradientStop Color="Black" Offset="0.51"/>
            <GradientStop Color="White" Offset="0.5"/>
            <LinearGradientBrush.RelativeTransform>
            <RotateTransform CenterX="0.5" CenterY="0.5" Angle="45"></RotateTransform>
            </LinearGradientBrush.RelativeTransform>
        </LinearGradientBrush>
    </Rectangle.Fill>
+3
source

Set brush MappingMode = BrushMappingMode.Absolute

+2
source

Source: https://habr.com/ru/post/1716412/


All Articles