1

What is the best and easiest way to record the Screen using C# / Win Forms. I have tried a lot of things but nothing works properly. The best i got was 20 fps and it was not stable. I tried Task Run and Thread pool but cant get it to work properly. I want to record the screen in 30 FPS and display the recording in real time in a PictureBox. Later I want to adjust the FPS so the screen can be recorded in different FPSs.

Thanks in Advance.

Image of what I have done till now. Image 1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using System.Windows.Forms;

namespace ScreenRecorder1
{
    public partial class Form1 : Form
    {
        // Recording Size
        Size CaptureSize = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

        bool Not_closed = true; // Stop Work on Close

         // Capture Thread 1
        Thread captureThread1;      
  
   

        int count_FPS = 0;
     
 


        public Form1()
        {
            InitializeComponent();  
        }





         //FPS:::::::::::::::::::::START:::::::

        //Timer FPS - Display
        System.Windows.Forms.Timer FPS_TimerSec;
  
        
        // FPS - Timer Settings
        private void FPS_TimerSec_Settings()
        {
            FPS_TimerSec = new System.Windows.Forms.Timer();
            FPS_TimerSec.Interval = 1000;
            FPS_TimerSec.Tick += new EventHandler(FPS_TimerSec_Tick);
            FPS_TimerSec.Enabled = true;
            FPS_TimerSec.Start();
            
        }


         // FPS -Timer - Tick
        void FPS_TimerSec_Tick(object sender, EventArgs e)
        {
              
            current_fps_label.Text = count_FPS.ToString(); // Display FPS
            count_FPS = 0;
           
        }

        //FPS:::::::::::::::::::::END:::::::














        // Load
        private void Form1_Load(object sender, EventArgs e)
        {

            // Timer
            FPS_TimerSec_Settings();


            // Capture - Thread
            captureThread1 = new Thread(Record12);
            captureThread1.IsBackground = true;
            captureThread1.Start();


        }





 


       








        // Aborts the Thread On Form Close // Stops Crashing on Form CLose
        protected override void OnClosing(CancelEventArgs e)
        {

            Not_closed = true; // Stop Before Exiting

            base.OnClosing(e);
        }




            
           public  void Record12()
           {
            

              while(Not_closed)
              {
                count_FPS++;
                Task<Task> task = new Task<Task>(async () => {
                    await CaptureImage();
                });
                task.Start();
                task.Wait();
                task.Result.Wait();

                //await task;

                //Task.Delay(40).Wait();


                //Task.Run(() =>
                //{
                //    count_FPS++;


                //    CaptureImage();


            //});
                         

                        

              }
           }




        //public async Task PeriodicFooAsync(TimeSpan interval, CancellationToken cancellationToken)
        //{
        //    while (true)
        //    {
        //        await CaptureImage();
        //        await Task.Delay(interval, cancellationToken);
        //    }
        //}




        //async Task MethodName(object e, ElapsedEventArgs args)


      private async Task CaptureImage()
        {
            await Task.CompletedTask;

            using (Bitmap b = new Bitmap(CaptureSize.Width, CaptureSize.Height))
                {


                    using (Graphics g = Graphics.FromImage(b))
                    {
                        g.CopyFromScreen(0, 0, 0, 0, CaptureSize, CopyPixelOperation.SourceCopy);

                    }


                    Invoke(new Action(() =>
                    {
                        if (Player_pictureBox.BackgroundImage != null)
                        {
                            Player_pictureBox.BackgroundImage.Dispose();
                        }

                        Player_pictureBox.BackgroundImage = b.Clone(
                        new Rectangle(0, 0, b.Width, b.Height),
                        System.Drawing.Imaging.PixelFormat.DontCare);

                        Player_pictureBox.BackgroundImageLayout = ImageLayout.Stretch;



                        //Player_pictureBox.BackgroundImage = b;

                    }));

                }
           


        }


       



    }




}
Stefan27
  • 845
  • 8
  • 19
  • I'm not well versed in this sort of thing at all, but I can't imagine repeatedly capturing images and throwing them in a PictureBox will be very performant. Have you looked at posts like [How to capture screen to be video using C# .Net?](https://stackoverflow.com/questions/4068414/how-to-capture-screen-to-be-video-using-c-sharp-net) – Broots Waymb Aug 10 '20 at 13:28
  • Thanks I will look at it. – Stefan27 Aug 10 '20 at 17:29

0 Answers0