WPF Bitmap transparent background turns black

冷暖自知 提交于 2020-01-04 05:45:26

问题


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.

Here a picture of how it looks

My code for the listbox:

 <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>

The Binding binds to a BitmapSource.

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..

Thank you for helping in advance!

update: AllowsTransparency works if i set WindowStyle="None" but it still does not fix the problem with the untransparent bitmaps.


回答1:


As reported in this answer

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.

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.



来源:https://stackoverflow.com/questions/51541674/wpf-bitmap-transparent-background-turns-black

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!