0
使用C#我怎樣可以產生3點的梯度:WPF 3點梯度
- 白色(左)
- 藍(右)
- 黑色(底部)
我現在做的方式是疊加2個漸變。
如果可能,我想將它簡化爲1個漸變。
C#
// White to Blue
LinearGradientBrush WhiteBlue = new LinearGradientBrush();
WhiteBlue.StartPoint = new System.Windows.Point(0, 0);
WhiteBlue.EndPoint = new System.Windows.Point(1, 0);
WhiteBlue.GradientStops.Add(new GradientStop(Color.FromArgb(255, 255, 255, 255), 0)); // white
WhiteBlue.GradientStops.Add(new GradientStop(Color.FromArgb(255, 255, 0, 0), 1)); // blue
rectangle1.Fill = WhiteBlue;
// Transparent to Black
LinearGradientBrush Black = new LinearGradientBrush();
Black.StartPoint = new System.Windows.Point(0, 0);
Black.EndPoint = new System.Windows.Point(0, 1);
Black.GradientStops.Add(new GradientStop(Color.FromArgb(0, 0, 0, 0), 0)); // transparent
Black.GradientStops.Add(new GradientStop(Color.FromArgb(255, 0, 0, 0), 1)); // black
rectangle2.Fill = Black;
XAML
<Rectangle x:Name="rectangle1"
HorizontalAlignment="Left"
Width="255"
Height="255"
VerticalAlignment="Top">
</Rectangle>
<Rectangle x:Name="rectangle2"
HorizontalAlignment="Left"
Width="255"
Height="255"
VerticalAlignment="Top">
</Rectangle>
看起來像你的解決方案已經是一個很好的方法:https://stackoverflow.com/questions/2925172/is-there-a-mergedgradientbrush-in-wpf – Fruchtzwerg
另一種方法是從原始像素數據創建一個位圖和在Image控件中顯示它。或者創建一個WritableBitmap並更新它的Buffer。 – Clemens