简体   繁体   English

XAML主窗口上的VideoDrawing?

[英]VideoDrawing on XAML main window?

I have the following simple example in WPF to play a video file using a VideoDrawing object - here is the code behind: 我在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;            
    }
}

and here is the XAML: 这是XAML:

<Window x:Class="MyMediaPlayer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MediaPlayer in WPF" Width="620" Height="400" 
    WindowStyle="None"
    ShowInTaskbar="True"
    AllowsTransparency="True"
    Background="Transparent"
    WindowStartupLocation="Manual"
    Left="0"
    Top="0">

</Window>

look at the lines "drawing.Rect = new Rect(…) above and note the comments - no matter what size I set the Rect to - the video always plays at the size of the XAML MainWindow size (620, 400), however I have to set at least some Rect size I can't set it to 0 or comment it out. It seems like the video ought to play at the Rect size set, unless it is larger than the XAML MainWindow? What is it I don't understand about what I am doing and why doesn't the video play to the size of the Rect? 查看上面的“ drawing.Rect = new Rect(…)”行,并注意注释-无论我将Rect设置为什么大小-视频始终以XAML MainWindow大小(620,400)的大小播放。必须至少设置一些Rect大小,我不能将其设置为0或将其注释掉。好像视频应该以Rect大小设置播放,除非它大于XAML MainWindow?不了解我在做什么,为什么视频无法播放Rect的大小?

Set the stretch mode to None: 将拉伸模式设置为“无”:

brush.Stretch = Stretch.None;

The problem with this of course is that you now don't have a way to set the color of the area around the player. 当然,问题在于您现在无法设置播放器周围区域的颜色。 If you want control of that then you'll have to switch to a VisualBrush and use a MediaElement instead: 如果要控制它,则必须切换到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 };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM