WPF 3-Point Gradient

时间:2017-08-30 20:53:07

标签: c# wpf xaml

Using C# how can I generate a gradient with 3 points:

  • White (left)
  • Blue (right)
  • Black (bottom)

The way I'm doing it now is overlaying 2 gradients.

I'd like to simplify it into 1 gradient if possible.

C#

-like

XAML

$results = $hash.GetEnumerator() |Where-Object {
    -not($_.Value -like '*snagit*')
}

rectangle 1
Gradient White, Blue

rectangle 2
Gradient Black

rectangle 1 + 2
Gradient White, Blue, Black

2 个答案:

答案 0 :(得分:2)

你可以使用不透明蒙版与线性渐变画笔相结合,但它并不完美。

<Rectangle>
    <Rectangle.Fill>
        <LinearGradientBrush  EndPoint="0.5,1" StartPoint="0.5,0" >
                <GradientStop Color="Blue" Offset="0"/>
                <GradientStop Color="Black" Offset="1"/>
            </LinearGradientBrush>
        </Rectangle.Fill>

        <Rectangle.OpacityMask>
            <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                <GradientStop Color="Transparent" Offset="0"/>
                <GradientStop Color="Black" Offset="0.5"/>
            </LinearGradientBrush>
        </Rectangle.OpacityMask>
</Rectangle>

enter image description here

答案 1 :(得分:1)

简单地说,你无法定义二维渐变 最简单的技术是您正在使用的技术:执行具有2个线性渐变的叠加 正如弗雷德里克的回答所暗示的那样,使用不透明蒙版同样有效,但这一切都归结为结合渐变:WPF不允许(仍然)定义2D渐变。