zoukankan      html  css  js  c++  java
  • C#实现录制屏幕

    C#实现录制屏幕

      以前写过两篇录制麦克风语音和摄像头视频的文章(实现语音视频录制在服务器端录制语音视频),最近有朋友问,如果要实现屏幕录制这样的功能,该怎么做了?实际上原理是差不多的,如果了解了我前面两篇文章中介绍的内容,只要在它们的基础上做一些修改就可以了。

    一.实现原理

       实现方案仍然基于OMCS+MFile构建,原理与实现语音视频录制差不多,我这里只列出其中的主要差异:

    (1)使用DynamicDesktopConnector连接到屏幕桌面。

    (2)使用定时器(比如10fps,则每隔100ms一次)定时调用DynamicDesktopConnector的GetCurrentImage方法,把得到的图像使用MFile写入视频文件。

    (3)Demo演示的是不需要同时录制麦克风的声音,所以使用了MFile提供的SilenceVideoFileMaker组件(而非原来的VideoFileMaker组件),仅仅录制视频数据。

    (4)通过MultimediaManager的DesktopEncodeQuality属性,控制屏幕图像的清晰度。

    二.实现代码

      该Demo的所有源码如下所示,如果不想下载Demo,可以直接通过下面的代码了解详细的实现思路。

    复制代码
        public partial class Form1 : Form
        {
            private MultimediaServer server; //在本地内嵌OMCS服务器
            private IMultimediaManager multimediaManager;
            private SilenceVideoFileMaker maker = new SilenceVideoFileMaker(); //录制无声视频
            private DynamicDesktopConnector dynamicDesktopConnector = new DynamicDesktopConnector(); //远程桌面连接器
           
            public Form1()
            {
                InitializeComponent();
                int port = 9900;
                OMCSConfiguration config = new OMCSConfiguration(10,8, EncodingQuality.High,16000,640,480,"") ;
                this.server = new MultimediaServer(port, new DefaultUserVerifier(), config, false, null);
    
                this.multimediaManager = MultimediaManagerFactory.GetSingleton();
                this.multimediaManager.DesktopEncodeQuality = 1; //通过此参数控制清晰度 
                this.multimediaManager.Initialize("aa01", "", "127.0.0.1", port);
    
                this.dynamicDesktopConnector.ConnectEnded += new ESBasic.CbGeneric<ConnectResult>(dynamicDesktopConnector_ConnectEnded);
                this.dynamicDesktopConnector.BeginConnect("aa01"); //连接本地桌面          
    
                this.Cursor = Cursors.WaitCursor;            
            }       
    
            void dynamicDesktopConnector_ConnectEnded(ConnectResult obj)
            {
                System.Threading.Thread.Sleep(500);
                this.Ready();  
            }       
    
            private void Ready()
            {
                if (this.InvokeRequired)
                {
                    this.BeginInvoke(new CbGeneric(this.Ready));
                }
                else
                {
                    this.Cursor = Cursors.Default;
                    this.button1.Enabled = true;
                    this.label1.Visible = false;
                }
            }
    
            private System.Threading.Timer timer;
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    Oraycn.MFile.GlobalUtil.SetAuthorizedUser("FreeUser", "");
                    //初始化H264视频文件
                    this.maker.Initialize("test.mp4", VideoCodecType.H264, this.dynamicDesktopConnector.DesktopSize.Width, this.dynamicDesktopConnector.DesktopSize.Height, 10);
    
                    this.timer = new System.Threading.Timer(new System.Threading.TimerCallback(this.Callback), null ,0, 100);
                    this.label1.Text = "正在录制......";
                    this.label1.Visible = true;
                    this.button1.Enabled = false;
                    this.button2.Enabled = true;
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.Message);
                }
            } 
    
            //定时获取屏幕图像,并使用MFile写入视频文件
            private void Callback(object state)
            {            
                Bitmap bm = this.dynamicDesktopConnector.GetCurrentImage();
                this.maker.AddVideoFrame(bm);
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                this.timer.Dispose();
                this.button1.Enabled = false;
                this.button2.Enabled = false;
                this.label1.Visible = false;
    
                this.maker.Close(true);
                MessageBox.Show("生成视频文件成功!");
            }
        }
    复制代码

    三.Demo下载

           录制屏幕Demo源码 

      F5运行Demo,我录制了一段时间的自己桌面为mp4视频文件(我的屏幕分辨率为1920*1080),然后,使用QQ影音播放效果如下:

             

      本文的Demo还可以做更多的扩展,比如: 

    (1)当前demo仅仅是录制了无声的屏幕图像,如果希望在录制屏幕的同时,录制麦克风的声音(比如,录制课件时经常有这样的需求),那么,可以引入MicrophoneConnector,并使用MFile的VideoFileMaker组件就可以了。 

    (2)如果把OMCS服务端从demo中拆出来,并用两个客户端帐号登录,就可以实现远程屏幕录制了。当然,这需要有足够大的带宽来支持。

     

     

     
     
    分类: C#专栏
  • 相关阅读:
    实例教程五:采用SharedPreferences保存用户偏好设置参数
    实例教程四:采用Pull解析器解析和生成XML内容
    实例教程六:创建数据库与完成数据添删改查第一种写法
    实例教程二:短信发送器
    实例教程九:采用ContentProvider对外共享数据
    带手势滑动的日历Demo
    实例教程三:文件的保存与读取
    短信快速回复(源码)
    实例教程八:采用ListView实现数据列表显示
    javascript中的变量申明
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3594638.html
Copyright © 2011-2022 走看看