简体   繁体   English

WPF Canvas - 如何使部分出现淡出,同时保持 rest 上的常规不透明度?

[英]WPF Canvas - How to make part appear faded out, while keeping regular opacity on the rest?

I have a WPF Canvas that I set the background of to an image.我有一个 WPF Canvas,我将其背景设置为图像。

I am then drawing a transparent rectangle on the Canvas. What I am trying to achieve is to have the image faded out slightly (presumably through lowering Opacity) everywhere except where the rectangle is .然后我在 Canvas 上绘制了一个透明矩形。我想要实现的是让图像在矩形所在的任何地方都略微淡出(大概是通过降低不透明度)。

Example of this effect on a plain white background.这种效果在纯白色背景上的示例。 在此处输入图像描述

Is there any way to bring opacity up only in the area covered by the Rectangle?有什么方法可以只在矩形覆盖的区域提高不透明度吗? Or any other, simple way to achieve this effect?或者任何其他简单的方法来实现这种效果?

I have tried lowering the Opacity of the entire Canvas, and then bringing the opacity of the transparent Rectangle up, but it didn't have any effect - the entire Canvas was faded out.我试过降低整个 Canvas 的不透明度,然后调高透明矩形的不透明度,但没有任何效果——整个 Canvas 都淡出。

Another idea (which I guess could be quite costly in performance) I had would be to, instead of reducing Opacity of the entire Canvas, to fill the remainder (except the selected area) with semi-transparent Rectangles with a darker background.我的另一个想法(我想这在性能上可能会非常昂贵)不是降低整个 Canvas 的不透明度,而是用具有较暗背景的半透明矩形填充其余部分(所选区域除外)。 However, I assume this would require a lot of re-calculations when placing the Rectangles on the Canvas.但是,我假设在将矩形放置在 Canvas 上时需要进行大量重新计算。

You need to set an opacity mask.您需要设置不透明蒙版。 I show an example how to create it using VisualBrush.我展示了一个示例,说明如何使用 VisualBrush 创建它。

<Window x:Class="Core2022.SO.nooblet2.OpacityMaskWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Core2022.SO.nooblet2"
        mc:Ignorable="d"
        Title="OpacityMaskWindow" Height="450" Width="800">
    <Canvas x:Name="mainCanvas">
        <Panel.Background>
            <ImageBrush ImageSource="/telegram.png"/>
        </Panel.Background>
        <UIElement.OpacityMask>
            <VisualBrush>
                <VisualBrush.Visual>
                    <Canvas Background="#40000000"
                            Width="{Binding ActualWidth, ElementName=mainCanvas, Mode=OneWay}"
                            Height="{Binding ActualHeight, ElementName=mainCanvas, Mode=OneWay}">
                        <Rectangle Fill="#FF000000"
                                   Canvas.Left="{Binding Path=(Canvas.Left), ElementName=someFrame, Mode=OneWay}"
                                   Canvas.Top="{Binding Path=(Canvas.Top), ElementName=someFrame, Mode=OneWay}"
                                   Width="{Binding ActualWidth, ElementName=someFrame, Mode=OneWay}"
                                   Height="{Binding ActualHeight, ElementName=someFrame, Mode=OneWay}"/>
                    </Canvas>
                </VisualBrush.Visual>
            </VisualBrush>
        </UIElement.OpacityMask>
        <Rectangle x:Name="someFrame" Canvas.Left="200" Canvas.Top="100" Width="400" Height="200"/>
    </Canvas>
</Window>

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM