示例#1
0
        public VideoHelper(string saveToPath)
        {
            if (string.IsNullOrEmpty(saveToPath))
            {
                _savetoPath = string.Format(@"{0}\{1}.wmv", Environment.CurrentDirectory, TextHelper.RandomString(10));
            }
            else
            {
                _savetoPath = saveToPath;
            }

            try
            {
                encoder = new WMEncoder();

                IWMEncSourceGroup2          SrcGrp;
                IWMEncSourceGroupCollection SrcGrpColl;
                SrcGrpColl = encoder.SourceGroupCollection;
                SrcGrp     = (IWMEncSourceGroup2)SrcGrpColl.Add("SG_1");
                IWMEncVideoSource2 SrcVid;
                SrcVid = (IWMEncVideoSource2)SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);
                SrcVid.SetInput("ScreenCapture1", "ScreenCap", "");
                IWMEncProfileCollection ProColl;
                IWMEncProfile           Pro;
                int  i;
                long lLength;
                ProColl = encoder.ProfileCollection;
                lLength = ProColl.Count;

                for (i = 0; i < lLength; i++)
                {
                    Pro = ProColl.Item(i);
                    if (Pro.Name == "Screen Video High (CBR)")
                    {
                        SrcGrp.set_Profile((IWMEncProfile)Pro);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("{0}\r\nPlease install WMEncoder.exe http://www.microsoft.com/en-us/download/details.aspx?id=17792. \r\n And install WMEncoder Patch http://download.microsoft.com/download/0/3/D/03D35C05-67DA-40E0-9E45-3EA0CA6329A4/WindowsMedia9-KB929182-INTL.exe.", ex.Message));
            }
        }
示例#2
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            // 实例化Windows Media Encoder编码器
            enc = new WMEncoderClass();
            IWMEncSourceGroupCollection SrcGrpColl;
            IWMEncSourceGroup2          SrcGrp;
            // 音频源
            //IWMEncAudioSource SrcAud;
            // 视频源
            IWMEncVideoSource2 SrcVid;

            //IWMEncProfile2 Pro = new WMEncProfile2Class();

            try
            {
                SrcGrpColl = enc.SourceGroupCollection;
                SrcGrp     = (IWMEncSourceGroup2)SrcGrpColl.Add("SG_1");
                SrcVid     = (IWMEncVideoSource2)SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);

                Screen currentScreen;
                if (hWndURWPGSim2D == 0)
                {// 获取当前窗口所在屏幕作为当前屏幕
                    currentScreen = Screen.FromHandle(this.Handle);
                }
                else
                {// 获取待屏幕录像的URWPGSim2D程序主窗口所在屏幕作为当前屏幕
                    currentScreen  = Screen.FromHandle((IntPtr)hWndURWPGSim2D);
                    hWndURWPGSim2D = 0;
                }
                // 以当前屏幕设备作为视频来源设备
                SrcVid.SetInput(currentScreen.DeviceName, "ScreenCap", "");
                //SrcVid.SetInput("ScreenCapture1", "ScreenCap", "");

                // 设置音频来源
                //if (ckbSound.Checked)
                //{
                //    SrcAud = (IWMEncAudioSource)SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);
                //    SrcAud.SetInput("Default_Audio_Device", "DEVICE", "");
                //}

                foreach (IWMEncProfile wp in enc.ProfileCollection)
                {     // 循环检查当前编码器实例中包含的全部配置文件
                    if (wp.Name.Equals(cmbEncoder.SelectedItem.ToString()))
                    { // 如果当前配置文件(Profile)名称与选中的移植
                        // 将当前Profile设为当前视频源的配置文件
                        SrcGrp.set_Profile(wp);
                        break;
                    }
                }

                // 为当前编码器实例设定输出文件名称
                enc.File.LocalFileName = txtFileName.Text;

                // 从界面输入取得待录屏区域左上角坐标/宽度/高度(单位为像素)
                int x = Convert.ToInt32(txtX.Text);
                int y = Convert.ToInt32(txtY.Text);
                int w = Convert.ToInt32(txtWidth.Text);
                int h = Convert.ToInt32(txtHeight.Text);

                // 适应多显示器环境 取得当前系统所有显示器构成的虚拟屏幕区域
                Rectangle screenRect = SystemInformation.VirtualScreen;

                #region
                //// 为当前视频源设定宽度为当前屏幕宽度
                //SrcVid.Width = currentScreen.Bounds.Width;
                //// 为当前视频源设定高度为当前屏幕高度
                //SrcVid.Height = currentScreen.Bounds.Height;

                //// 对当前视频源进行裁剪 裁剪后的视频宽度高度即为待录像区域宽度高度
                //// 左边裁掉待录像区域左上角坐标X值的大小
                //SrcVid.CroppingLeftMargin = x;
                //// 上边裁掉待录像区域左上角坐标Y值的大小
                //SrcVid.CroppingTopMargin = y;
                //// 右边裁掉总宽度和左边裁剪尺寸、待录像区域宽度之差
                //SrcVid.CroppingRightMargin = SrcVid.Width - SrcVid.CroppingLeftMargin - w;
                //// 下边裁掉总高度和上边裁剪尺寸、待录像区域高度之差
                //SrcVid.CroppingBottomMargin = SrcVid.Height - SrcVid.CroppingTopMargin - h;
                #endregion

                #region
                //int wm = SystemInformation.VirtualScreen.Width;
                //int hm = SystemInformation.VirtualScreen.Height;

                //SrcVid.Width = w;
                //SrcVid.Height = h;

                //float rx = (float)wm / (float)SrcVid.Width;
                //float ry = (float)hm / (float)SrcVid.Height;

                //SrcVid.CroppingLeftMargin = (int)(x / rx);
                //SrcVid.CroppingTopMargin = (int)(y / ry);
                //SrcVid.CroppingRightMargin = (int)((wm - x - w) / rx);
                //SrcVid.CroppingBottomMargin = (int)((hm - y - h) / ry);
                #endregion

                #region
                // 为当前视频源设定宽度为整个虚拟屏幕宽度
                SrcVid.Width = screenRect.Width;
                // 为当前视频源设定高度为整个虚拟屏幕高度
                SrcVid.Height = screenRect.Height;

                // 对当前视频源进行裁剪 裁剪后的视频宽度高度即为待录像区域宽度高度
                // 左边裁掉待录像区域左上角坐标X值与虚拟屏幕左上角坐标X值之差
                SrcVid.CroppingLeftMargin = x - screenRect.Left;
                // 上边裁掉待录像区域左上角坐标Y值与虚拟屏幕左上角坐标Y值之差
                SrcVid.CroppingTopMargin = y - screenRect.Top;
                // 右边裁掉总宽度和左边裁剪尺寸、待录像区域宽度之差
                SrcVid.CroppingRightMargin = SrcVid.Width - SrcVid.CroppingLeftMargin - w;
                // 下边裁掉总高度和上边裁剪尺寸、待录像区域高度之差
                SrcVid.CroppingBottomMargin = SrcVid.Height - SrcVid.CroppingTopMargin - h;
                #endregion

                #region
                //SrcVid.Width = w;
                //SrcVid.Height = h;

                //float rx = (float)screenRect.Width / (float)SrcVid.Width;
                //float ry = (float)screenRect.Height / (float)SrcVid.Height;

                //SrcVid.CroppingLeftMargin = (int)((x - screenRect.Left) / rx);
                //SrcVid.CroppingTopMargin = (int)((y - screenRect.Top) / ry);
                //SrcVid.CroppingRightMargin = (int)((screenRect.Width - (x - screenRect.Left) - w) / rx);
                //SrcVid.CroppingBottomMargin = (int)((screenRect.Height - (y - screenRect.Top) - h) / ry);
                #endregion

                // 最小化Screencast程序主窗口
                this.WindowState = FormWindowState.Minimized;

                // 开始编码
                enc.Start();
                btnStop.Enabled  = true;
                btnStart.Enabled = false;
                this.TopMost     = false;
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
        }