通过meia encoder捕获桌面,带预览(可用)

//添加引用 windows media encoder 和 windows media encoder preview

using WMEncoderLib;
using WMPREVIEWLib;
        WMEncoder Encoder;
        private void Form1_Load( )
        {
            Encoder = new WMEncoder();
            IWMEncSourceGroup SrcGrp;
            IWMEncSourceGroupCollection SrcGrpColl;
            SrcGrpColl = Encoder.SourceGroupCollection;
            SrcGrp = SrcGrpColl.Add("SG_1");

            IWMEncSource SrcVid;
            IWMEncSource SrcAud;
            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", "");


            //预览
            IWMEncDataViewCollection DVColl_Postview;
            DVColl_Postview = SrcVid.PostviewCollection;
            WMEncDataView Postview;
            Postview = new WMEncDataView();
            int lPostviewStream;
            lPostviewStream = DVColl_Postview.Add(Postview);


            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);
                if (Pro.Name == "Windows Media Video 8 for Local Area Network (384 Kbps)")
                {
                    SrcGrp.set_Profile(Pro);
                    break;
                }
            }

            IWMEncDisplayInfo Descr;
            Descr = Encoder.DisplayInfo;
            Descr.Author = "Armoghan Asif";
            Descr.Copyright = "Copyright information";
            Descr.Description = "Text description of encoded content";
            Descr.Rating = "Rating information";
            Descr.Title = "Title of encoded content";

            IWMEncAttributes Attr;
            Attr = Encoder.Attributes;
            Attr.Add("URL", "www.adnare.com");

            IWMEncFile File;
            File = Encoder.File;
            File.LocalFileName = @"C:/OutputFile.avi";
            // SrcVid.CroppingBottomMargin = 2;
            // SrcVid.CroppingTopMargin = 2;
            // SrcVid.CroppingLeftMargin = 2;
            // SrcVid.CroppingRightMargin = 2;


            Encoder.Start();


            Postview.SetViewProperties(lPostviewStream, (int)panel2.Handle);
            Postview.StartView(lPostviewStream);
        }

        private void button1_Click2(object sender, System.EventArgs e)
        {
            this.Encoder.Stop();

            // if( Encoder.RunState )
            // {
            // Encoder.Stop();
            // Application.Exit();
            // }
        }

 

 

 

 

 

以下为参考的源代码之一


                // 创建一个Windows Media Encoder对象.
                WMEncoder Encoder = new WMEncoder();

                //获取一个源的集合.
                IWMEncSourceGroupCollection SrcGrpColl = Encoder.SourceGroupCollection;
              
                //创建一个源"SG_1"
                IWMEncSourceGroup2 SrcGrp = (IWMEncSourceGroup2)SrcGrpColl.Add("SG_1");

                //创建一个音频和视频的源

                IWMEncVideoSource2 SrcVid = (IWMEncVideoSource2)SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);
                IWMEncSource SrcAud = SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);

                //指定源文件
                SrcAud.SetInput("C://jintian.mp3", "", "");//MP3
                SrcVid.SetInput("C://result.wmv", "", "");//视频,可换成图片

                //裁剪场景
                SrcVid.CroppingBottomMargin = 25;
                SrcVid.CroppingLeftMargin = 25;
                SrcVid.CroppingRightMargin = 25;
                SrcVid.CroppingTopMargin = 25;

                //预览
                IWMEncDataViewCollection DVColl_Postview;
                DVColl_Postview = SrcVid.PostviewCollection;
                WMEncDataView Postview;
                Postview = new WMEncDataView();
                int lPostviewStream;
                lPostviewStream = DVColl_Postview.Add(Postview);

                IWMEncProfileCollection ProColl = Encoder.ProfileCollection;
                WMEncProfile2 Pro;
                Pro = new WMEncProfile2();

                //定义配置文件profile.具体的请看media encoder中的设置
               /*for (int i = 0; i < ProColl.Count; i++)
                {
                    Pro = ProColl.Item(i);
                    if (Pro.Name == "屏幕视频 - 高(CBR)")
                    {*/
                Pro.LoadFromFile("myPrx.prx");
                        SrcGrp.set_Profile(Pro);
                        /*break;
                    }
                }*/
              
                 //输出到目的文件
                IWMEncFile2 File = (IWMEncFile2)Encoder.File;
                File.LocalFileName = "C://test.wmv";              

                //如果源是图片则需要取消如下的注释。因为图片是静止的,本身不会播放.开启广播模式将会使图片进行不间断广播,从而实现连续的录制
                /*Encoder.PrepareToEncode(true);
                IWMEncBroadcast Brdcst = Encoder.Broadcast;

                Brdcst.set_PortNumber(WMENC_BROADCAST_PROTOCOL.WMENC_PROTOCOL_HTTP, 8080);
                */

                Encoder.Start();//录制开始

                Postview.SetViewProperties(lPostviewStream, (int)panel2.Handle);
                Postview.StartView(lPostviewStream);

 

 

 

 

 

 

 

你可能感兴趣的:(windows,video,File,NetWork,button,audio)