Search code examples
c#videoaforgescreen-recording

c# VideoFileWriter properties


I want to record my screen while using my program.

Now I am using this code:

recorder.Open(pathFolder+GetCurrentDateAndTime() + ".mp4", Convert.ToInt32(System.Windows.SystemParameters.PrimaryScreenWidth), Convert.ToInt32(System.Windows.SystemParameters.PrimaryScreenHeight), 10, VideoCodec.MPEG4, 2000000);

The record is good but it's too fast.

What should I change to prevent it to be too fast and to be in normal speed?


Solution

  • First of all, initialize a timer control and assign properties to the control. Then, create a tick event to that timer.

    videoTimer = new Timer(); videoTimer.Interval = 20; videoTimer.Tick += videoTimer_Tick;

    vfWriter = new VideoFileWriter(); vfWriter.Open("Exported_Video.avi", 800, 600, 25, VideoCodec.MPEG4, 1000000);

    Then create a start button to start the timer.

    private void btnStart_Click(object sender, EventArgs e) { videoTimer.Start(); }
    

    In the timer tick event, create a bitmap image from the size of VideoFileWriter and capture screen and write it to bitmap image. Then, write the image to VideoFileWriter.

    private void videoTimer_Tick(object sender, EventArgs e){bp = new Bitmap(800, 600); gr = Graphics.FromImage(bp);gr.CopyFromSceen(0, 0, 0, 0, new Size(bp.Width, bp.Height));
    pictureBox1.Image = bp;
    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
    vfWriter.WriteVideoFrame(bp);
    

    }

    In the end create a stop button to stop the timer and save the file.

    private void btnStop_Click(object sender, EventArgs e){ videoTimer.Stop();vfWriter.Close();}