C# WPF窗口中的透明PNG

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12900988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:53:31  来源:igfitidea点击:

Transparent PNG in WPF window

c#wpfxaml

提问by Developer

I try to apply PNG image that has transparency into the whole window but the window is always white.

我尝试将具有透明度的 PNG 图像应用于整个窗口,但窗口始终为白色。

Any clue to see PNG with its transparency?

有什么线索可以看到 PNG 的透明度吗?

Thank you!

谢谢!

C#

C#

public SplashScreen()
        {
            InitializeComponent();

            var myBrush = new ImageBrush();
            var image = new Image
            {
                Source = new BitmapImage(
                    new Uri(
                        "pack://application:,,,/MyApp;component/Images/Logo.png"))
            };
            myBrush.ImageSource = image.Source;
            Background = myBrush;

        }

XAML

XAML

<Window x:Class="MyApp.SplashScreen"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Topmost="True"
        Title="SplashScreen" Height="400" Width="400" WindowStartupLocation="CenterScreen" WindowStyle="None" 
           BorderThickness="5" ShowInTaskbar="False" ResizeMode="NoResize" >
    <Grid Name="MainGrid">
        <Label FontSize="10" Height="20" Foreground="White" Margin="0,0,0,0" Padding="10,0,0,5" Name="statusLabel"  VerticalAlignment="Bottom"></Label>
        <TextBlock  Visibility="Collapsed"  FontSize="10" Foreground="White"   Margin="18,110,18,30" Name="appInfo" TextAlignment="Center">

        </TextBlock>
        <TextBlock Visibility="Collapsed"  FontSize="20" Foreground="White"   Margin="0,83,0,90" Name="version" TextAlignment="Center">

        </TextBlock>
    </Grid>
</Window>

采纳答案by N_A

Lots of information here. I think the piece you're missing is AllowsTransparency="True"on your window.

这里有很多信息。我认为您缺少的那件作品AllowsTransparency="True"在您的窗户上。

回答by Alexandru

My window had a default background on it and this didn't work for me without setting Background="Transparent" on the window (probably due to Expression Blend), just in case anyone has a similar issue like this.

我的窗口有一个默认背景,如果没有在窗口上设置 Background="Transparent"(可能是由于 Expression Blend),这对我不起作用,以防万一有人遇到类似的问题。