录屏.net代码

来自ling
跳转至: 导航搜索

[1] 相关代码转存到[2]

internal class ScreenRecordHelper
    {
        #region Fields
        private const int DEFAULT_FRAME_RATE = 15;
        private int screenWidth;
        private int screenHight;
        private int bitRate;
        private int frameRate;
        internal bool isRecording;
        private int framesCount;
        private string fileName;
        private Stopwatch stopWatch;
        private Rectangle screenArea;
        private VideoFileWriter videoWriter;
        private ScreenCaptureStream videoStreamer;
        private FolderBrowserDialog folderBrowser;
        private VideoCodec videoCodec;
        #endregion

        public ScreenRecordHelper()
        {
            screenWidth = SystemInformation.VirtualScreen.Width;//Screen.PrimaryScreen.Bounds.Width;
            screenHight = SystemInformation.VirtualScreen.Height; // Screen.PrimaryScreen.Bounds.Height;不论注释的或没注释的发现在win10系统上是不能全屏录制的,
                                        只能部分,win8上没试过,由于我装的是win10系统,没有win7,就先这样写了,至于win10系统有自己的
                                        一套录制方法,还挺牛逼,我们可以使用win+G键来开始,还可以自己来创建录制软件(只适用于win8.x
                                        及其以上),msdn提供了文章:
                                        https://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/dn642093.aspx

            frameRate = DEFAULT_FRAME_RATE;
            isRecording = false;
            framesCount = default(int);
            stopWatch = new Stopwatch();
            screenArea = Rectangle.Empty;
            videoWriter = new VideoFileWriter();
            folderBrowser = new FolderBrowserDialog();
            videoCodec = (VideoCodec)Enum.Parse(typeof(VideoCodec), ScreenRecord.codec);
            bitRate = ScreenRecord.bitRate;
        }

        /// <summary>
        /// 开始录屏
        /// </summary>
        internal void StartRecording()
        {
            try
            {
                InitializeRecordingParameters();
                videoStreamer = new ScreenCaptureStream(screenArea);
                videoStreamer.NewFrame += new NewFrameEventHandler(VideoStreamer_NewFrame);
                videoStreamer.Start();
                stopWatch.Start();

            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        internal void StopRecord()
        {
            isRecording = false;
        }

        private void VideoStreamer_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            if (isRecording)
            {
                framesCount++;
                videoWriter.WriteVideoFrame(eventArgs.Frame);
            }
            else
            {
                stopWatch.Reset();
                videoStreamer.Stop();
                videoWriter.Close();
                videoWriter.Dispose();
            }
        }

        private void InitializeRecordingParameters()
        {
            if (!isRecording)
            {
                isRecording = true;
                SetScreenArea();
                FileName = string.Format(@"{0}{1}-{2}.avi", "ScreenDraw", "bySTAR", new Random().Next(100) + DateTime.Now.ToString("yyyy-MM-dd_HH_mm"));
                videoWriter.Open(FileName, screenWidth, screenHight, frameRate, videoCodec, bitRate);
            }
        }

        private string FileName
        {
            get { return this.fileName; }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new ArgumentNullException
                        ("fileName", "File name can not be empty or null");
                }

                this.fileName = value;
            }
        }

        private void SetScreenArea()
        {
            foreach (Screen screen in Screen.AllScreens)
            {
                this.screenArea = Rectangle.Union(this.screenArea, screen.Bounds);
            }

            if (this.screenArea == Rectangle.Empty)
            {
                throw new InvalidOperationException("Screan area can not be set");
            }
        }
    }

  由于一些不得不做的事(哈,有时间再聊),所以就不得不写一个关于录屏的软件了,如此无力的开篇,开始吧。

  在网上搜了很多关于录屏的源码,发现都使不了,剧情的需要很难满足。于是突然想到了github上的一个录屏的软件,就引用过来了,稍加修改就用上了(核心代码基本没动)Bo.wang讨论)刚查了下,github上没了?!

  剧情需要:开机启动, 自动检测对应软件启动情况并进行录屏,当然最主要的就是录屏这个技术了。

  经过几番整弄算是能用了。

  这里贴上核心代码

需要配置如下方这三个

internal static string savePath = Directory.GetCurrentDirectory(); internal static string codec = "MSMPEG4v2"; internal static int bitRate = 2000000; 解码器可以设置"MPEG4", "WMV1", "WMV2", "MSMPEG4v2", "MSMPEG4v3", "H263P", "FLV1", "MPEG2", "Raw"

比特率就按自己想要的设定了,可以google下,我这里设定为200000,也有300000,或者低点,测试下就知道了

还有一个上方写的DEFAULT_FRAME_RATE,帧率,录制的帧率,我用的是15帧,感觉太低了手速不够

相关dll需要引入:

    avcodec-53.dll

    avdevice-53.dll

    avfilter-2.dll

    avformat-53.dll

    avutil-51.dll

    和最主要的有,AForge.dll,AForge.Video.dll,Aforge.Video.FFMPEG.dll

    其中创建的时候需要使用x86的的生成,这里给个在github上的那哥们的code,http://pan.baidu.com/s/1o81s0xs

可能不太深入,只是需求想做这样的软件,后边由于比较忙就没怎么深入,只是分享下困扰了我一段时间的录屏,希望大家多多包涵Bo.wang讨论

我在刚开始的时候也遇到了一些小问题,有些解决了,有些则是替代了