WPF play vide via MediaPlayer VideoDrawing

FredGrit發表於2024-04-12
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
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 WpfApp56
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            PlayVideo();
        }

        private void PlayVideo()
        { 
            string videoFile = @"R2024-04-12 013708.mp4"; 
            MediaPlayer player = new MediaPlayer();
            player.Open( new Uri(videoFile, UriKind.Relative));
            VideoDrawing drawing = new VideoDrawing();
            drawing.Rect=new Rect(0, 0, ActualWidth,ActualHeight);
            drawing.Player = player;
            player.Play();
            DrawingBrush brush = new DrawingBrush(drawing);
            this.Background = brush;
        }
    }
}

相關文章