gpt4 book ai didi

c# - 了解 OpacityMask 的有效区域

转载 作者:太空宇宙 更新时间:2023-11-03 15:01:57 25 4
gpt4 key购买 nike

我正在使用 WPF opacitymask 来“剪切”动画区域的形状。我注意到默认情况下,不透明 mask 仅适用于面板的内容,而不适用于整个面板。

将背景设置为透明是一种可行的解决方案,因为(显然它会强制拉伸(stretch)不透明 mask 。不过,这感觉像是一种解决方法,所以我很好奇是否有一种“预期”的方法可以使不透明 mask 拉伸(stretch)到整个网格。

示例代码和结果见下:

<Window x:Class="Sandbox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="400" Width="800">
<Window.Resources>
<ImageBrush x:Key="SomeAlphaMaskBrush" ImageSource="https://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Dog_silhouette.svg/1034px-Dog_silhouette.svg.png"></ImageBrush>
</Window.Resources>
<Grid Name="Container">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid Name="Container01" OpacityMask="{StaticResource SomeAlphaMaskBrush}" Background="Transparent">
<!-- Container01 has a transparent background and it makes the OpacityMask stretch to the entire Container01 -->
<Rectangle Margin="70" Fill="Black"/>
</Grid>
<Grid Grid.Column="1" Name="Container02" OpacityMask="{StaticResource SomeAlphaMaskBrush}">
<!-- Container02 has no background, the opacitymask is applied only to its content -->
<Rectangle Margin="70" Fill="Black"/>
</Grid>
</Grid>

Resulting output here : Output

最佳答案

您可以使用 ViewportUnits="Absolute"VisualBrush 属性来设置所需的 OpacityMask 矩形:

<ImageBrush x:Key="SomeAlphaMaskBrush" ViewportUnits="Absolute" Viewport="0 0 20 20" ImageSource="https://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Dog_silhouette.svg/1034px-Dog_silhouette.svg.png"/>

关于c# - 了解 OpacityMask 的有效区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45811851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com