SVG-> WPF线性渐变

发布于 2024-08-03 12:27:56 字数 262 浏览 3 评论 0原文

有谁知道WPF中的LinearGradientBrush是否有SVG的“gradientUnits=userSpaceOnUse”的等效属性?我好像找不到这个

如果没有,有人知道如何用(C# 或 VB.NET)计算它吗?例如,如果我在 100x100 的矩形上的起点为 0,0,终点为 1,1,则角度为 45 度。但是,当我更改矩形的宽度或高度(例如宽度=150)时,轴不再是 45 度。我怎样才能计算出在不是正方形的矩形中保持 45 度的角度,以便它从左下角延伸到右上角以形成中间渐变停止点。

Does anyone know if there is an equilant attribute of SVG's "gradientUnits=userSpaceOnUse" in WPF for a LinearGradientBrush? I can't seem to find this.

If not, does anyone know about how to calculate it in (C# or VB.NET)? For example if I have a StartPoint of 0,0 and EndPoint of 1,1 on a Rectangle that is 100x100, the angle is 45 degrees. However, when I change either the width or height of the Rectangle, for example Width=150, the axis is no longer at 45 degrees. How could I calculate to keep the angle at 45 degrees in a rectangle that is not a square so that it runs from bottom left to top right corner for a middle gradientstop.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

音盲 2024-08-10 12:27:56

设置画笔MappingMode = BrushMappingMode.Absolute

Set the brush MappingMode = BrushMappingMode.Absolute

一身仙ぐ女味 2024-08-10 12:27:56

这在新的 Silverlight 4 中就像一个魅力 - 在 RotateTransform 中将角度设置为 45 度,这样可以针对边界框而不是形状。像这样:

  <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>

This works like a charm now in the new Silverlight 4 - setting the angle to 45 degrees in RotateTransform does so for the bounding box instead of the 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文