PNG不透明

问题描述 投票:1回答:1

我有几个PNG,白色设置为透明。在XAML设计器中,它们是透明的:

enter image description here

但是执行代码时它们并不透明。我在按下按钮的两个图像和背景之间切换。我的交换图片代码:

   private void SwitchStyleButton_Click(object sender, RoutedEventArgs e)
    {
        IsVictorian = !IsVictorian;

        if (!IsVictorian)
        {

            ImageBrush ib = new ImageBrush();
            ib.ImageSource = new BitmapImage(new Uri(@"Images\Cropped Aluminum.png", UriKind.Relative));
            MainCanvas.Background = ib;

            Left_Image.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\GSBP Left Panel Modern.png", UriKind.Absolute));
        }
        else {
            ImageBrush ib = new ImageBrush();
            ib.ImageSource = new BitmapImage(new Uri(@"Images\Cropped Old Paper.png", UriKind.Relative));
            MainCanvas.Background = ib;
            Left_Image.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\GSBP Left Panel Victorian.png", UriKind.Absolute));

        }
    }

我应该指出,这是我想要透明的Left_Image.Source;不是画布背景。

根据请求,这里是XAML:

<Window x:Class="General_Staff_BP.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:General_Staff_BP"
    mc:Ignorable="d"
    Title="General Staff Black Powder" Height="870" Width="1440" WindowStartupLocation="CenterScreen">
<Canvas x:Name= "MainCanvas">
    <Canvas.Background>
        <ImageBrush ImageSource="Cropped Old Paper.png" Stretch="None"/>
    </Canvas.Background>
    <Grid Height="870" Width="1440" 
          HorizontalAlignment ="Left" VerticalAlignment ="Top" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Label Grid.Column="0" ></Label>
        <Label Grid.Column="1" >
            <StackPanel>
                <Button x:Name="SwitchStyleButton"   Content="Change Style to Modern"  Click="SwitchStyleButton_Click" />
            </StackPanel>
        </Label>
        <Image x:Name="Left_Image" Source="Images\GSBP Left Panel Victorian.png" Stretch="None" />

    </Grid>
</Canvas>

和屏幕截图:enter image description here

然后单击按钮:enter image description here

如您所见,白色不是透明的。

而且,我很确定图像的白色设为透明:enter image description here

c# wpf png transparent
1个回答
0
投票

我认为代码可以为您提供帮助,在这里我只是将Backgroung设置为透明。

<Setter Property="Background" Value="Transparent" />
© www.soinside.com 2019 - 2024. All rights reserved.