名字里面放视频

来源:互联网 发布:ipad桌面图标整理软件 编辑:程序博客网 时间:2024/04/29 15:13

界面上

<Window x:Class="nameVedio.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Viewbox Stretch="Uniform">
        <Canvas
      Name="canvas"
      Background="Black"
      Width="400"
      Height="300">

            <!-- 設定要播放的影片 -->
            <MediaElement
        Canvas.Top="10"
        Canvas.Left="40"
        Name="myMediaElement">
                <MediaElement.Triggers>
                    <EventTrigger RoutedEvent="MediaElement.Loaded">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <MediaTimeline Source="xbox.wmv" Storyboard.TargetName="myMediaElement" 
                    RepeatBehavior="Forever" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>
                </MediaElement.Triggers>
            </MediaElement>

            <!-- 定義用來裁剪影片和小白球動畫的路徑 -->
            <!-- 為了讓影片可以透出來,所以 Fill 屬性要設定成 Transparent -->
            <Path
        Canvas.Top="10"
        Canvas.Left="40"
        Name="path"
        Stroke="Gray"
        Fill="Transparent" />

            <!-- 定義小白球 -->
            <!-- 座標是相對於路徑的相對座標 -->
            <Ellipse 
        Visibility="Hidden"   
        Name="ellipse"
        Canvas.Top="0"
        Canvas.Left="30"        
        Width="20"
        Height="20">
                <Ellipse.Fill>
                    <RadialGradientBrush>
                        <GradientStop Color="#ffff" Offset="0"/>
                        <GradientStop Color="#0fff" Offset="1"/>
                    </RadialGradientBrush>
                </Ellipse.Fill>
                <Ellipse.RenderTransform>
                    <MatrixTransform />
                </Ellipse.RenderTransform>
                <Ellipse.Triggers>
                    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard x:Name="storyboard">
                                    <MatrixAnimationUsingPath
                  x:Name="matrixAnimation"
                  Duration="0:00:59"
                  RepeatBehavior="Forever"
                  Storyboard.TargetProperty="RenderTransform.Matrix" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>
                </Ellipse.Triggers>
            </Ellipse>

        </Canvas>
    </Viewbox>
</Window>

 

 

后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace nameVedio
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            // 建立格式化文字物件
            FormattedText formattedText = new FormattedText(
                "姜一多",
                new System.Globalization.CultureInfo("zh-TW"),
                FlowDirection.LeftToRight,
                new Typeface(
                    new FontFamily("楷体"),
                    FontStyles.Normal,
                    FontWeights.Bold,
                    FontStretches.Normal),
                120,
                Brushes.Red);

            // 建立文字外框的幾何物件
            Geometry geometry = new PathGeometry();
            geometry = formattedText.BuildGeometry(new Point(0, 0));

            // 調整幾何的比例,以便於可以和影片100% 重疊在一起
            ScaleTransform myScaleTransform = new ScaleTransform();
            myScaleTransform.ScaleX = .85;
            myScaleTransform.ScaleY = 1.85;
            geometry.Transform = myScaleTransform;

            // 建立路徑幾何
            PathGeometry pathGeometry = new PathGeometry();
            pathGeometry = geometry.GetFlattenedPathGeometry();

            //1. 套用到 XAML 中的 PATH 物件
           path.Data = pathGeometry;

            // 2. 使用路徑幾何來裁剪影片
            myMediaElement.Clip = pathGeometry;

            // 3. 讓白色小球依照字型外框移動
            //matrixAnimation.PathGeometry = pathGeometry;
            //ellipse.Visibility = Visibility.Visible;
        }
    }
}

 

 

路径放好就可以了

原创粉丝点击