【发布时间】:2020-04-13 17:42:08
【问题描述】:
我有几个将白色设置为透明的 PNG。在 XAML 设计器中它们是透明的:
但在执行代码时它们并不透明。我在按下按钮时在两个图像和背景之间切换。我的图片交换代码:
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>
如您所见,白色不是透明的。
【问题讨论】:
-
可以显示对应的wpf吗?
-
已添加 XAML。感谢收看。
-
你能显示执行程序的图吗?
-
我没有看到问题你确定你的图像有透明度吗? xaml 不是证明。你可以在 gimp 或其他软件中测试吗?或发布您的原始图片,请我检查一下
-
我有一个免费的开源项目,如果您有兴趣,它可能会对您有所帮助,称为 Transparency Maker。 github.com/DataJuggler/TransparencyMaker我今天早上刚刚发布了一个新视频:youtube.com/…
标签: c# wpf png transparent