1

I want to display a video as background of a grid. Based on this post I created this grid code:

<Grid>
  <Grid.Background>
    <VisualBrush>
      <VisualBrush.Visual>
        <StackPanel Background="White">
          <Image Source="/WPF.Resources;component/Videos/background.wmv" Opacity="0.3"></Image>
        </StackPanel>
      </VisualBrush.Visual>
    </VisualBrush>
  </Grid.Background>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="1*" />
    <ColumnDefinition Width="491" />
    <ColumnDefinition Width="491" />
    <ColumnDefinition Width="1*" />
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
    <RowDefinition Height="1*" />
    <RowDefinition Height="250" />
    <RowDefinition Height="250" />
    <RowDefinition Height="105" />
    <RowDefinition Height="1*" />
  </Grid.RowDefinitions>
</Grid>

However I am getting this error:

Blend does not support this file format. D:\WPF\Resources\Videos\background.wmv

What type of file WPF support as video background? I tried to play the video and I can play it so it is a valid file format.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
mans
  • 17,104
  • 45
  • 172
  • 321

2 Answers2

3

Instead of Image use MediaElement.

<VisualBrush.Visual>            
       <StackPanel Background="White">
                        <MediaElement Source="/WPF.Resources;component/Videos/background.wmv" Opacity="0.3"></MediaElement>
                    </StackPanel>
                </VisualBrush.Visual>

Go to Properties of the background.wmv file and in Copy to Output Directory select Copy always. This means that the video will be copied to bin folder and from there wpf application will be able to access the file and it will be dispayed.

enter image description here

Sats
  • 1,913
  • 1
  • 15
  • 21
  • If you also need to make it loop: https://stackoverflow.com/questions/3406736/what-is-simplest-way-to-repeat-a-video-in-a-mediaelement – EPurpl3 Mar 03 '20 at 08:49
0

Try using MediaElement instead of Image.

Nick
  • 4,787
  • 2
  • 18
  • 24