问题
i'm trying to blur a background, but I want to use a mask so not all of it is blurred. Is this possible?
Basically I have got some code which adds rectangles to a canvas anywhere where there is a certain control, so this canvas would be the mask.
This is the C#
List<Rectangle> MaskBlocks = new List<Rectangle>();
foreach (StackPanel gr in FindVisualChildren<StackPanel>(Container))
if (gr.Tag != null && gr.Tag.ToString() == "Blur")
{
System.Windows.Point tmp = gr.TransformToAncestor(this).Transform(new System.Windows.Point(0, 0));
MaskBlocks.Add(new System.Drawing.Rectangle(new System.Drawing.Point((int)tmp.X,(int)tmp.Y), new System.Drawing.Size((int)gr.ActualWidth, (int)gr.ActualHeight)));
}
foreach(System.Drawing.Rectangle x in MaskBlocks)
{
System.Windows.Shapes.Rectangle tmp;
tmp = new System.Windows.Shapes.Rectangle();
tmp.Stroke = new SolidColorBrush(Colors.Black);
tmp.Fill = new SolidColorBrush(Colors.Black);
tmp.Width = x.Width;
tmp.Height = x.Height;
Canvas.SetLeft(tmp, x.X);
Canvas.SetTop(tmp, x.Y);
Canvas.Children.Add(tmp);
}
And here is the XAML layout
<Grid Name="Container">
<Grid.Background>
<ImageBrush ImageSource="vlcsnap-2015-08-11-16h34m58s106.jpg" />
</Grid.Background>
<StackPanel Margin="10,10,398,175" Name="Stack1" Tag="Blur" />
<StackPanel Margin="235,38,27,133" Name="Stack2" Tag="Blur">
<Button Click="Button_Click" Content="Start" Name="Test2" />
<StackPanel Name="stackPanel" VerticalAlignment="Stretch" Height="127" />
</StackPanel>
<StackPanel Margin="36,245,235,21" Name="Stack3" Tag="Blur" />
<Canvas Name="Canvas"></Canvas>
</Grid>
来源:https://stackoverflow.com/questions/34325221/use-canvas-to-mask-background