简体   繁体   English

将透明图像设置为窗口背景,同时仍显示背景颜色

[英]Set a transparent image as a window background while still showing the background color

I'm trying to add an image as the background of a window; 我正在尝试添加图像作为窗口的背景; the image is a transparent PNG. 图像是透明的PNG。 My issue here is whenever I set the image as background it covers whatever color is under it despite being transparent, not showing my desired background color. 我的问题是,无论何时将图像设置为背景,它都会覆盖其下的任何颜色,尽管它是透明的,但没有显示我想要的背景颜色。 When I compile the result is the window having the desired image as background with the transparent part being replaced by a black color instead of showing the background color I set. 当我编译时,结果是窗口具有所需的图像作为背景,透明部分被黑色代替,而不显示我设置的背景颜色。

My code for MainWindows.xaml is as follows: 我的MainWindows.xaml代码如下:

<Window x:Class="Eorzea_Timers.MainWindow"
    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:Eorzea_Timers"
    mc:Ignorable="d"        
    Title="MainWindow" Height="667" Width="375">

<Window.Background>
    <ImageBrush ImageSource="Background.png"/>
</Window.Background>

<Window.Resources>
    <Style TargetType="Window">
        <Setter Property="Background" Value="White"/>
    </Style>
</Window.Resources>

<Grid>

</Grid>

Is it even possible to have what I want or should I just include the colored background in the image itself? 甚至有可能拥有我想要的东西,还是仅将彩色背景包括在图像本身中?

Put an Image element into the Grid: 将Image元素放入网格中:

<Window ... Background="White">
    <Grid>
        <Image Source="Background.png"/>
        <Grid>
            <!-- other elements here -->
        </Grid>
    </Grid>
</Window>

Or if for whatever reason you need to use an ImageBrush, use it as Background of the top-level Grid: 或者,如果出于任何原因需要使用ImageBrush,请将其用作顶级Grid的Background:

<Window ... Background="White">
    <Grid>
        <Grid.Background>
            <ImageBrush ImageSource="Background.png"/>
        </Grid.Background>
        <Grid>
            <!-- other elements here -->
        </Grid>
    </Grid>
</Window>

尝试使用VisualBrush,并创建具有所需背景颜色的形状,然后将图像放在其顶部。

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

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