Use Canvas to mask background

二次信任 提交于 2020-01-04 07:22:47

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!