2013年3月22日星期五

转)C#实现录屏功能

转帖,C#实现录屏功能
  参考了一些方法,实现了录屏功能。(我的机器必须安装WMEncoder_cn.exe。稍微改写了下提供的代码。另改写成VB.NET后有异常,原因待查。。。)
  环境:windows xp
  用到的dll为:Interop.WMEncoderLib.dll,下载地址:http://download.csdn.net/detail/yysyangyangyangshan/4056611。如果有条件再安装上WMEncoder_cn.exe,下载地址:http://download.csdn.net/detail/yysyangyangyangshan/4056628。主要录屏方法如下:
        WMEncoder Encoder = null;
    //开始录制
        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);
                    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;
                }
                Encoder.Start();
            }
            catch (Exception ex)
            {
                
            } 
        }
    //停止录制
        private void button3_Click(object sender, EventArgs e)
        {
            Encoder.Stop();
        }

录屏工程详见:http://download.csdn.net/detail/yysyangyangyangshan/4056621,编程环境VS2008。

没有评论:

发表评论