Vlc.DotNet stretched over the entire video image control method

Vlc.DotNet stretched over the entire video image control method

Reference Vlc.DotNet

The official example, VLC first download, which the VLC various dll copied to the output directory, and then install the package Nuget Vlc.DotNet.Core.Interops, Vlc.DotNet.Core, Vlc.DotNet selection depending on the type of procedure. Forms or Vlc.DotNet.Wpf. There are many online tutorials specific examples. I am sure you search to this question also was set for the environment.

Implementation code

Here is my program with Wpf, the direct effect. Here Insert Picture Description
xaml code looks
substantially the same as the sample code and the official

<Window x:Class="VLCTest.DemoWin"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wpf="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"
        xmlns:local="clr-namespace:VLCTest"
        mc:Ignorable="d"
        Title="DemoWin" Height="450" Width="800">
    <Grid>
        <ContentControl Width="400" Height="400" Name="Container">
            <wpf:VlcControl x:Name="VlcControl" />
        </ContentControl>
        <Button Content="Stop" HorizontalAlignment="Left" Margin="664,360,0,0" VerticalAlignment="Top" Width="75" Click="StopPlay"/>
    </Grid>
</Window>

Play code is as follows

        private void Play()
        {
            string path = @"D:\TestFiles\TestVideo.mp4";
            string currentDirectory = Directory.GetCurrentDirectory();
            DirectoryInfo vlcDirectory = new DirectoryInfo(System.IO.Path.Combine(currentDirectory, "VLC"));
            VlcControl.SourceProvider.CreatePlayer(vlcDirectory);
            FileInfo playFile = new FileInfo(path);
            // 这里把帧图像拉伸成填满整个控件
            var viewBox = VlcControl.Content as Viewbox;
            viewBox.Stretch = System.Windows.Media.Stretch.Fill;
            VlcControl.SourceProvider.MediaPlayer.Play(playFile);
        }

Here it can be seen that, as long as the control mode into the inside of the ViewBox Fill stretching to fill the control.

Realization of ideas

Thanks to the big brother here first article provides a guideline for me.
Reference: https: //my.oschina.net/duoing/blog/1499664
to achieve the effect of the video picture fills the entire control, in fact, is to export it to change the aspect ratio of the video. There are two ideas:
1 video stream rendered at the right time on the screen of the aspect ratio of a certain scale and rendering. This need is achieved by changing the parameters or the aspect ratio of a method call in the call VLC.
2. get render each frame, before presentation to the interface, the picture is scaled to adjust the aspect ratio and then output to the screen. This program will rely on the interface program to achieve, that is, by our own written procedures for each frame reprocessed once and then output.
The above code uses the second program.

Scheme comparison

The first scenario is significantly more performance than saving the second option, the source put it in frames to render better. The second option, although more than a stretch operation screen, but it does not require in-depth understanding of the internal implementation mechanism of VLC. Overall, in fact, belong to the second option tricky program encountered multi-zone playback high-definition video or playing time may still have Caton.

Record stepped pit

Decided to start their own drawing frames, my first thought would take a look at the source code VlcControl Vlc.dotnet.Wpf is how to achieve this control, the results look a little code is found, only a few dozen lines. Although there are dozens of lines, but still very hard core, frame rendering and presentation of the complete separation, in the control where it is only responsible to get the picture Image thrown inside, and then bind the rendering source, each change to a changing the screen.
Part of the source code

        /// <summary>
        /// The constructor
        /// </summary>
        public VlcControl()
        {
            sourceProvider = new VlcVideoSourceProvider(this.Dispatcher);
            this.viewBox = new Viewbox
            {
                Child = this.videoContent,
                Stretch = Stretch.Uniform
            };

            this.Content = this.viewBox;
            this.Background = Brushes.Black;
            // Binds the VideoSource to the Image.Source property
            this.videoContent.SetBinding(Image.SourceProperty, new Binding(nameof(VlcVideoSourceProvider.VideoSource)) { Source = sourceProvider });
        }

Just right here VlcControl inherited from UserControl control, which the Content is ViewBox, the ViewBox stretching into Fill mode to just be able to achieve results.
Finally, to thank Vlc.dotnet of good design and open source spirit, standing on the shoulders of giants we see farther.

Vlc.dotnet Source: https: //github.com/ZeBobo5/Vlc.DotNet/tree/develop/src
if this article have the opportunity to help you, please do not mean your praise.

Published 15 original articles · won praise 2 · Views 4857

Guess you like

Origin blog.csdn.net/weixin_38138153/article/details/100674017