XAML 主要 window 上的视频绘图?
VideoDrawing on XAML main window?
我在 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;
}
}
这里是 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>
查看上面的行“drawing.Rect = new Rect(…) 并注意注释 - 无论我将 Rect 设置为多大 - 视频始终以 [=21= 的大小播放] MainWindow 大小 (620, 400),但是我必须至少设置一些 Rect 大小我不能将它设置为 0 或将其注释掉。看起来视频应该以设置的 Rect 大小播放,除非它更大比 XAML MainWindow 还大?我不明白我在做什么,为什么视频不能播放到 Rect 的大小?
将拉伸模式设置为None:
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 };
我在 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;
}
}
这里是 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>
查看上面的行“drawing.Rect = new Rect(…) 并注意注释 - 无论我将 Rect 设置为多大 - 视频始终以 [=21= 的大小播放] MainWindow 大小 (620, 400),但是我必须至少设置一些 Rect 大小我不能将它设置为 0 或将其注释掉。看起来视频应该以设置的 Rect 大小播放,除非它更大比 XAML MainWindow 还大?我不明白我在做什么,为什么视频不能播放到 Rect 的大小?
将拉伸模式设置为None:
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 };