c#录制屏幕 超级简单!

使用Aforge.net对屏幕进行截图,并将每一张BITMAP写到AVI文件中。超级简单!

AFORGE.NET的ScreenCaptureStream性能有限,目前测试基本能达到10帧左右。

设置AVIWriter的FrameRate为8帧,但是播放起来像快进一样,目前还没有找到原因。暂时采取一个New Frame到达时,多写几次的方法进行规避。

有知道如何处理的请联系我。

c#录制屏幕 超级简单!_第1张图片

详细代码如下:

 

  public partial class Form1 : Form
    {
        Screen c = Screen.PrimaryScreen;

        AVIWriter write;
        /// 
        /// 截屏实例
        /// 
        ScreenCaptureStream cap;
        Pen p = new Pen(Brushes.Red, 5);
        Pen p2 = new Pen(Brushes.Green, 5);
        bool isStart = false;

        public Form1()
        {
            InitializeComponent();
      
         }
        void cap_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            int x = Control.MousePosition.X;
            int y = Control.MousePosition.Y;
            using (Graphics g = Graphics.FromImage(eventArgs.Frame))
            {
                //画时间
                g.DrawString(DateTime.Now.ToString(), new Font("宋体", 14f, FontStyle.Bold), Brushes.Red, new PointF(100, 100));
                //画鼠标位置
                g.DrawEllipse(p, new Rectangle(x - 10, y - 10, 20, 20));
            }
            if (Control.MouseButtons == MouseButtons.Left)
            {
                //鼠标左键点击时,绘制点击图像
                Bitmap bt_re = (Bitmap)eventArgs.Frame.Clone();
                using (Graphics g = Graphics.FromImage(bt_re))
                {
                    g.SmoothingMode = SmoothingMode.HighSpeed;
                    g.DrawEllipse(p2, new Rectangle(x - 20, y - 20, 40, 40));
                }
                write.AddFrame(eventArgs.Frame);
                write.AddFrame(bt_re);
                write.AddFrame(bt_re);
                write.AddFrame(eventArgs.Frame);
            }
            else
            {
                write.AddFrame(eventArgs.Frame);
            }
            //多写一次,如果不多写,感觉播放像在快进。前面设置了帧速率也没什么用。有知道的高手请回答。
            write.AddFrame(eventArgs.Frame);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            button2.Enabled = false;
            p.StartCap = LineCap.Round;
            p.EndCap = LineCap.Round;
            p2.StartCap = LineCap.Round;
            p2.EndCap = LineCap.Round;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            isStart = true;
            cap = new ScreenCaptureStream(c.Bounds,125);
            cap.NewFrame += new NewFrameEventHandler(cap_NewFrame);
            string filename=DateTime.Now.ToString()+"avi";
            SaveFileDialog sf = new SaveFileDialog();
            sf.Filter = "视频文件|*.avi";
            if (sf.ShowDialog() == DialogResult.OK)
            {
                filename = sf.FileName;
                write = new AVIWriter();
                write.Open(filename, c.Bounds.Width, c.Bounds.Height);
                write.FrameRate = 8;
                cap.Start();
            }
            button1.Enabled = false;
            button2.Enabled = true;
        }

      


        private void button2_Click(object sender, EventArgs e)
        {
            //多等1s记录画面
            Thread.Sleep(1000);
            cap.Stop();
            write.Close();
            write.Dispose();
            button1.Enabled = true;
            button2.Enabled = false;
        }
    }


 

你可能感兴趣的:(C#)