C#實現螢幕錄製
阿新 • • 發佈:2019-02-18
public partial class Form1 : Form { private MultimediaServer server; //在本地內嵌OMCS伺服器 private IMultimediaManager multimediaManager; private SilenceVideoFileMaker maker = new SilenceVideoFileMaker(); //錄製無聲視訊 private DynamicDesktopConnector dynamicDesktopConnector = newDynamicDesktopConnector(); //遠端桌面聯結器 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 += newESBasic.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("生成視訊檔案成功!"); } }