Q
填充矩形的外部
8
A
回答
5
您也可以用半透明Path
元件重叠图像使用一个CombinedGeometry
它结合一个非常大的外部矩形具有内矩形:
<Grid>
<Image Name="image" Source="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"/>
<Path Fill="#AAFFFFFF">
<Path.Data>
<CombinedGeometry GeometryCombineMode="Xor">
<CombinedGeometry.Geometry1>
<RectangleGeometry Rect="0,0,10000,10000"/>
</CombinedGeometry.Geometry1>
<CombinedGeometry.Geometry2>
<RectangleGeometry x:Name="transparentRect" Rect="150,100,200,100"/>
</CombinedGeometry.Geometry2>
</CombinedGeometry>
</Path.Data>
</Path>
</Grid>
您现在将根据需要以编程方式调整transparentRect
成员的Rect
属性。
+0
您的答案使用C#语法高亮显示。这是不正确的,因为XAML是XML。其中一个明显的问题是元素和属性具有相同的颜色。 – Athari
2
您可以使用OpacityMask和DrawingBrush的组合:
XAML:
<Grid Background="Gray">
<Image Name="image"Source="...">
<Image.OpacityMask>
<DrawingBrush x:Name="mask"/>
</Image.OpacityMask>
</Image>
</Grid>
代码隐藏:
private void UpdateOpactiyMask()
{
Point topLeft = new Point();
Point bottomRight = new Point(image.ActualWidth, image.ActualHeight);
GeometryDrawing left = new GeometryDrawing();
left.Brush = borderBrush;
left.Geometry = new RectangleGeometry(new Rect(topLeft, new Point(SelectedArea.Left, bottomRight.Y)));
GeometryDrawing right = new GeometryDrawing();
right.Brush = borderBrush;
right.Geometry = new RectangleGeometry(new Rect(new Point(SelectedArea.Right, topLeft.Y), bottomRight));
GeometryDrawing top = new GeometryDrawing();
top.Brush = borderBrush;
top.Geometry = new RectangleGeometry(new Rect(new Point(SelectedArea.Left, topLeft.Y), new Point(SelectedArea.Right, SelectedArea.Top)));
GeometryDrawing bottom = new GeometryDrawing();
bottom.Brush = borderBrush;
bottom.Geometry = new RectangleGeometry(new Rect(new Point(SelectedArea.Left, SelectedArea.Bottom), new Point(SelectedArea.Right, bottomRight.Y)));
GeometryDrawing center = new GeometryDrawing();
center.Brush = selectionBrush;
center.Geometry = new RectangleGeometry(SelectedArea);
DrawingGroup drawing = new DrawingGroup();
drawing.Children.Add(left);
drawing.Children.Add(right);
drawing.Children.Add(top);
drawing.Children.Add(bottom);
drawing.Children.Add(center);
mask.Drawing = drawing;
}
SelectedArea是一个矩形。
2
可以使用UIElement.Clip
属性:
<Window x:Class="So17720970_RectangularBoublik.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
SizeToContent="WidthAndHeight">
<Grid Width="500" Height="500">
<Image Source="http://i.stack.imgur.com/Py65S.jpg"/> <!-- image -->
<Rectangle Fill="#AA000000"> <!-- selection -->
<Rectangle.Clip>
<GeometryGroup FillRule="Nonzero"> <!-- selection clip: -->
<RectangleGeometry Rect="0 0 500 200"/> <!-- top -->
<RectangleGeometry Rect="0 0 100 500"/> <!-- left -->
<RectangleGeometry Rect="0 300 500 200"/> <!-- bottom -->
<RectangleGeometry Rect="400 0 100 500"/> <!-- right -->
</GeometryGroup>
</Rectangle.Clip>
</Rectangle>
<Rectangle StrokeThickness="1" Stroke="Black" StrokeDashArray="1 2" SnapsToDevicePixels="True"
Margin="100 200 100 200"/> <!-- "ants" -->
</Grid>
</Window>
1
这是使用OpacityMask的解决方案的变体。而不是在代码中完成,它是在XAML中完成的。此外,它颠倒了逻辑:不是绘制4个边框,而是绘制2个矩形。最后,这个解决方案的重要特性是中央透明矩形的大小与图像大小(而不是绝对像素)有关。您不需要知道实际的图像大小或其拉伸/定位方式(对Stretch =“Uniform”尤其重要)。在这里,我将图像大小(maskRect)指定为1,1,并使用小数作为相对掩码大小和位置(transpRect)。你也可以指定图像尺寸为100,100,并使用掩码的百分比值(或者甚至使用实际的像素值)。
<Grid Background="#FFF4F4F5" >
<Image Name="PhotoImage" Source="...">
<Image.OpacityMask>
<DrawingBrush>
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<RectangleGeometry x:Name="maskRect" Rect="0,0,1,1"/>
</GeometryDrawing.Geometry>
<GeometryDrawing.Brush>
<SolidColorBrush Color="#60000000" />
</GeometryDrawing.Brush>
</GeometryDrawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<RectangleGeometry x:Name="transpRect" Rect=".25,.20,.40,.40"/>
</GeometryDrawing.Geometry>
<GeometryDrawing.Brush>
<SolidColorBrush Color="Black" />
</GeometryDrawing.Brush>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Image.OpacityMask>
</Image>
</Grid>
相关问题
- 1. 在矩形外部填充画布
- 2. 部分填充矩形对象
- 3. Fabric.js - 更改矩形填充
- 4. 矩形渐变填充
- 5. 填充按钮和矩形
- 6. 用矩形填充面板
- 7. 油滑 - 填充矩形
- 8. 填充矩形的扇形片
- 9. 绘制透明圆形外部填充
- 10. 用矩形填充多边形
- 11. 用矩形填充2D形状
- 12. 从UIIMage创建UIIMageView会导致ImageView的外部矩形为灰色填充
- 13. 将矩形的填充颜色填充为前景色
- 14. 填充矩形中的Java Swing模式
- 15. 使用图像的矩形填充
- 16. 删除填充的矩形c#
- 17. lapack - 填充矩形格式的寻址
- 18. 用Java中的图像填充矩形
- 19. 填充区域的矩形选择?
- 20. 检测用碰撞填充的矩形
- 21. Java:填充网格上的矩形
- 22. 如何在xaml中部分填充画布内的矩形
- 23. 从顶部减少填充矩形的高度
- 24. 我如何填充一个矩形区域除外
- 25. 当旋转外部矩形时,在矩形内移动矩形
- 26. 用图像填充WPF矩形
- 27. 用图像填充ShapeRenderer矩形(.png)?
- 28. 结算ctx.fillStyle绘制填充矩形
- 29. Android重绘矩形填充白色
- 30. 画布矩形填充颜色
有一个剪辑属性,您可以操纵以获得您想要的结果。 – Nitesh