WPF改变亮度

发布于 2024-07-14 18:06:45 字数 149 浏览 6 评论 0原文

嗯,我有一个黑白应用程序,我需要一个降低亮度的功能,我该怎么做? 所有白色都来自保存在 ResourceDictionary(Application.xaml) 中的 SolidColorBrush,我当前的解决方案是放置一个空窗口,其不透明度为 80%,但这不允许我使用底层窗口。

Well i have a application that is black and white and i need a function to lower the brightness how can i do this? all the white comes from a SolidColorBrush that is saved in a ResourceDictionary(Application.xaml), my current solution is to put a empty window that is back with 80% opacity over it but this does not allow me to use the underlying window..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

熊抱啵儿 2024-07-21 18:06:45

如果您的所有 UI 元素都使用相同的 Brush,为什么不直接修改 Brush 来降低亮度呢? 例如:

public void ReduceBrightness()
{
    var brush = Application.Resources("Brush") as SolidColorBrush;
    var color = brush.Color;
    color.R -= 10;
    color.G -= 10;
    color.B -= 10;
    brush.Color = color;
}

在对被冻结的 Brush 发表评论后进行编辑:

如果您使用的是内置画笔之一(通过 Brushes class) 那么它将被冻结。 不要使用其中之一,而是声明您自己的 Brush 而不冻结它:

<SolidColorBrush x:Key="Brush">White</SolidColorBrush>

在 Robert 对应用程序级资源的评论后进行编辑:

Robert 是对的。 在应用程序级别添加的资源如果可冻结,则会自动冻结。 即使您明确要求不要冻结它们:

<SolidColorBrush x:Key="ForegroundBrush" PresentationOptions:Freeze="False" Color="#000000"/>

我可以看到有两种解决方法:

  1. 正如 Robert 建议的那样,将资源放在资源树中的较低级别。 例如,在 WindowResources 集合中。 但这使得分享变得更加困难。
  2. 将资源放入不可冻结的包装器中。

作为 #2 的示例,请考虑以下内容。

App.xaml

<Application.Resources>
    <FrameworkElement x:Key="ForegroundBrushContainer">
        <FrameworkElement.Tag>
            <SolidColorBrush PresentationOptions:Freeze="False" Color="#000000"/>
        </FrameworkElement.Tag>
    </FrameworkElement>
</Application.Resources>

Window1.xaml

<StackPanel>
    <Label Foreground="{Binding Tag, Source={StaticResource ForegroundBrushContainer}}">Here is some text in the foreground color.</Label>
    <Button x:Name="_button">Dim</Button>
</StackPanel>

Window1.xaml.cs

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        _button.Click += _button_Click;
    }

    private void _button_Click(object sender, RoutedEventArgs e)
    {
        var brush = (FindResource("ForegroundBrushContainer") as FrameworkElement).Tag as SolidColorBrush;
        var color = brush.Color;
        color.R -= 10;
        color.G -= 10;
        color.B -= 10;
        brush.Color = color;
    }
}

它不是那么漂亮,但它是我能想到的最好的正确的现在。

If all your UI elements are using the same Brush, why not just modify the Brush to reduce the brightness? For example:

public void ReduceBrightness()
{
    var brush = Application.Resources("Brush") as SolidColorBrush;
    var color = brush.Color;
    color.R -= 10;
    color.G -= 10;
    color.B -= 10;
    brush.Color = color;
}

Edit after your comment on the Brush being frozen:

If you're using one of the built-in brushes (via the Brushes class) then it will be frozen. Instead of using one of them, declare your own Brush without freezing it:

<SolidColorBrush x:Key="Brush">White</SolidColorBrush>

Edit after Robert's comment on Application-level resources:

Robert is right. Resources added at the Application level are automatically frozen if they are freezable. Even if you explicitly ask for them not to be frozen:

<SolidColorBrush x:Key="ForegroundBrush" PresentationOptions:Freeze="False" Color="#000000"/>

There are two ways around this that I can see:

  1. As Robert suggested, put the resource at a lower level in the resource tree. For example, in a Window's Resources collection. This makes it harder to share though.
  2. Put the resource in a wrapper that is not freezable.

As an example of #2 consider the following.

App.xaml:

<Application.Resources>
    <FrameworkElement x:Key="ForegroundBrushContainer">
        <FrameworkElement.Tag>
            <SolidColorBrush PresentationOptions:Freeze="False" Color="#000000"/>
        </FrameworkElement.Tag>
    </FrameworkElement>
</Application.Resources>

Window1.xaml:

<StackPanel>
    <Label Foreground="{Binding Tag, Source={StaticResource ForegroundBrushContainer}}">Here is some text in the foreground color.</Label>
    <Button x:Name="_button">Dim</Button>
</StackPanel>

Window1.xaml.cs:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        _button.Click += _button_Click;
    }

    private void _button_Click(object sender, RoutedEventArgs e)
    {
        var brush = (FindResource("ForegroundBrushContainer") as FrameworkElement).Tag as SolidColorBrush;
        var color = brush.Color;
        color.R -= 10;
        color.G -= 10;
        color.B -= 10;
        brush.Color = color;
    }
}

It's not as pretty, but it's the best I can come up with right now.

初吻给了烟 2024-07-21 18:06:45

通过更改我的根元素的不透明度来解决这个问题,而不是尝试修改画笔,但如果有人告诉我我是否可以做到这一点或它不可能,那仍然很好。

Solved this by changing the Opacity of my root element instead of trying to modify a brush but it would still be nice if some told me if i can do that some how or its not possible.

沦落红尘 2024-07-21 18:06:45

如果将 SolidColorBrush 添加到较低级别的资源中,Kent 的解决方案将起作用。 Freezables 添加到 Application.Resources 后会自动冻结。

Kent's solution will work if the SolidColorBrush is added to the resources at a lower level. Freezables are automatically frozen when they're added to Application.Resources.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文