Simple Media Player


   
   

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid.Triggers>
    <EventTrigger RoutedEvent="Button.Click" SourceName="playButton">
      <EventTrigger.Actions>
        <BeginStoryboard Name="beginStoryboard">
          <Storyboard>
            <MediaTimeline Source="C:video.wmv"
            Storyboard.TargetName="video"/>
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger.Actions>
    </EventTrigger>
    <EventTrigger RoutedEvent="Button.Click" SourceName="pauseButton">
      <EventTrigger.Actions>
        <PauseStoryboard BeginStoryboardName="beginStoryboard"/>
      </EventTrigger.Actions>
    </EventTrigger>
    <EventTrigger RoutedEvent="Button.Click" SourceName="resumeButton">
      <EventTrigger.Actions>
        <ResumeStoryboard BeginStoryboardName="beginStoryboard"/>
      </EventTrigger.Actions>
    </EventTrigger>
  </Grid.Triggers>
  <MediaElement x:Name="video"/>
  <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom">
    <Button x:Name="playButton">Play</Button>
    <Button x:Name="pauseButton">Pause</Button>
    <Button x:Name="resumeButton">Resume</Button>
  </StackPanel>
</Grid>

   
    
    
     


Overlapping Videos with Effects


   
   
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <MediaElement Source="C:video.wmv" Opacity="0.5">
    <MediaElement.Clip>
      <EllipseGeometry Center="220,220" RadiusX="220" RadiusY="220"/>
    </MediaElement.Clip>
    <MediaElement.LayoutTransform>
      <RotateTransform Angle="180"/>
    </MediaElement.LayoutTransform>
  </MediaElement>
  <MediaElement Source="C:video.wmv" Opacity="0.5">
    <MediaElement.Clip>
      <EllipseGeometry Center="220,220" RadiusX="220" RadiusY="220"/>
    </MediaElement.Clip>
  </MediaElement>
</Canvas>

   
    
    
     


Using MediaElement for Audio


   
   

<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <MediaElement x:Name="audio"/>
  <Button>A Button With Sounds
    <Button.Triggers>
      <EventTrigger RoutedEvent="Button.Click">
        <EventTrigger.Actions>
          <BeginStoryboard>
            <Storyboard>
              <MediaTimeline Source="click.wav" Storyboard.TargetName="audio"/>
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger.Actions>
      </EventTrigger>
      <EventTrigger RoutedEvent="Button.MouseEnter">
        <EventTrigger.Actions>
          <BeginStoryboard>
            <Storyboard>
              <MediaTimeline Source="hover.wav" Storyboard.TargetName="audio"/>
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger.Actions>
      </EventTrigger>
    </Button.Triggers>
  </Button>
</Canvas>

   
    
    
     


Object Transforms in WPF


   
  


<Window x:Class="WpfApplication1.ObjectMatrixTransforms"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Object Matrix Transforms" Height="300" Width="400">
  <StackPanel>
    <Button Click="BtnClose_Click" Margin="15,0,15,5">Close</Button>
    <Canvas Name="canvas1" ClipToBounds="True" Width="270" Height="280">
    <TextBlock Canvas.Top="53" Canvas.Left="90">Original shape</TextBlock>
    <Rectangle Canvas.Top="70" Canvas.Left="100" Width="50" Height="70" Stroke="Black" StrokeThickness="2"
            StrokeDashArray="3,1" />
    <Rectangle Name="rect" Canvas.Top="70" Canvas.Left="100" Width="50" Height="70" Fill="LightCoral"
            Opacity="0.5" Stroke="Black" StrokeThickness="2">
        <Rectangle.RenderTransform>
          <MatrixTransform x:Name="matrixTransform" />
        </Rectangle.RenderTransform>
    </Rectangle>
    </Canvas>
  </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class ObjectMatrixTransforms : Window
    {
        public ObjectMatrixTransforms()
        {
            InitializeComponent();
        }
        public void BtnApply_Click(object sender, EventArgs e)
        {
            Matrix m = new Matrix();
            m.M11 = 1;
            m.M12 = 0;
            m.M21 = 0;
            m.M22 = 1;
            m.OffsetX = 1;
            m.OffsetY = 2;
            matrixTransform.Matrix = m;
        }

    }
}

   
    
     


Matrix3D RotateAtPrepend


   
  

<Window x:Class="WpfApplication1.Matrix3DTransforms"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Matrix3D Transformations" Height="450" Width="300">
  <StackPanel>
    <TextBlock Margin="10,10,5,5" Text="Original Matrix:" />
    <TextBlock Name="tbOriginal" Margin="20,0,5,5" />
    <TextBlock Margin="10,0,5,5" Text="Scale:" />
    <TextBlock Name="tbResult" Margin="20,0,5,5" />

  </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace WpfApplication1
{
    public partial class Matrix3DTransforms : Window
    {
        public Matrix3DTransforms()
        {
            InitializeComponent();
            Matrix3D M = new Matrix3D(1, 2, 3, 4,
                                      2, 1, 0, 0,
                                      0, 0, 1, 0,
                                      1, 2, 3, 1);


            tbOriginal.Text = "(" + M.ToString() + ")";

            //Translation: 
            M.RotateAtPrepend(new Quaternion(new Vector3D(1, 2, 3), 45),new Point3D(10, 20, 30));
            tbResult.Text = "(" + M.ToString() + ")";
        }
    }
}

   
    
     


Matrix3D RotateAt


   
  

<Window x:Class="WpfApplication1.Matrix3DTransforms"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Matrix3D Transformations" Height="450" Width="300">
  <StackPanel>
    <TextBlock Margin="10,10,5,5" Text="Original Matrix:" />
    <TextBlock Name="tbOriginal" Margin="20,0,5,5" />
    <TextBlock Margin="10,0,5,5" Text="Scale:" />
    <TextBlock Name="tbResult" Margin="20,0,5,5" />

  </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace WpfApplication1
{
    public partial class Matrix3DTransforms : Window
    {
        public Matrix3DTransforms()
        {
            InitializeComponent();
            Matrix3D M = new Matrix3D(1, 2, 3, 4,
                                      2, 1, 0, 0,
                                      0, 0, 1, 0,
                                      1, 2, 3, 1);


            tbOriginal.Text = "(" + M.ToString() + ")";

            //Translation: 
            M.RotateAt(new Quaternion(new Vector3D(1, 2, 3), 45),new Point3D(10, 20, 30));
            tbResult.Text = "(" + M.ToString() + ")";
        }
    }
}

   
    
     


Matrix3D Rotate


   
  

<Window x:Class="WpfApplication1.Matrix3DTransforms"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Matrix3D Transformations" Height="450" Width="300">
  <StackPanel>
    <TextBlock Margin="10,10,5,5" Text="Original Matrix:" />
    <TextBlock Name="tbOriginal" Margin="20,0,5,5" />
    <TextBlock Margin="10,0,5,5" Text="Scale:" />
    <TextBlock Name="tbResult" Margin="20,0,5,5" />

  </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace WpfApplication1
{
    public partial class Matrix3DTransforms : Window
    {
        public Matrix3DTransforms()
        {
            InitializeComponent();
            Matrix3D M = new Matrix3D(1, 2, 3, 4,
                                      2, 1, 0, 0,
                                      0, 0, 1, 0,
                                      1, 2, 3, 1);


            tbOriginal.Text = "(" + M.ToString() + ")";

            //Translation: 
            M.Rotate(new Quaternion(new Vector3D(1, 2, 3), 45));
            tbResult.Text = "(" + M.ToString() + ")";
        }
    }
}