WPF位图透明背景变为黑色 [英] WPF Bitmap transparent background turns black
问题描述
我有一个列表框,其中的项目都具有随机的背景颜色. 我想在列表框的每个项目中显示一个位图图片. 现在由于某种原因,每个位图的背景(我已将其设置为Color.Transparent)显示为黑色.
I have a listbox with Items that all have a random background color. In each Item of the listbox i want to display a Bitmap picture. Now for some reason the background of each bitmap (which I've set to Color.Transparent) Shows up black.
这里是它的外观图片
我在列表框中的代码:
<WrapPanel Grid.Row="1" Grid.Column="1" Margin="6" >
<ListBox x:Name="CharListBox" BorderThickness="0" ScrollViewer.VerticalScrollBarVisibility="Hidden"
ItemsSource="{Binding ListToDisplay, UpdateSourceTrigger=PropertyChanged}"
ItemTemplate="{DynamicResource ItemTemplate1}"
SelectionChanged="SelectionChangedNewCharSelected">
</ListBox>
</WrapPanel>
项目模板(我将图像部分旁边的所有内容都删除了,以进行更好的展示)
The item template (I removed everything beside the image part for bettter overview):
<DataTemplate x:Key="ItemTemplate1" >
<Image Source="{Binding OutfitImageSource, UpdateSourceTrigger=PropertyChanged}" Height="40" Width="40" />
</DataTemplate>
该绑定绑定到BitmapSource.
The Binding binds to a BitmapSource.
位图是否可能没有实际的透明度,只是显示为黑色?
我还尝试将AllowsTransparency="True"
添加到窗口属性中,这只会导致窗口立即崩溃.
Is it possible that bitmaps dont have actual transparency, its just shown as black?
I also tried to add AllowsTransparency="True"
to the window properties, this just lead to the window crashing instantly..
谢谢您的帮助!
更新:如果我设置了WindowStyle="None"
,则AllowsTransparency可以工作,但仍不能解决不透明位图的问题.
update: AllowsTransparency works if i set WindowStyle="None"
but it still does not fix the problem with the untransparent bitmaps.
推荐答案
此 answer
位图(即扩展名为.BMP的文件)本身不支持透明性:您需要另存为PNG等格式.
Bitmaps (i.e. files with .BMP extension) do not natively support transparency: you need to save as a different format like PNG.
您也可以此处找到相同的信息.
You can find the same info also here.
因此它不取决于您的XAML或您的代码.只需将位图另存为PNG文件,然后将这些文件用于您的应用程序即可.
So it does not depend on your XAML or your code. Just save your bitmaps as PNG files and then use those files for your application.
这篇关于WPF位图透明背景变为黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!