使用画布遮罩背景

时间:2015-12-17 01:34:19

标签: c# wpf canvas

我试图模糊背景,但我想使用一个面具,所以不是所有的都模糊了。这可能吗?

基本上我有一些代码可以在任何有特定控件的地方为画布添加矩形,所以这个画布就是掩码。

这是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);
}

这是XAML布局

<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>

0 个答案:

没有答案