我正在尝试向ImageBrush
控件添加一个不透明蒙版。虽然它与纯 xaml 一起使用,但我在后面的 C# 代码中遇到问题。
这是 xaml 代码:
<Grid Name="grid" Background="Gray">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Grid.Column="1" Source="betta.png">
<Image.OpacityMask>
<ImageBrush ImageSource="star.png"
Viewport="0,0,100,100"
ViewportUnits="Absolute"
TileMode="Tile">
</ImageBrush>
</Image.OpacityMask>
</Image>
</Grid>
虽然星形不透明蒙版显示在网格的右列上,但我通过后面的代码添加了另一个图像,但它没有显示:
ImageSource imageSource = new BitmapImage(new Uri("pack://application:,,,/star.png"));
imageSource.Freeze();
ImageBrush imageBrushMask = new ImageBrush(imageSource);
imageBrushMask.Viewport = new Rect(0, 0, 100, 100);
imageBrushMask.ViewboxUnits = BrushMappingMode.Absolute;
imageBrushMask.TileMode = TileMode.Tile;
BitmapImage bitmapImage = new BitmapImage(new Uri("pack://application:,,,/betta.png"));
Image image = new Image();
image.Source = bitmapImage;
image.OpacityMask = imageBrushMask;
grid.Children.Add(image);
但是,如果我将图像画笔单元更改为BrushMappingMode.RelativeToBoundingBox
:
imageBrushMask.Viewport = new Rect(0, 0, 1, 1);
imageBrushMask.ViewboxUnits = BrushMappingMode.RelativeToBoundingBox;
问题:
如何在 C# 代码ImageBrush
中正确设置模式?BrushMappingMode.Absolute
编辑:
感谢@Clemens,正确的代码应该是:
imageBrushMask.ViewportUnits = BrushMappingMode.Absolute;