在WPF中利用MediaElement控件可以播放视频,但是要想完全给人一种嵌入窗体的感觉就要花一点功夫了。
先上效果图:
1.xaml代码如下:
<Window x:Class="wpf窗体中视频播放.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Name="Window" Title="MainWindow" Width="519" Height="360" mc:Ignorable="d" WindowStartupLocation="CenterScreen"> <Window.Resources> <ImageBrush x:Key="ImageBrush1" ImageSource="pict1.jpg"/> <DrawingBrush x:Key="DrawingBrush1" Viewbox="0,0,104,126.703333333333" ViewboxUnits="Absolute"> <DrawingBrush.Drawing> <DrawingGroup> <DrawingGroup.ClipGeometry> <RectangleGeometry Rect="0,0,104,119"/> </DrawingGroup.ClipGeometry> <GeometryDrawing> <GeometryDrawing.Geometry> <GeometryGroup FillRule="Nonzero"> <PathGeometry Figures="M79.40625,93.092293 L51.375,94.404793 51.375,97.217293 C51.375,98.654793 51.8125,99.732918 52.6875,100.45167 53.5625,101.17042 54.875,101.52979 56.625,101.52979 L72,101.52979 C74.40625,101.52979 76.125,101.06886 77.15625,100.14698 77.671875,99.686043 78.117188,98.867683 78.492188,97.691902 78.867188,96.516121 79.171875,94.982918 79.40625,93.092293 z M63.328125,38.623543 C60.703125,43.623543 57.84375,48.029793 54.75,51.842293 L74.25,50.857918 C71.5625,48.389168 68.6875,45.904793 65.625,43.404793 L69.9375,38.623543 63.328125,38.623543 z M21.46875,38.623543 C18.25,44.154793 14.390625,49.123543 9.890625,53.529793 L31.640625,52.779793 C28.578125,49.748543 25.5,46.811043 22.40625,43.967293 L27.890625,38.623543 21.46875,38.623543 z M16.125,21.279793 L29.0625,21.279793 C28.34375,23.592293 27.546875,25.842293 26.671875,28.029793 L48.28125,28.029793 48.28125,38.623543 31.359375,38.623543 C34.015625,40.779793 36.96875,43.404793 40.21875,46.498543 L34.03125,52.686043 53.15625,51.889168 C50.125,49.732918 46.78125,47.717293 43.125,45.842293 46.3125,42.248543 49.109375,38.404793 51.515625,34.311043 53.921875,30.217293 55.9375,25.873543 57.5625,21.279793 L70.6875,21.279793 C69.875,23.623543 69.046875,25.873543 68.203125,28.029793 L94.125,28.029793 94.125,38.623543 75.5625,38.623543 C78.625,40.842293 81.375,42.936043 83.8125,44.904793 L78.328125,50.670418 83.625,50.342293 86.0625,60.279793 51.375,62.061043 51.375,67.732918 85.21875,66.092293 85.96875,76.311043 51.375,77.998543 51.375,83.857918 92.90625,81.936043 93.65625,92.436043 82.875,92.951668 C86.125,94.232918 89.375,95.404793 92.625,96.467293 91.75,99.998543 90.859375,102.96729 89.953125,105.37354 89.046875,107.77979 87.421875,109.58448 85.078125,110.78761 82.734375,111.99073 79.6875,112.59229 75.9375,112.59229 L52.21875,112.59229 C49.890625,112.59229 47.853516,112.29737 46.107422,111.70753 44.361328,111.11768 42.90625,110.23292 41.742188,109.05323 40.578125,107.87354 39.705078,106.39893 39.123047,104.6294 38.541016,102.85987 38.25,100.79542 38.25,98.436043 L38.25,95.014168 2.71875,96.654793 2.0625002,86.154793 38.25,84.467293 38.25,78.654793 8.53125,80.061043 7.7812495,69.842293 38.25,68.389168 38.25,62.670418 10.96875,63.748543 C10.4375,60.561043 9.828125,57.389168 9.140624,54.232918 8.796875,54.576668 8.4375,54.904793 8.062501,55.217293 6.5,51.904793 4.375,48.217293 1.6875,44.154793 4.90625,40.873543 7.71875,37.326668 10.124999,33.514168 12.53125,29.701668 14.53125,25.623543 16.125,21.279793 z" FillRule="Nonzero"/> </GeometryGroup> </GeometryDrawing.Geometry> <GeometryDrawing.Brush> <ImageBrush ImageSource="pict1.jpg"/> </GeometryDrawing.Brush> </GeometryDrawing> </DrawingGroup> </DrawingBrush.Drawing> </DrawingBrush> <DrawingBrush x:Key="DrawingBrush2" Viewbox="0,0,86,81" ViewboxUnits="Absolute"> <DrawingBrush.Drawing> <GeometryDrawing> <GeometryDrawing.Pen> <Pen Brush="Black" DashCap="Flat" EndLineCap="Flat" LineJoin="Miter" MiterLimit="10" StartLineCap="Flat" Thickness="1"/> </GeometryDrawing.Pen> <GeometryDrawing.Geometry> <RectangleGeometry RadiusX="0" RadiusY="0" Rect="0.5,0.5,85,80"/> </GeometryDrawing.Geometry> <GeometryDrawing.Brush> <LinearGradientBrush MappingMode="RelativeToBoundingBox" EndPoint="0.104,1.072" StartPoint="0.814,0.103"> <GradientStop Color="Black" Offset="0.043"/> <GradientStop Color="White" Offset="1"/> <GradientStop Color="#FF30A12E" Offset="0.448"/> <GradientStop Color="#FFD2E24A" Offset="0.67"/> </LinearGradientBrush> </GeometryDrawing.Brush> </GeometryDrawing> </DrawingBrush.Drawing> </DrawingBrush> <Storyboard x:Key="video1_avi"> <MediaTimeline BeginTime="00:00:00" Source="三峡大坝.avi" Storyboard.TargetName="三峡大坝" d:DesignTimeNaturalDuration="22.439183699999997"/> <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="三峡大坝" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"> <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.869"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="三峡大坝" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"> <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.858"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="三峡大坝" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"> <SplineDoubleKeyFrame KeyTime="00:00:00" Value="42"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="三峡大坝" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"> <SplineDoubleKeyFrame KeyTime="00:00:00" Value="34"/> </DoubleAnimationUsingKeyFrames> </Storyboard> <ImageBrush x:Key="ImageBrush2" ImageSource="bg.png"/> </Window.Resources> <Window.Triggers> <EventTrigger RoutedEvent="FrameworkElement.Loaded"> <BeginStoryboard Storyboard="{StaticResource video1_avi}"/> </EventTrigger> </Window.Triggers> <Grid x:Name="LayoutRoot" Background="#FFC6C4C4"> <Grid Background="White"> <MediaElement x:Name="三峡大坝" HorizontalAlignment="Right" Stretch="Fill" RenderTransformOrigin="0.5,0.5" OpacityMask="{DynamicResource ImageBrush2}"> <MediaElement.RenderTransform> <TransformGroup> <ScaleTransform/> <SkewTransform/> <RotateTransform/> <TranslateTransform/> </TransformGroup> </MediaElement.RenderTransform> </MediaElement> </Grid> </Grid> </Window>
2.MainWindow窗体界面效果: