Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture screen to be video using C# .Net?

I know there are lots of question like this.

But I don't want to use the Windows media encoder 9 because it's a problem to get one, and then it is no longer supported.

I know that, one possibility is to capture lots of screenshots and create a video with ffmpeg but I don't want use third party executables.

Is there are a .net only solution?

like image 588
masterchris_99 Avatar asked Jul 06 '11 06:07

masterchris_99


2 Answers

the answer is the Microsoft Expression Encoder. It is according to my opinion the easiest way to record something on vista and windows 7

private void CaptureMoni()
        {

            try
            {
                Rectangle _screenRectangle = Screen.PrimaryScreen.Bounds;
                _screenCaptureJob = new ScreenCaptureJob();
                _screenCaptureJob.CaptureRectangle = _screenRectangle;
                _screenCaptureJob.ShowFlashingBoundary = true;
                _screenCaptureJob.ScreenCaptureVideoProfile.FrameRate = 20;
                _screenCaptureJob.CaptureMouseCursor = true;

                _screenCaptureJob.OutputScreenCaptureFileName = string.Format(@"C:\test.wmv");
                if (File.Exists(_screenCaptureJob.OutputScreenCaptureFileName))
                {
                    File.Delete(_screenCaptureJob.OutputScreenCaptureFileName);
                }
                _screenCaptureJob.Start();
            }
            catch(Exception e) { }
        }
like image 63
masterchris_99 Avatar answered Sep 20 '22 16:09

masterchris_99


Edit Based on Comment Feedback:

A developer by the name baSSiLL has graciously shared a repository that has a screen recording c# library as well as a sample project in c# that shows how it can be used to capture the screen and mic.

Starting a screen capture using the sample code is as straight forward as:

recorder = new Recorder(_filePath,
            KnownFourCCs.Codecs.X264, quality,
            0, SupportedWaveFormat.WAVE_FORMAT_44S16, true, 160);

_filePath is the path of the file I'd like to save the video to.

You can pass in a variety of codecs including AVI, MotionJPEG, X264, etc. In the case of x264 I had to install the codec on my machine first but AVI works out of the box.

Quality only comes into play when using AVI or MotionJPEG. The x264 codec manages its own quality settings.

The 0 above is the audio device I'd like to use. The Default is zero.

It currently supports 2 wave formats. 44100 at 16bit either stereo or mono.

The true parameter indicates that I want the audio encoded into mp3 format. I believe this is required when choosing x264 as the uncompressed audio combined in a .mp4 file would not play back for me.

The 160 is the bitrate at which to encode the audio.

~~~~~

To stop the recording you just

recorder.Dispose(); recorder = null;

Everything is open source so you can edit the recorder class and change dimensions, frames per second, etc.

~~~~

To get up and running with this library you will need to either download or pull from the github / codeplex libraries below. You can also use NuGet:

Install-Package SharpAvi

Original Post:

Sharp AVI: https://sharpavi.codeplex.com/ or https://github.com/baSSiLL/SharpAvi

There is a sample project within that library that has a great screen recorder in it along with a menu for settings/etc.

I found Screna first from another answer on this StackoverFlow question but I ran into a couple issues involving getting Mp3 Lame encoder to work correctly. Screna is a wrapper for SharpAVI. I found by removing Screna and going off of SharpAvi's sample I had better luck.

like image 36
ickydime Avatar answered Sep 21 '22 16:09

ickydime