C# How to draw a overlay layer on Libvlcsharp videoview object?

170 views Asked by At

i need write some OSD information on my video i use libvlcsharp and videoview for play my videos. i saw when i play video, i can't access videos bitmap to draw some graphics on it this is my code and result thank for your help :)

   public partial class Form1 : Form
    {
        private LibVLC libVLC;
        private MediaPlayer mediaPlayer;

        public Form1()
        {
            InitializeComponent();
            libVLC = new LibVLC();
            mediaPlayer = new MediaPlayer(libVLC);
            var myMedia = new Media(libVLC, new Uri(@"C:\ffmpeg\Sample.mp4"));
            mediaPlayer.Media = myMedia;
            videoView1.MediaPlayer = mediaPlayer;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void cmdPlay_Click(object sender, EventArgs e)
        {
            if (videoView1.MediaPlayer.IsPlaying)
            {
                cmdPlay.Text = "Play";
                videoView1.MediaPlayer.Pause();
            }
            else
            {
                cmdPlay.Text = "Pause";
                videoView1.MediaPlayer.Play();
            }
        }

        private void videoView1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.DrawString("Hello", new Font("Arial", 9, FontStyle.Bold), Brushes.White, new Point(10, 10));
        }

        private void cmdDrawOSD_Click(object sender, EventArgs e)
        {
            Graphics g = Graphics.FromHwnd(videoView1.Handle);
            g.DrawString("Hello", new Font("Arial", 9, FontStyle.Bold), Brushes.White, new Point(20, 20));
        }

        private void cmdRewind_Click(object sender, EventArgs e)
        {
            videoView1.MediaPlayer.Stop();
        }
    }

videoview paint event is working before play any video, if you play any video, this code not draw anythings on screen

        private void videoView1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.DrawString("Hello", new Font("Arial", 9, FontStyle.Bold), Brushes.White, new Point(10, 10));
        }

i try to access videoview graphics by this code

            Graphics g = Graphics.FromHwnd(videoView1.Handle);
            g.DrawString("Hello", new Font("Arial", 9, FontStyle.Bold), Brushes.White, new Point(20, 20));

and it's work if you don't play video, when you play a video ,this code don't work and you can't see anythings happend on videoview object.

Result without play video Result when play video

thanks for your attention

I try to access graphics handle but not successfuly yet when video play all drawing disappear

2

There are 2 answers

0
cube45 On

You can try drawing another window (frameless topmost window) on top of the first one and sync the move/resizes event on top of it.

This is what is done in LibVLCSharp.WPF, but for WPF

2
mfkl On

You have to use the video callbacks for this, but you will have poor performance as it removes hardware decoding so you can interact with that bitmap from CPU. Here's a starting point https://github.com/mfkl/libvlcsharp-samples/blob/master/PreviewThumbnailExtractor/Program.cs

For the best performance, use the equivalent libvlc 4 api which uses direct3d or opengl directly.