C# XAML主窗口上的视频绘图?

C# XAML主窗口上的视频绘图?,c#,wpf,media-player,C#,Wpf,Media Player,我在WPF中有一个使用VideoDrawing对象播放视频文件的简单示例,下面是代码: public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); MediaTimeline timeline = new MediaTimeline(new Uri(@"c:\test\RedRock-uhd-h264.mp4", UriKind.A

我在WPF中有一个使用VideoDrawing对象播放视频文件的简单示例,下面是代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        MediaTimeline timeline = new MediaTimeline(new Uri(@"c:\test\RedRock-uhd-h264.mp4", UriKind.Absolute));
        timeline.RepeatBehavior = RepeatBehavior.Forever;
        MediaClock clock = timeline.CreateClock();
        MediaPlayer player = new MediaPlayer();
        player.Clock = clock;
        VideoDrawing drawing = new VideoDrawing();

        drawing.Rect = new Rect(0, 0, 820, 600);    //<--video size is 620 x 400 same as XAML MainWindow size
        drawing.Rect = new Rect(0, 0, 420, 280);    //<--video size is 620 x 400 same as XAML MainWindow size
        drawing.Rect = new Rect(0, 0, 220, 80);     //<--video size is 620 x 400 same as XAML MainWindow size
        drawing.Rect = new Rect(0, 0, 1, 1);        //<--video size is 620 x 400 same as XAML MainWindow size
        drawing.Rect = new Rect(0, 0, 0, 0);        //<--video does not show
        //drawing.Rect = new Rect(0, 0, 0, 0);      //<--video does not show

        drawing.Player = player;

        DrawingBrush brush = new DrawingBrush(drawing);
        this.Background = brush;            
    }
}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
MediaTimeline=新的MediaTimeline(新Uri(@“c:\test\RedRock-uhd-h264.mp4”,UriKind.Absolute));
timeline.RepeatBehavior=RepeatBehavior.Forever;
MediaClock时钟=timeline.CreateClock();
MediaPlayer=新的MediaPlayer();
player.Clock=时钟;
VideoDrawing=新的VideoDrawing();

drawing.Rect=new Rect(0,0820600);//将拉伸模式设置为无:

brush.Stretch = Stretch.None;
当然,问题是您现在无法设置播放器周围区域的颜色。如果您想要控制该区域,则必须切换到VisualBrush并使用MediaElement:

// create a grid and bind it to the parent window's size
var grid = new Grid { Background = Brushes.CornflowerBlue };    // <- sets background color
grid.SetBinding(WidthProperty, new Binding
{
    RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Window), 1),
    Path = new PropertyPath("ActualWidth"),
    Mode = BindingMode.OneWay,
    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
grid.SetBinding(HeightProperty, new Binding
{
    RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Window), 1),
    Path = new PropertyPath("ActualHeight"),
    Mode = BindingMode.OneWay,
    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});

// add the media player
grid.Children.Add(new MediaElement
{
    Source = new Uri("yourvideo.mp4", UriKind.RelativeOrAbsolute),
    LoadedBehavior = MediaState.Play,
    Stretch = Stretch.Fill,
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Center,
    Width = 640,    // <-- video size
    Height = 480
});

// wrap it all up in a visual brush
this.Background = new VisualBrush { Visual = grid };
//创建网格并将其绑定到父窗口的大小

var grid=new grid{Background=brush.CornflowerBlue};//谢谢标记-brush.Stretch设置为none正是我所需要的。我想在我的项目中使用MediaPlayer而不是MediaElement,我不需要设置背景色,但感谢网格信息和代码示例中的MediaElement。
// create a grid and bind it to the parent window's size
var grid = new Grid { Background = Brushes.CornflowerBlue };    // <- sets background color
grid.SetBinding(WidthProperty, new Binding
{
    RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Window), 1),
    Path = new PropertyPath("ActualWidth"),
    Mode = BindingMode.OneWay,
    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
grid.SetBinding(HeightProperty, new Binding
{
    RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Window), 1),
    Path = new PropertyPath("ActualHeight"),
    Mode = BindingMode.OneWay,
    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});

// add the media player
grid.Children.Add(new MediaElement
{
    Source = new Uri("yourvideo.mp4", UriKind.RelativeOrAbsolute),
    LoadedBehavior = MediaState.Play,
    Stretch = Stretch.Fill,
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Center,
    Width = 640,    // <-- video size
    Height = 480
});

// wrap it all up in a visual brush
this.Background = new VisualBrush { Visual = grid };