Page view counter

Did You Know That... The Target of Video Brushes Is Not The Video

When you use an image brush, the target of the brush is the image you wish to display,

<Rectangle x:Name="myRectImage"
           Width="100"
           Height="44"
         Canvas.Left="0"
         Canvas.Top = "500"
           StrokeThickness="2"
           Stroke="Black" >
  <Rectangle.Fill>
    <ImageBrush ImageSource="Picture.jpg" />
  </Rectangle.Fill>
</Rectangle>

<Ellipse x:Name="myEllipseImage"
         Width="100"
         Height="44"
         Canvas.Left="110"
         Canvas.Top = "500"
         StrokeThickness="2"
         Stroke="Blue" >
  <Ellipse.Fill>
    <ImageBrush ImageSource="Picture.jpg" />
  </Ellipse.Fill>
</Ellipse>

Assuming you have a jpeg file in your directory named Picture, the ImageBrush will find it and display it,

Picture

If, however, you wish to display a video, you can not write your code by simply pointing the VideoBrush to the video image, as you might expect, (even if Butterfly.wmv is in the correct directory)

<Rectangle x:Name="myRectVideoBroken"
           Width="100"
           Height="44"
         Canvas.Left="0"
         Canvas.Top = "500"
           StrokeThickness="2"
           Stroke="Black" >
  <Rectangle.Fill>
     <!-- Won't work!!-->
    <VideoBrush VideoSource="Butterfly.wmv" />
  </Rectangle.Fill>
</Rectangle>

<Ellipse x:Name="myEllipseVideoBroken"
         Width="100"
         Height="44"
         Canvas.Left="110"
         Canvas.Top = "500"
         StrokeThickness="2"
         Stroke="Blue" >
  <Ellipse.Fill>
         <!-- Won't work!!-->
    <VideoBrush VideoSource="Butterfly.wmv" />
  </Ellipse.Fill>
</Ellipse>

Unfortunately, this will generate nothing more than an error message,

Video Error Message

Video brushes require that their source be a MediaElement, whose source, in turn, is the video you wish to display.

<MediaElement
  x:Name="butterflyMediaElement"
  Source="Butterfly.wmv"   />
It is this MediaElement (butterflyMediaElement) that becomes the source for the VideoBrush:
<Rectangle x:Name="myRectMedia"
       Width="100"
       Height="44"
     Canvas.Left="220"
     Canvas.Top = "500"
       StrokeThickness="2"
       Stroke="Black" >
    <Rectangle.Fill>
        <VideoBrush SourceName="butterflyMediaElement" />
    </Rectangle.Fill>
</Rectangle>

<Ellipse x:Name="myEllipseMedia"
     Width="100"
     Height="44"
     Canvas.Left="330"
     Canvas.Top = "500"
     StrokeThickness="2"
     Stroke="Blue" >
    <Ellipse.Fill>
        <VideoBrush SourceName="butterflyMediaElement" />
    </Ellipse.Fill>
</Ellipse>

With this bit of indirection, the VideoBrush is able to display the movie.

MediaElement

Unfortunately, the MediaElement itself totally dominates the page; which is not at all what we want. We can solve this by making the MediaElement invisible. One easy way to do so is to set its opacity to 0. While we're at it, we'll mute the MediaElement, thereby turning off all sounds from the movie as well,

<MediaElement
  x:Name="butterflyMediaElement"
  Source="Butterfly.wmv"
  IsMuted="True"
  Opacity="0.0" />

The result is that the MediaElement continues to serve as the source for the VideoBrush but is not itself visible.

VideoBrushWorking

kick it on DotNetKicks.com
Published 19 January 2008 10:17 AM by jesseliberty

Comments

# DotNetKicks.com said on 19 January, 2008 11:09 AM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# wisecarver said on 19 January, 2008 12:38 PM

I really appreciate these notes Jesse. Thanks.

Looking forward to more on Silverlight and ASP.NET; can hardly wait actually.

# jesseliberty said on 19 January, 2008 06:22 PM

Great idea! I'll be sure to add a few notes that focus on Silverlight + ASP.NET + AJAX in coming days.

Thanks

# Community Blogs said on 23 January, 2008 03:31 PM

Tim Sneath discusses the Update capability just released for SL 1.0, Shawn Wildermuth gives us a *very