示例#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));
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Encoder = new WMEncoder();

                IWMEncSourceGroup SrcGrp;

                IWMEncSourceGroupCollection SrcGrpColl;

                SrcGrpColl = Encoder.SourceGroupCollection;

                SrcGrp = SrcGrpColl.Add("SG_1");

                IWMEncSource SrcVid = null;

                IWMEncSource SrcAud = null;

                SrcVid = SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);

                SrcAud = SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);

                SrcAud.SetInput("Default_Audio_Device", "Device", "");

                SrcVid.SetInput("ScreenCapture1", "ScreenCap", "");

                IWMEncProfileCollection ProColl;

                IWMEncProfile Pro;

                int i;

                long lLength;

                ProColl = Encoder.ProfileCollection;

                lLength = ProColl.Count;

                for (i = 0; i < lLength - 1; i++)
                {
                    Pro = ProColl.Item(i);

                    Console.WriteLine("<><>>" + Pro.Name);

                    if (Pro.Name == "屏幕视频/音频 - 高(CBR)")
                    {
                        SrcGrp.set_Profile(Pro);

                        break;
                    }
                }
                IWMEncDisplayInfo Descr;

                Descr = Encoder.DisplayInfo;

                Descr.Author = "";

                Descr.Copyright = "";

                Descr.Description = "";

                Descr.Rating = "";

                Descr.Title = "";

                IWMEncAttributes Attr;

                Attr = Encoder.Attributes;

                IWMEncFile File;

                File = Encoder.File;

                //if (label1.Text != string.Empty)
                //{
                //    File.LocalFileName = @"C:\1.WMA"; //保存路径
                //}
                //else
                //{
                //    MessageBox.Show("请先选择路径!");
                //    return;
                //}

                File.LocalFileName = @"C:\1.WMA"; //保存路径

                Encoder.Start();
            }
            catch (Exception ex)
            {
                Encoder.Stop();
            }
        }