C# PNG是不透明的
我有两个PNG,白色设置为透明。在XAML设计器中,它们是透明的: 但是当代码被执行时,它们是不透明的。我用一个按钮在两个图像和背景之间切换。用于交换图像的我的代码:C# PNG是不透明的,c#,wpf,png,transparent,C#,Wpf,Png,Transparent,我有两个PNG,白色设置为透明。在XAML设计器中,它们是透明的: 但是当代码被执行时,它们是不透明的。我用一个按钮在两个图像和背景之间切换。用于交换图像的我的代码: private void SwitchStyleButton_Click(object sender, RoutedEventArgs e) { IsVictorian = !IsVictorian; if (!IsVictorian) {
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));
}
}
我应该指出,我想要透明的是左_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>
和屏幕截图:
点击按钮后:
正如你所见,白色不是透明的
而且,我非常确定图像的白色设置为透明:
我认为代码对您有帮助,这里我只是将背景设置为透明
<Setter Property="Background" Value="Transparent" />
我认为代码对您有帮助,这里我只是将背景设置为透明
<Setter Property="Background" Value="Transparent" />
能否显示wpf对应的?添加了XAML。谢谢看。你能给我看一个执行程序的图吗?我看不出问题。你确定你的图像有透明度吗?xaml不是证据。你能用gimp或其他软件测试吗?或者发布你的原始图像,请我检查一下,我有一个免费的开源项目,如果你感兴趣的话,它可能会帮助你叫Transparency Maker。我今天早上刚刚发布了一个新视频:你能显示wpf对应的吗?XAML补充道。谢谢看。你能给我看一个执行程序的图吗?我看不出问题。你确定你的图像有透明度吗?xaml不是证据。你能用gimp或其他软件测试吗?或者发布你的原始图像,请我检查一下,我有一个免费的开源项目,如果你感兴趣的话,它可能会帮助你叫Transparency Maker。今天早上我刚刚发布了一个新视频: