Search code examples
wpfimagecolorsimagesource

WPF: Solid color ImageSource


In my app I display images I get from external DLLs, by binding the Source of an Image control to an ImageSource.

This works well, but sometimes I don't get any data from my DLL and I'd like to just show a black image. In that case, how do I create an ImageSource which contains just a solid color?


Solution

  • An alternative method is to give a background color, and not show the image.

    // XAML
    <Grid Background="Black">
      <Image x:Name="imgDisplay"/>
    </Grid>
    
    // C#
    imgDisplay.Source = null;
    // -- OR --
    imgDisplay.Visibility = Visibility.Collapsed;